This tutorial explains Linux “echo” command, options and its usage with examples.
The echo command writes character strings to standard output. Strings are separated by spaces, and a new-line character follows the last String parameter specified. If no String parameter is specified, a blank line (new-line character) is displayed.
echo [OPTIONS] [ String … ]
On BSD and some variants derived from BSD does not begin a new line after the echoed text.
on some variants this option is necessary to enable recognition of escape characters in string
The echo command recognizes the following escape conventions:
Displays an alert character.
Displays a backspace character.
Suppresses the new-line character that otherwise follows the final argument in the output. All characters following the \c sequence are ignored.
Displays a form-feed character.
Displays a new-line character.
Displays a carriage return character.
Displays a tab character.
Displays a vertical tab character.
Displays a backslash character.
Displays an 8-bit character whose ASCII value is a 0-, 1-, 2-, or 3-digit octal number.
1. To write a message to standard output, enter:
$ echo Hi there Hi there
2. To display a message containing special characters, enter:
$ echo -e "I'm at lunch.\nI'll be back at 1:00." I'm at lunch. I'll be back at 1:00.
Note: You must put the message in quotation marks if it contains escape sequences. Otherwise, the shell interprets the \ (backslash) as a metacharacter and treats the \ differently.
$ echo * Desktop Documents ls-output.txt Music Pictures Public Templates Videos
The above example would list all the files and directories in the current directory.
4. To add a single line of text to a file, enter:
$ echo Remember to set the shell search path to $PATH. >>notes
This usage adds the message to the end of the file notes after the shell substitutes the value of the PATH shell variable.
5. Arithmetic Expansion
$ echo $((2 + 2)) 4
Arithmetic expansion uses the form:
where expression is an arithmetic expression consisting of values and arithmetic operators.
Arithmetic expansion only supports integers (whole numbers, no decimals), but can perform quite a number of different operations.
6. Command Substitution
Command substitution allows us to use the output of a command as an expansion:
$ echo $(cal) December 2013 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $ echo "$(cal)" December 2013 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Note : We should take a moment to look at the effect of double quotes on command substitution. By default, word-splitting looks for the presence of spaces, tabs, and newlines (linefeed characters) and treats them as delimiters between words. This means that unquoted spaces, tabs, and newlines are not considered to be part of the text. They only serve as separators. Since they separate the words into different arguments, our example command line contains a command followed by four distinct arguments. If we add double quotes word-splitting is suppressed and the embedded spaces are not treated as delimiters, rather they become part of the argument. Once the double quotes are added, our command line contains a command followed by a single argument. The fact that newlines are considered delimiters by the word-splitting mechanism causes an interesting, albeit subtle, effect on command substitution.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Apply for Programming Internship
- Buy Information Technology Books
- Buy Linux Books
- Practice Programming MCQs
- Apply for Linux Internship