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:

504 questions
692
votes
4 answers

Why is printf better than echo?

I have heard that printf is better than echo. I can recall only one instance from my experience where I had to use printf because echo didn't work for feeding some text into some program on RHEL 5.8 but printf did. But apparently, there are other…
amphibient
  • 12,222
  • 18
  • 62
  • 87
363
votes
5 answers

How do I set an environment variable on the command line and have it appear in commands?

If I run export TEST=foo echo $TEST It outputs foo. If I run TEST=foo echo $TEST It does not. How can I get this functionality without using export or a script?
ashleysmithgpu
  • 4,287
  • 3
  • 21
  • 19
122
votes
3 answers

Why is "echo" so much faster than "touch"?

I'm trying to update the timestamp to the current time on all of the xml files in my directory (recursively). I'm using Mac OSX 10.8.5. On about 300,000 files, the following echo command takes 10 seconds: for file in `find . -name "*.xml"`; do echo…
Casey Patton
88
votes
6 answers

Redirecting the content of a file to the command "echo"

I have a file named my_file.txt whose content is just the string Hello. How could I redirect its content to the command echo? I know I have the commands less, cat, more... but I need to do it with echo. I tried this: $ cat my_file.txt | echo and…
danielmbcn
  • 1,075
  • 1
  • 8
  • 7
86
votes
8 answers

How to delete line with echo?

I know that I could delete the last three chars with: echo -ne '\b\b\b' But how can I delete a full line? I mean I don't want to use: echo -ne…
LanceBaynes
  • 39,295
  • 97
  • 250
  • 349
86
votes
5 answers

How to put a newline special character into a file using the echo command and redirection operator?

I would like to create a file by using the echo command and the redirection operator, the file should be made of a few lines. I tried to include a newline by "\n" inside the string: echo "first line\nsecond line\nthirdline\n" > foo but this way no…
Abdul Al Hazred
  • 25,760
  • 23
  • 64
  • 88
77
votes
4 answers

echo bytes to a file

I'm trying to connect my rasberry Pi to some display using the i2c bus. To get started I wanted to manually write stuff, bytes in particular to a file. How do you write specific bytes to a file? I already read that one and I figured my problem…
Mark
  • 1,149
  • 3
  • 9
  • 18
76
votes
9 answers

How to add new lines when using echo

Why does the following command not insert new lines in the generated file and what's the solution? $ echo "Line 1\r\nLine2" >> readme.txt $ cat readme.txt Line 1\r\nLine2
Dumbo
  • 1,516
  • 6
  • 16
  • 20
75
votes
3 answers

What defines the maximum size for a command single argument?

I was under the impression that the maximum length of a single argument was not the problem here so much as the total size of the overall argument array plus the size of the environment, which is limited to ARG_MAX. Thus I thought that something…
Graeme
  • 33,607
  • 8
  • 85
  • 110
67
votes
6 answers

How to echo a bang!

I tried to create a script by echo'ing the contents into a file, instead of opening it with a editor echo -e "#!/bin/bash \n /usr/bin/command args" > .scripts/command The output: bash: !/bin/bash: event not found I've isolated this strange…
Stefan
  • 24,830
  • 40
  • 98
  • 126
42
votes
6 answers

append text with echo without new line

I want to append text to file like echo "abc" >>file.txt. But this add abc after new line How can I add abc in the end of file with echo without new line?
choijj
  • 421
  • 1
  • 4
  • 3
35
votes
3 answers

Encode file content and echo it as one line

I am trying to echo the content of key and certificate files encoded with base64 so that I can then copy the output into other places. I found this thread: Redirecting the content of a file to the command echo? which shows how to echo the file…
Kajsa
  • 453
  • 1
  • 4
  • 5
33
votes
6 answers

Is 'cat' a shell built-in or an external program?

When I use the type command to find out if cat is a shell built-in or an external program I get the output below: -$ type cat cat is hashed (/bin/cat) -$ Does this mean that cat is an external program which is /bin/cat? I got confused, because when…
sps
  • 1,396
  • 5
  • 15
  • 23
32
votes
1 answer

Append same text to many files using cat or echo?

How can I write the same content to many text files by using cat or echo in only one command? For example I want to write "hello" to file1 and file2. I tried: echo "hello" >> file1 file2 but it didn't work. How can I do it?
neo0
  • 755
  • 2
  • 7
  • 7
31
votes
4 answers

What is the difference between echo `date`, echo "`date`", and echo '`date`'?

What is the difference between these three commands? echo `date` echo "`date`" echo '`date`' I am confused on what the differences actually are. I think that when the ' are around it means that it is a string, therefore echo would literally output…
John
  • 3,459
  • 13
  • 29
  • 25
1
2 3
33 34