Questions tagged [echo]

Questions about either the shell builtin or /bin/echo. This tag should not be used if your question is simply about printing to the terminal, only apply it if your question is specific to the echo command.

echo prints its operands connected by space to stdout with a trailing new line, for example, echo hello world ..'OVERFLOW!' gives hello world ..OVERFLOW!.

The echo command — either program or builtin — may be one of the commands that new shell users are most familiar with, and may also be one of the commands that get built into most non-POSIX shells with similiar functionality (e.g. cmd, csh), and even got its name into other types of languages like PHP.

However, echo has its limitations. As in POSIX, echo generates undefined results when the first operand is -n, or any of the operands contains backslashes; while SUSv2 XCU explicitly defines -n to be treated as a string and backslashes to be escaped in a C-like way. There are also extra options extended by some implementations including GNU printf and bash, like -e to enable backslash interpretation and -E to explicitly disable it (default). With all the options, you should have already realized the consequences of echo "$var".

POSIX also makes this point clear:

It is not possible to use echo portably across all POSIX systems unless both -n (as the first argument) and escape sequences are omitted. [...]

New applications are encouraged to use printf instead of echo.

echo's printf replacement has been described in POSIX already, with examples for different echo variants. The following is an example with support for -e/-E/-n flags:

# Variant of http://www.etalabs.net/sh_tricks.html, mimics GNU & bash echo.
# with the addition that one can use echo - "$var" to output the content
# of $var verbatim (followed by a newline) like in zsh (but echo - still
# outputs - unlike in zsh).

echo() (
  fmt=%s end='\n' IFS=' '
  while [ $# -gt 1 ] ; do
    case "$1" in ([!-]*|-*[!neE]*) break;; esac # not a flag
    case "$1" in (*n*) end='';; esac # no newline
    case "$1" in (*e*) fmt=%b;; esac # interpret backslash escapes
    case "$1" in (*E*) fmt=%s;; esac # don't interpret backslashes
    shift
  done
  printf "$fmt$end" "$*"
)

The %b directive in printf works exactly as echo -e does: it only interprets \0ooo.

Read more about this:

511 questions
4
votes
1 answer

Why doesn't text piped into an echo get outputted?

Why doesn't this command output "1"? echo 1 | echo I imagine it working this way: 1. echo 1 (outputs 1) 2. | echo (takes the 1 as an input, then echos it) Isn't this what should happen?
Jeff
3
votes
2 answers

I am trying to send stdout message to mailx -- Red Hat

All, thanks for your help... This should be easy: My script prints this to stdout/terminal perfectly... its pretty in the script but not below... ???? : # Print to stdout echo "------- CAPACITY TEST FAILED -------" echo -n " SYSTEM NAME: " ; uname…
SSDdude
  • 171
  • 2
  • 12
2
votes
2 answers

How to append multiple lines before the last line using echo command

{ This is test1 this is test2 this is test3 } Now i want append multiple lines before the last line using echo command Anyone please help me!!!! My output is look like below { This is test1 this is test2 this is test3 …
2
votes
1 answer

How does echo parse $100?

I'm learning echo command: $ echo $100 00 $ echo $1 $ so why does echo parse $1 as undefined variable and print following zeros but not $100 as a whole? I'm using Ubuntu 14.04.
mzoz
  • 155
2
votes
1 answer

What does mean echo -e \e - how escape?

What does this mean? echo -e \e
1
vote
0 answers

How to export a variable and log the details to the console at the same time?

How to export a variable and log the details at the same time? For example, how to do the following in a one liner? export myvar=$var1$var2$var3 echo $myvar
phanin
  • 119
1
vote
5 answers

echo {0..9} without space

There is a space in between $ echo {0..9} 0 1 2 3 4 5 6 7 8 9 How do I produce similar output using echo {0..9} without space? Desired output 0123456789
Wolf
  • 1,631
1
vote
1 answer

echo -e "word\b" not working as expected

According to man: If -e is in effect, the following sequences are recognized: \b backspace So I would expect echo -e "word\b" to produce wor output. But yet: $ echo -e "wor\bd" wod $ echo -e "word\b" word What's the reason behind this?
SantaXL
  • 365
1
vote
3 answers

How to get a opposite background by echo

If I use mode 0,we can set the font color and background color,shuc as echo -e "\e[0;31;47m teststring \e[0m" But I hope to use a opposite color to be a background color(I think that is cyan).So I want to use the mode 7(mode 7 will get a opposite…
yode
  • 1,047
1
vote
2 answers

How would I loop this command?

A snippet of my code is shown below. The script currently closes if its not able to find the path. [echo "could not find $REPLY, ensure the path is correct and try again"] Instead I would like it at this point to loop back and try to accept input…
HS'
  • 101
  • 1
  • 1
  • 3
0
votes
0 answers

What is the difference between three `echo` commands?

What are the differences between the three following echo commands? var='You' echo "Hello\nThere\n${var}" Output: Hello\nThere\nYou echo 'Hello\nThere\n${var}' Output: Hello\nThere\n${var} echo…
Fred
  • 335
0
votes
3 answers

Echo HTML Into Text File

Is there a simple way to echo some HTML code into a text/HTML file? I'm trying to do: echo "\n\n\t\n\t\t

Hello World!

\n\t\n" > index.html But get: -bash: !DOCTYPE: event not found
GTS Joe
  • 625
0
votes
2 answers

Trying to echo a string will multiple spaces to a terminal but need one string as-is

trying to do the following: echo "- - 830 "FTL MFG" -" the "FTL MFG" should be left alone as one entry. what am I doing wrong?
0
votes
1 answer

Echo to file clashes with mailutils

After installing ssmtp and mailutils, writing to a file with 'echo' tries to send me an email to username@hostname. For example: Sending mail with mailutils: echo "Body text here." | mail -s "Subject text here." sendto@email.com and I use: echo "log…
0
votes
2 answers

How do I include commands in an echo statement?

Pretty simple, but I didn't find a question on here that asked this exactly. I thought this would work but it does not. echo "Your disk usage is ${df -k -h}." How do I make the above work?
1
2