Replacing echo with print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing echo with print
# 1  
Old 04-15-2015
Replacing echo with print

Hi everyone,

I'm executing a shell script and one of the commands is creating a file with text via echo.

However, if the text within echo has "\t" or similar, it automatically translates it into a TAB character, same goes for other special characters.

I know that if I put another "\" before it would ignore it and proceed as desired.

Code:
echo 'This is a test and want \t to be just a text' > newfile

ex:
\t => TAB (notok)
\\t => \t (ok)

But I don't intend to search for possible scenarios every time I run the script and was looking to create the file using print or similar (hoping that there is another way to remove this automatic translation of special chars. ) but I'm not so familiar with this function yet.

Do you have any solution for this?

Appreciate the help.
# 2  
Old 04-15-2015
Strange - works fine on Linux under bash.
What's your OS/shell and do you have 'echo' aliased to something else?
# 3  
Old 04-15-2015
Quote:
Originally Posted by vgersh99
Strange - works fine on Linux under bash.
What's your OS/shell and do you have 'echo' aliased to something else?
Its running with AIX OS.

Shell is currently as #! /usr/bin/ksh

The echo command is just literally as:

Code:
echo 'text
text
text
text' > /home/newfile

# 4  
Old 04-15-2015
Hi,
under bash, you have a set parameter to control these:
Code:
$ shopt -s xpg_echo
$ echo 'foo\tbar'
foo	bar
$ shopt -u xpg_echo
$ echo 'foo\tbar'
foo\tbar
$ echo -e 'foo\tbar'
foo	bar

Regards.
This User Gave Thanks to disedorgue For This Post:
# 5  
Old 04-15-2015
Code:
if [ "$(uname)" == "AIX" ] ; then
  ECHO="print -R"
else
  ECHO="echo"
fi

$ECHO "some text"

This User Gave Thanks to agent.kgb For This Post:
# 6  
Old 04-15-2015
echo will give undetermined results across shells and/or platforms when using special characters. The best way to get predictable behavior is to use printf with a format specifier.
Code:
printf '%s\n' 'This is a test and want \t to be just a text'

should reliably and constantly produce the output you require in any Posix or Bourne type shell across platforms.....

---
To get consistent echo behavior, you could add a simple function at the start of the script, that overrides the echo builtin and emulates the desired echo behavior (without the -n option)
Code:
echo () 
{ 
    printf "%s\n" "$*"
}

This should work, provided IFS is set to the standard $' \t\n' (space-tab-newline).

--
But to create a text file I would use a here document instead of printf commands..

For example
Code:
cat <<EOF >file
text
text\text2
text\text2\ntext3
text\text2\ntext3    text4
EOF

Note that the second EOF has to be at the start of the line

Last edited by Scrutinizer; 04-16-2015 at 01:30 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-16-2015
Quote:
Originally Posted by Scrutinizer
echo will give undetermined results across shells and/or platforms when using special characters. The best way to get predictable behavior is to use printf with a format specifier.
Code:
printf '%s\n' 'This is a test and want \t to be just a text'

should reliably and constantly produce the output you require in any Posix or Bourne type shell across platforms.....

---
To get consistent echo behavior, you could add a simple function at the start of the script, that overrides the echo builtin and emulates the desired echo behavior (without the -n option)
Code:
echo () 
{ 
    printf "%s\n" "$*"
}

This should work, provided IFS is set to the standard $' \t\n' (space-tab-newline).

--
But to create a text file I would use a here document instead of printf commands..

For example
Code:
cat <<EOF >file
text
text\text2
text\text2\ntext3
text\text2\ntext3    text4
EOF

Note that the second EOF has to be at the start of the line
Your suggestion using a here document worked best for all purposes. I don't even have to worry about quotes and double quotes using this solution. The script is much more improved now.
Smilie
Thank you very much.


EDITED: With this option, I just realized that I'm not able to input dollar sign as text, it will remove the entire variable. Do you have a workaround for this too without having to put \ before the $ sign?


Thanks!

Last edited by demmel; 04-16-2015 at 02:49 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

2. Shell Programming and Scripting

Using echo to print arguments inside a function

I am using echo in bash. Have created a function prargv which takes a number of arguments. Example: prargv "-e" "--examples" Inside prargv, I want to print all the arguments using echo echo "$@" This returns --examples rather than -e --examples" This problem can be fixed... (3 Replies)
Discussion started by: kristinu
3 Replies

3. Shell Programming and Scripting

Replacing part of the sentence using echo and sed

Hi, Iam using ksh and trying to execute the following syntax to replace one word of the sentence with a new word. But somehow sed is not able to replace the old value with new value. Please let me know where Iam going wrong. Sample Code : --> export line="VORTEX,abcdef" export... (3 Replies)
Discussion started by: ajithab
3 Replies

4. Shell Programming and Scripting

Print (echo) variable in a single line

Hi, I have written this code ------------------------------------------------ # !/bin/ksh i=0 while do j=$i while do echo -e $j #printf "%d",$j j=`expr $j - 1` done echo i=`expr $i + 1` done ---------------------------------------------------- The ouput which... (2 Replies)
Discussion started by: rac
2 Replies

5. Shell Programming and Scripting

Echo is replacing characters

Hi All, I'm using KSH and am writing a small script that counts the lines of various files in several folders under one root folder. Below is the script: #!/usr/bin/ksh common_path_in="/interface_in/rsc" file_out="/interface_in/rsc/record_count.csv" tot_rec_count=-1 act_rec_count=-1... (5 Replies)
Discussion started by: jagari
5 Replies

6. Shell Programming and Scripting

echo/print variable question

while read filer ; do echo $filer $1 $2; ssh $filer vfiler status -r | awk '/running/{host=$1}/Path:/{path=$2;print host,path}'; done < filers.list this will print node1 vfiler0 / vfiler2 /vol/vfiler2_vol0 vfilert /vol/vfiler_vol vfilert /vol/virt_vol where node1 = $filer. however how... (1 Reply)
Discussion started by: riegersteve
1 Replies

7. Shell Programming and Scripting

how to print backslah using echo

how to print 3 backslah using Unix AIX, i have try in my fedora and AIX,both give dirrent output for same echo statment test.sh sed -e 's/\(\)/\\\1/g' -e 's/\$?/\\$?/g' -e 's/\$#/\\$#/g' -e 's/^/echo "/g' -e 's/$/ "/g' xMNBDF070 > xMNBDF070_test xMNBDF070 sed -e "s/'/\\\'/g" -e... (8 Replies)
Discussion started by: mani_um
8 Replies

8. Shell Programming and Scripting

Differenc between print and echo

can anyone explain me what is the difference between echo and print in shell programming? (3 Replies)
Discussion started by: chandhar
3 Replies

9. UNIX for Dummies Questions & Answers

echo vs. print

Is there a functional difference between echo and print? (1 Reply)
Discussion started by: PhilW
1 Replies

10. UNIX for Dummies Questions & Answers

echo or print to screen and file

I did a search for this topic but I couldn't find it and I was sure I have seen something similar before (hard because I am not sure of the criteria for the keywords) What I was looking for was to be able to echo a message to the screen from a bash.sh script at the same time logging it to a... (2 Replies)
Discussion started by: Shakey21
2 Replies
Login or Register to Ask a Question