echo command tutorial in linux/unix with examples and use cases

linux echo command – display a line of text.

Echo command is one of the most commonly used commands and is commonly used in scripting languages ​​to display a line of text/string on a standard output or file.

syntax

echo [SHORT-OPTION]... [STRING]...

options

  • -n
    do not output the trailing newline
  • -e
    enable interpretation of backslash escapes
  • -E
    disable interpretation of backslash escapes (default)
  • –help
    display this help and exit
  • –version
    output version information and exit

If –e is in effect, the following sequences are recognized:

   \\     backslash

   \a     alert (BEL)

   \b     backspace

   \c     produce no further output

   \e     escape

   \f     form feed

   \n     new line

   \r     carriage return

   \t     horizontal tab

   \v     vertical tab

   \0NNN  byte with octal value NNN (1 to 3 digits)

   \xHH   byte with hexadecimal value HH (1 to 2 digits)

examples

empty file

In the following example, we can use the echo command to create an empty file. Of course, you can also use the touch command to create an empty file.

➜  ~ echo > test.txt  
OR
➜  ~ echo ""> test.txt 

echo newline or Multi-line, using optino “-e” and “\n”

➜  ~ echo -e "hello! \nylspirit"

append file content using echo

In the following example, we can append content to a file using the echo command and “>>” concatenation.

➜  ~ echo "ylspirit" >> test.txt 

echo content to file

➜  ~ echo "hahaha, hello, ylspirit" > test.txt 

print the variable value using echo command – echo $

➜  ~ x=100
➜  ~ echo $x
100
➜  ~

print all the files/folder using echo command

➜ echo *

print files of a specific kind using echo command

➜ echo *.txt

Add a Comment

Your email address will not be published.