Linux Commands – echo
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed cp command in Linux which is used to copy files and directories in Linux.
https://cloudaffaire.com/linux-commands-cp/
In this blog post, we will discuss echo command in Linux. the echo command is used to print each given STRING to standard output, with a space between each and a newline after the last one.
Linux Commands – echo
You can print in the standard output using echo command. The string that passed to echo can be without quotes, with single quotes or with double-quotes. Shell interprets the input string differently according to the string format passed.
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 |
########################### ## Linux Commands | echo ## ########################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##----- ## echo ##----- ## echo [option]... [string]... echo hello world ## returns hello world echo 'hello world' ## returns hello world echo "hello world" ## returns hello world name="debjeet" echo $name ## returns hello debjeet echo "$name" ## returns hello debjeet echo '$name' ## returns hello $name echo \$name ## returns $name echo \\$name ## returns \debjeet echo '\$name' ## returns \$name echo "\$name" ## returns $name echo "\\$name" ## returns \debjeet echo "\"$name\"" ## returns "debjeet" echo "'$name'" ## returns 'debjeet' |
By default, echo appends a newline after the output. But you can use echo -n option to suppress newline appending.
1 2 |
echo "hello world" ## returns hello world and a new line echo -n "hello world" ## returns hello world |
By default, echo does not escape the backslash escapes. But you can use echo -e option for interpretation of backslash escapes.
escape character | meaning |
\a | alert (bell) |
\b | backspace |
\c | suppress further output |
\e | escape character |
\f | form feed |
\n | new line |
\r | carriage return |
\t | horizontal tab |
\v | vertical tab |
\\ | backslash |
\0nnn | the character whose ASCII code is NNN (octal). NNN can be 0 to 3 octal digits |
\xHH | the eight-bit character whose value is HH (hexadecimal). HH can be one or two hex digits |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
echo "\thello" ## returns \thello echo -e "\thello" ## returns hello ## execute yourself and check the output echo -e "line one\nline two" echo -e "\vverticale_tab1\v\nverticale_tab2" echo -e "\thorizontal_tab1 \thorizontal_tab2" echo -e "\aalert" echo -e "\bbackspace" echo -e "hello\csupress further output" echo -e "\fform feed" echo -e "hello\rcarriage return" echo -e "\\ backslash" echo -e "\e[32mGreen" echo -e "\0043" echo -e "\x5E" echo -e "hello\n\tworld" |
Using echo -E option to explicitly suppress backslash-escape (echo default behavior).
1 2 3 4 |
echo "hello\n\tworld" ## returns hello\n\tworld echo -E "hello\n\tworld" ## returns hello\n\tworld echo -e "hello\n\tworld" ## returns hello ## world |
You can redirect the output of echo command to a file using > and >> redirection operators.
1 2 3 |
echo "hello world" > hello.txt ## redirects the output to hello.txt echo "welcome to cloudaffaire" >> hello.txt ## appends the output to hello.txt cat hello.txt |
You can perform command substitution in echo command.
1 2 3 4 |
echo "cat hello.txt" ## returns cat hello.txt echo "$(cat hello.txt)" ## returns hello.txt content echo `cat hello.txt` ## returns hello.txt content but new line charecters removed echo "`cat hello.txt`" ## returns hello.txt content |
You can perform parameter expansion in echo command.
1 2 3 4 |
echo {1..5} ## returns 1 2 3 4 5 echo {1..10..2} ## returns 1 3 5 7 9 echo {a..e} ## returns a b c d e echo {1,2}{a,b} ## returns 1a 1b 2a 2b |
You can perform parameter substitution in echo command.
1 2 3 |
user1=debjeet echo ${user1-`whoami`} ## returns debjeet echo ${user2-`whoami`} ## returns the output of whoami as user2 not set |
You can also perform an arithmetic operation in echo command.
1 2 3 4 |
echo $(( 10 + 5 )) ## returns 15 echo $(( 10 - 5 )) ## returns 5 echo $(( 10 / 5 )) ## returns 2 echo $(( 10 * 5 )) ## returns 50 |
You can perform regular expression in echo.
1 2 3 |
touch myfile.{sh,txt,tar,zip} echo * ## returns ls output echo *.txt ## returns hello.txt myfile.txt |
Hope you have enjoyed this article. In the next blog post, we will discuss printf command in Linux.