echoPrint text and variables (and its quoting traps)
echo prints its arguments separated by spaces, followed by a newline. You use it to show variables, write lines into files and debug scripts, but its output depends on quoting and on which echo runs, so printf is the safer choice for anything exact.
echo [-n] [-e | -E] [STRING]...Print a variable, a literal dollar sign and preserved spacing correctly in a deploy script.
echoIn bash it's a builtin (no new process); /usr/bin/echo also exists and can behave differently.
Before echo ever runs, the shell has already done the interesting work: it expands variables ($APP), command substitutions ($(date)), arithmetic ($((6*7))) and globs (*.log), then splits unquoted results into words on spaces, tabs and newlines. echo receives the final argument list and simply writes them joined by single spaces plus a newline to stdout with write(). That's why echo $x collapses repeated spaces and turns a multi-line value into one line, while echo "$x" keeps it exactly. An unquoted glob that matches nothing is passed through as is (echo *.nothing prints *.nothing). In bash, echo is a builtin. Its options are -n (no trailing newline), -e (interpret \n, \t, \e...) and -E, and it has no -- end-of-options, so you can't easily print the string '-n'. Under sh/dash, echo always interprets backslashes, which makes the same script behave differently. printf '%s\n' is consistent everywhere.
-nDon't print the trailing newline-eInterpret backslash escapes: \n \t \e \\-EDon't interpret escapes (bash default)"$VAR"Double quotes: expand variables, keep spaces and newlines'$VAR'Single quotes: print literally, no expansion> / >>Write to a file: overwrite / appendprintf '%s\n' ARGSPortable, exact alternativesudo echo 'x' > /etc/some.confThe redirect is done by your unprivileged shell before sudo starts, so you get 'Permission denied'.
echo 'x' | sudo tee /etc/some.conf > /dev/nullecho $var in scriptsUnquoted, the value is word-split and globbed: spaces collapse, * expands to file names, an empty value disappears.
echo "$var" # or printf '%s\n' "$var"echo "line1\nline2" expecting two linesbash's echo doesn't interpret \n without -e (but sh's does), so output differs by shell.
printf 'line1\nline2\n'echo "$password" | some-cmd in a script run with set -xset -x traces the expanded command, printing the secret into CI logs.
Turn off tracing around secrets (set +x) or pass them via filesTry these on a Linux machine of your own. Each task has a hint and a solution.
Print your user name and home directory in one sentence, then print the same text literally with the dollar signs.
Append a timestamped line 'practice run' to ~/logs/deploy.log and show the last line.
Add the line '# managed by devops' to /etc/app/app.conf, which is owned by root.
Show that unquoted echo collapses spaces: store 'a b' in a variable and echo it both ways.
echoPrint text and variables (and its quoting traps)
echo prints its arguments separated by spaces, followed by a newline. You use it to show variables, write lines into files and debug scripts, but its output depends on quoting and on which echo runs, so printf is the safer choice for anything exact.
echo [-n] [-e | -E] [STRING]...Print a variable, a literal dollar sign and preserved spacing correctly in a deploy script.
echoIn bash it's a builtin (no new process); /usr/bin/echo also exists and can behave differently.
Before echo ever runs, the shell has already done the interesting work: it expands variables ($APP), command substitutions ($(date)), arithmetic ($((6*7))) and globs (*.log), then splits unquoted results into words on spaces, tabs and newlines. echo receives the final argument list and simply writes them joined by single spaces plus a newline to stdout with write(). That's why echo $x collapses repeated spaces and turns a multi-line value into one line, while echo "$x" keeps it exactly. An unquoted glob that matches nothing is passed through as is (echo *.nothing prints *.nothing). In bash, echo is a builtin. Its options are -n (no trailing newline), -e (interpret \n, \t, \e...) and -E, and it has no -- end-of-options, so you can't easily print the string '-n'. Under sh/dash, echo always interprets backslashes, which makes the same script behave differently. printf '%s\n' is consistent everywhere.
-nDon't print the trailing newline-eInterpret backslash escapes: \n \t \e \\-EDon't interpret escapes (bash default)"$VAR"Double quotes: expand variables, keep spaces and newlines'$VAR'Single quotes: print literally, no expansion> / >>Write to a file: overwrite / appendprintf '%s\n' ARGSPortable, exact alternativesudo echo 'x' > /etc/some.confThe redirect is done by your unprivileged shell before sudo starts, so you get 'Permission denied'.
echo 'x' | sudo tee /etc/some.conf > /dev/nullecho $var in scriptsUnquoted, the value is word-split and globbed: spaces collapse, * expands to file names, an empty value disappears.
echo "$var" # or printf '%s\n' "$var"echo "line1\nline2" expecting two linesbash's echo doesn't interpret \n without -e (but sh's does), so output differs by shell.
printf 'line1\nline2\n'echo "$password" | some-cmd in a script run with set -xset -x traces the expanded command, printing the secret into CI logs.
Turn off tracing around secrets (set +x) or pass them via filesTry these on a Linux machine of your own. Each task has a hint and a solution.
Print your user name and home directory in one sentence, then print the same text literally with the dollar signs.
Append a timestamped line 'practice run' to ~/logs/deploy.log and show the last line.
Add the line '# managed by devops' to /etc/app/app.conf, which is owned by root.
Show that unquoted echo collapses spaces: store 'a b' in a variable and echo it both ways.