APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2003/12/22 echo


© December 2003 Tony Lawrence

What could be more simple?

bash-2.05a$ echo foo
foo
 

Well, actually it does get more complicated. The first problem is the interpretation of escape characters. System V Unixes interpret these by default, like this:

bash-2.05a$ echo "b\bcook"
cook
 

Linux and BSD systems do not unless you use -e:

bash-2.05a$ echo  "b\bcook"
b\bcook
bash-2.05a$ echo -e "b\bcook"
cook
 

(echo is often a shell built in, but that doesn't affect this discussion for most shells).

Many System V shell scripts will use \c to suppress a new line:

apl$ echo "hello \c"; echo "there"
hello there
 

That won't work on Linux or BSD unless you "echo -e" or discard the \c and use echo -n instead.

The man page for Linux and BSD echo includes the apparently useless -E flag, which disables interpretation of escape characters. As that is the default, what is it needed for? Well, although few sites do this, you CAN compile echo with an option that reverses the default, and (with bash) you can use "shopt" to set xpg_echo (shopt -s xpg_echo):

bash-2.05a$ shopt -s xpg_echo
bash-2.05a$ echo  "b\bcook"
cook
bash-2.05a$ /bin/echo  "b\bcook"
b\bcook
bash-2.05a$ echo  -E "b\bcook"
b\bcook
 

Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> echo


Inexpensive and informative Apple related e-books:

Take control of Apple TV, Second Edition

Take Control of iCloud, Fifth Edition

Take Control of Upgrading to El Capitan

iOS 10: A Take Control Crash Course

Digital Sharing Crash Course




More Articles by © Tony Lawrence


"That won't work on Linux or BSD unless you "echo -e" or discard the \c and use echo -n instead."

The -n option is no longer considered to be POSIX-compliant, so it would appear that the version of echo shipped with Linux and BSD is obsolete.

--BigDumbDinosaur



Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





I think there is a world market for maybe five computers. (Thomas Watson, Chairman of IBM, 1943)




Linux posts

Troubleshooting posts


This post tagged:

Scripting

Shell

UnixWords



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode