Hot answers tagged

78 votes
Accepted

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

You asked for using some syntax with the echo command: echo $'first line\nsecond line\nthirdline' > foo (But consider also the other answer you got.) The $'...' construct expands embedded ANSI ...
user avatar
  • 13.3k
63 votes

How to add new lines when using echo

Pass the -e flag to echo to make it parse escaped characters such as "\r\n", like so: echo -e "Line 1\r\nLine2" >> readme.txt
user avatar
60 votes
Accepted

How to add new lines when using echo

echo An echo implementation which strictly conforms to the Single Unix Specification will add newlines if you do: echo 'line1\nline2' But that is not a reliable behavior. In fact, there really isn'...
user avatar
  • 55.7k
59 votes
Accepted

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

type tells you what the shell would use. For example: $ type echo echo is a shell builtin $ type /bin/echo /bin/echo is /bin/echo That means that if, at the bash prompt, you type echo, you will get ...
user avatar
  • 69.5k
56 votes

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

What echo does with character escapes is implementation defined. In many implementations of echo (including most modern ones), the string passed is not examined for escapes at all by default. With ...
user avatar
  • 114k
46 votes

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

cat is hashed (/bin/cat) is just like cat is /bin/cat (that is, it's an external program). The difference is that you already ran cat in this session, so bash has already looked it up in $PATH and ...
user avatar
43 votes
Accepted

Why is the behavior of `command 1>file.txt 2>file.txt` different from `command 1>file.txt 2>&1`?

You need to know two things: An open file descriptor known to the application-mode side of a process references an internal kernel object known as a file description, which is an instance of an open ...
user avatar
  • 62.3k
41 votes
Accepted

Is it always safe to use `eval echo`?

Counterexample: DANGEROUS=">foo" eval echo $DANGEROUS The arbitrary arguments to echo could have done something more nefarious than creating a file called "foo".
user avatar
  • 41.1k
40 votes
Accepted

Using `printf` to print variable containing `%` percent sign results in "bash: printf: `p': invalid format character"

Use printf in its normal form: printf '%s\n' "${TEST}" From man printf: SYNOPSIS printf FORMAT [ARGUMENT]... You should never pass a variable to the FORMAT string as it may lead to errors and ...
user avatar
  • 20.7k
40 votes
Accepted

How do you output a multi-line string that includes slashes and other special characters?

In this case, I'd use cat with a (quoted) here-document: cat <<'END_CAT' /\_/\ ( o.o ) > ^ < END_CAT This is the best way of ensuring the ASCII art is outputted the way it is intended ...
user avatar
  • 12.9k
37 votes
Accepted

why 'echo --help' doesn't give me help page of echo?

man echo relates to the echo program. GNU echo supports a --help option, as do some others. When you run echo in Bash you instead get its builtin echo which doesn't. To access the echo program, ...
user avatar
37 votes

append text with echo without new line

Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e.g. echo -n "some text here" >> file.txt ...
user avatar
  • 805
35 votes
Accepted

echo [9876543210] displays 1 4 5 6 ... why?

The open bracket [ is a special character to the shell; it opens up a pattern matching algorithm that says "match any of the characters inside the brackets". Because you have 4 files named as: 1, 4, 5,...
user avatar
  • 63.3k
35 votes
Accepted

Encode file content and echo it as one line

Use the -w option (line wrapping) of base64 like this: ... | base64 -w 0 A value of 0 will disable line wrapping.
user avatar
32 votes
Accepted

Carriage return with echo command

The \r is just that, a "carriage return" - nothing else. This means what is after the \r will overwrite the beginning of what has already been printed. For example: echo -e "1234\r56" Will display: ...
user avatar
  • 3,114
29 votes
Accepted

What is the format for piping a message into sendmail?

Your echo statement should really output newlines not the sequence \ followed by n. You can do that by providing the -e option: echo -e "Subject:Hello \n\n I would like to buy a hamburger\n" | ...
user avatar
  • 75.3k
27 votes

Is it always safe to use `eval echo`?

@Celada has provided an excellent answer. To demonstrate eval is really evil, here's something more nefarious than creating a file called "foo": DANGEROUS='$(rm foo)' eval echo "$DANGEROUS" And of ...
user avatar
  • 3,814
27 votes

Is it dangerous to run echo without quotes?

Just an extra note on top of @Kusalananda's fine answer. echo run after_bundle is fine because none of the characters in those 3 arguments¹ passed to echo contain characters that are special to the ...
user avatar
26 votes
Accepted

How do I print the nth argument of a script, when n isn't known until runtime?

By chronological order, in various shells: csh (late 70s): $argv[$n] (also works in zsh since 1991 and fish since 2005) zsh (1991): $argv[n] / $@[n] / $*[n] (the last too also supported by yash but ...
user avatar
25 votes
Accepted

Why there is such a difference in execution time of echo and cat?

There are several things to consider here. i=`cat input` can be expensive and there's a lot of variations between shells. That's a feature called command substitution. The idea is to store the ...
user avatar
25 votes
Accepted

Why is echo -e behaving weird in a Makefile?

UNIX compliant implementations of echo are required to output -e<space>hello<newline>world<newline> there. Those that don't are not compliant. Many aren't which means it's almost ...
user avatar
24 votes
Accepted

Why does "wc -c" print a value of one more with echo?

echo print newline (\n) at end of line echo abcd | xxd 0000000: 6162 6364 0a abcd. With some echo implementations, you can use -n : -n do not output the trailing newline and test: echo ...
user avatar
  • 2,874
24 votes

How do I print '-e' with echo?

The best solution is not to use echo, but to use printf instead. printf '%s\n' -e This works with arbitrary variables: var=-e printf '%s\n' "$var" ...meaning you don't need to do any ...
user avatar
23 votes
Accepted

Is it possible to print the content of the content of a variable with shell script? (indirect referencing)

You can accomplish this using bash's indirect variable expansion (as long as it's okay for you to leave out the $ from your reference variable): $ var=test $ test="my string" $ echo "$var" test $ ...
user avatar
  • 33.2k
22 votes

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

You're doing it correctly, but the bash syntax is easy to misinterpret: you could think that echo $TEST causes echo to fetch TEST env var then print it, it does not. So given export TEST=123 then ...
user avatar
  • 381
22 votes

append text with echo without new line

echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt. Note that it is ...
user avatar
22 votes
Accepted

Only print output after finding pattern

AWK can do this with pattern ranges, which allows the use of any regular expression: echoer | awk '/pattern/,0' will print echoer’s output starting with the first line matching pattern. AWK is ...
user avatar
22 votes
Accepted

Echoing a tail command produces unexpected output?

Your crontab line has one or more asterisks * in it, indicating "any time". When that line is substituted in from the command substitution, the result is something like echo * * * * * cmd > /path/...
user avatar
21 votes
Accepted

Why is echo matching some files twice?

Braces are not wildcard patterns. Check the documentation of bash expansions: braces are expanded very early in the process, and wildcard patterns are expanded in the last step¹. Initially, the ...
user avatar
21 votes
Accepted

Why doesn't echo called as /bin/sh -c echo foo output anything?

From man sh -c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional ...
user avatar
  • 6,798

Only top scored, non community-wiki answers of a minimum length are eligible