CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Environment & Shell / echo

echo

Print text and variables (and its quoting traps)

Beginneressentialshellscripting

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.

Syntaxecho [-n] [-e | -E] [STRING]...

Watch it run

Print a variable, a literal dollar sign and preserved spacing correctly in a deploy script.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
echo "Hello $USER"; echo 'Hello $USER'

Double quotes expand variables; single quotes print everything literally.

Anatomy of a command

$
commandecho

In bash it's a builtin (no new process); /usr/bin/echo also exists and can behave differently.

How it works

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.

Options you'll actually use

-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 / append
printf '%s\n' ARGSPortable, exact alternative

Common mistakes

sudo echo 'x' > /etc/some.conf

The redirect is done by your unprivileged shell before sudo starts, so you get 'Permission denied'.

echo 'x' | sudo tee /etc/some.conf > /dev/null
echo $var in scripts

Unquoted, 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 lines

bash'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 -x

set -x traces the expanded command, printing the secret into CI logs.

Turn off tracing around secrets (set +x) or pass them via files

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print your user name and home directory in one sentence, then print the same text literally with the dollar signs.

  2. 2

    Append a timestamped line 'practice run' to ~/logs/deploy.log and show the last line.

  3. 3

    Add the line '# managed by devops' to /etc/app/app.conf, which is owned by root.

  4. 4

    Show that unquoted echo collapses spaces: store 'a b' in a variable and echo it both ways.

Related commands

teeCopy a stream to the screen and to files at oncebash: variablesShell variables: assign, quote, default and exportenvShow the environment, or run a command with a changed one
PrevioussourceNextset
Linux / Environment & Shell / echo

echo

Print text and variables (and its quoting traps)

Beginneressentialshellscripting

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.

Syntaxecho [-n] [-e | -E] [STRING]...

Watch it run

Print a variable, a literal dollar sign and preserved spacing correctly in a deploy script.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
echo "Hello $USER"; echo 'Hello $USER'

Double quotes expand variables; single quotes print everything literally.

Anatomy of a command

$
commandecho

In bash it's a builtin (no new process); /usr/bin/echo also exists and can behave differently.

How it works

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.

Options you'll actually use

-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 / append
printf '%s\n' ARGSPortable, exact alternative

Common mistakes

sudo echo 'x' > /etc/some.conf

The redirect is done by your unprivileged shell before sudo starts, so you get 'Permission denied'.

echo 'x' | sudo tee /etc/some.conf > /dev/null
echo $var in scripts

Unquoted, 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 lines

bash'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 -x

set -x traces the expanded command, printing the secret into CI logs.

Turn off tracing around secrets (set +x) or pass them via files

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print your user name and home directory in one sentence, then print the same text literally with the dollar signs.

  2. 2

    Append a timestamped line 'practice run' to ~/logs/deploy.log and show the last line.

  3. 3

    Add the line '# managed by devops' to /etc/app/app.conf, which is owned by root.

  4. 4

    Show that unquoted echo collapses spaces: store 'a b' in a variable and echo it both ways.

Related commands

teeCopy a stream to the screen and to files at oncebash: variablesShell variables: assign, quote, default and exportenvShow the environment, or run a command with a changed one
PrevioussourceNextset