【Linux】命令 - echo

Posted by 西维蜀黍 on 2020-10-15, Last Modified on 2022-12-10

Usage

# Print a text message. Note: quotes are optional:
$ echo "Hello World"

# Print a message with environment variables:
$ echo "My path is $PATH"

# Append a message to the file:
$ echo "Hello World" >> file.txt

# Enable interpretation of backslash escapes (special characters):
$ echo -e "Column 1\tColumn 2"

常用转义字符

  • \b :转义后相当于按退格键(backspace) ,但前提是"\b"后面存在字符

  • \c :不换行输出,在"\c"后面不存在字符的情况下,作用相当于echo -n

  • \n:换行

    $ echo  "Hello World\n"
    Hello World
    
  • \f:换行,但是换行后的新行的开头位置连接着上一行的行尾

  • \v 与\f相同

  • \t:插入tab,即制表符

    $ echo  "Hello\tWorld"
    Hello	World
    
  • \r 光标移至行首,但不换行,相当于使用"\r"以后的字符覆盖"\r"之前同等长度的字符:

    $ echo  "Hello World\rWei"
    Weilo World
    
  • \\:表示插入""本身

    $ echo  "Hello\\World"
    Hello\World
    

-n - 输出结尾不换行

# Print a message without the trailing newline 
$ echo -n "Hello World"

Reference

  • man echo