Skip to content

The echo command in Linux

Echo Command Cover

Echo is a shell builtin that prints out its arguments to the standard output. The echo command is most commonly used in shell scripts and batch files in order to output some textual data to the user. Although the description of echo seems pretty straightforward, it is an extremely powerful command.

$ echo Hello Shell!
Hello Shell!

This seems easy, right? We sent the command some arguments and they got displayed. Now let us try another variation.

$ echo *
Desktop Documents Downloads Music Pictures Public Templates Videos

Why didn’t our echo print out ‘*’? Why did it instead print out the list of folders? Because the ‘*’ character is a wildcard character that matches any character in a filename. The shell (behind the curtains) expands the ‘*’ character into the names of all the files/directories in our current working directory, and it automatically transforms the character to the list of the results. Basically, the echo command never sees the ‘*’ character, but only the result of the transformation.

$ echo D* 
Desktop Documents Downloads

$ echo *s
Documents Downloads Pictures Templates Videos

$ echo D[ae]* 
Desktop

We call this mechanism pathname expansion. Above we see an example of this. The first command echoes anything that starts with the character ‘D’, and the second echoes anything that ends with the character ‘s’. The third example uses some regex magic to echo anything that starts with the letter ‘D’ and has either ‘a’ or ‘e’ as the subsequent character.

Tilde Expansion

The tilde (~) character has a special meaning. It is the home directory of the current user (or any other possible user).

$ echo ~    
/home/mehmed

$ echo ~selma
/home/selma

Arithmetic calculations

We can use the shell as a calculator. The shell allows arithmetic to be performed, by using the form $((expression)) where expression should be a valid mathematical expression. Arithmetic expansion (the shell being able to perform arithmetic) supports only integers but can perform a number of different operation such as addition (+), subtraction (-), multiplication (*), division (/, keep in mind results are integers only), modulo/remainder (%), exponentiation (**). Some examples can be seen below (we can even nest one expression inside the other).

$ echo $((3 * 12))
36

$ echo $((5 * $((2 + 2))))
20

Brace Expansion

We can use the brace expansion to create multiple text strings from an occurring pattern (for example, to create a list of files or directories). We could either use a comma-separated list of strings, or a range of integers or single characters. In the listing below we can see some uses of the brace expansion pattern.

$ echo 18/06/20{19,20,21}.txt
18/06/2019.txt 18/06/2020.txt 18/06/2021.txt

$ echo March_Report-{1..10}
March_Report-1 March_Report-2 March_Report-3 March_Report-4 March_Report-5 March_Report-6 March_Report-7 March_Report-8 March_Report-9 March_Report-10

$ echo '1. What is the answer?' '('{A..D}')'
1. What is the answer? (A) (B) (C) (D)

Keep in mind that the echo command by default removes any extra whitespace, thus we would need to use some form of quoting (either single quotes or double quotes).

Single vs. Double Quotes?

By using double quotes "", we would effectively remove the special meaning behind the build in characters. They are treated as ordinary letters inside the double-quotes. Using double quotes we can carry out parameter expansion, arithmetic expansion, and command substitution, and we can deal with spaces inside file names.

$ ls -l space file.txt 
ls: cannot access 'space': No such file or directory
ls: cannot access 'file.txt': No such file or directory

$ ls -l "space file.txt"
-rw-r--r-- 1 root root 0 Jun 10 16:58 'space file.txt'

On the other hand, single quotes' ' suppress all expansions – including word-splitting, pathname expansion, and brace expansion. Below you can see a nice explanation of the differences between single and double quotes inside the shell script.

$ year=2021

$ echo "$year"
2021

$ echo '$year'
$year

Additional Links

You can read a more detailed explanation inside the documentation of the echo command builtin here (or fire up the manual inside your shell).

Hope you learned something interesting, please read some more Linux articles by clicking down below.

1 thought on “The echo command in Linux”

  1. Pingback: How to escape characters in the command line? | The Dukh Chronicles

Comments are closed.