echo 命令詳解

xie仗劍天涯發表於2017-02-28

echo命令用於在shell中列印shell變數的值,或者直接輸出指定的字串。

選項

-e:啟用轉義字元。 使用-e選項時,若字串中出現以下字元,則特別加以處理,而不會將它當成一般文字輸出: 
\a 發出警告聲; 
\b 刪除前一個字元; 
\c 最後不加上換行符號; 
\f 換行但游標仍舊停留在原來的位置; 
\n 換行且游標移至行首; 
\r 游標移至行首,但不換行; 
\t 插入tab; 
\v 與\f相同; 
\\ 插入\字元; 
\nnn 插入nnn(八進位制)所代表的ASCII字元;

  

輸出顏色

文字色 \e[1;31m 將顏色設定為紅色 \e[0m 將顏色重新置

顏色碼:重置=0,黑色=30,紅色=31,綠色=32,黃色=33,藍色=34,洋紅=35,青色=36,白色=37

eg:

[root@layzj022301 ~]# echo -e "\e[1;31mThis is red text\e[0m"
This is red text
[root@layzj022301 ~]# 
[root@layzj022301 ~]# echo -e "\e[1;32mThis is green text\e[0m" 
This is green text
[root@layzj022301 ~]# echo -e "\e[1;33mThis is yellow text\e[0m" 
This is yellow text
[root@layzj022301 ~]# echo -e "\e[1;34mThis is blue text\e[0m"  
This is blue text
[root@layzj022301 ~]# echo -e "\e[1;35mThis is carmine text\e[0m" 
This is carmine text
[root@layzj022301 ~]# echo -e "\e[1;36mThis is cyan text\e[0m" 
This is cyan text
[root@layzj022301 ~]# echo -e "\e[1;37mThis is white text\e[0m" 
This is white text
[root@layzj022301 ~]#

  

背景色

顏色碼:重置=0,黑色=40,紅色=41,綠色=42,黃色=43,藍色=44,洋紅=45,青色=46,白色=47

eg:

[root@layzj022301 ~]# echo -e "\e[1;47mThis is red text\e[0m" 
This is red text
[root@layzj022301 ~]# echo -e "\e[1;46mThis is green text\e[0m" 
This is green text
[root@layzj022301 ~]# echo -e "\e[1;45mThis is yellow text\e[0m" 
This is yellow text
[root@layzj022301 ~]# echo -e "\e[1;44mThis is blue text\e[0m" 
This is blue text
[root@layzj022301 ~]# echo -e "\e[1;43mThis is carmine text\e[0m" 
This is carmine text
[root@layzj022301 ~]# echo -e "\e[1;42mThis is cyan text\e[0m" 
This is cyan text
[root@layzj022301 ~]# echo -e "\e[1;41mThis is white text\e[0m" 
This is white text
[root@layzj022301 ~]#

  

文字閃動

[root@layzj022301 ~]# echo -e "\033[37;31;5mHello This Wenderful Shell~~~\033[39;49;0m" 
Hello This Wenderful Shell~~~
[root@layzj022301 ~]#

  

man echo

[root@layzj022301 ~]# man echo      
ECHO(1)                          User Commands                         ECHO(1)

NAME
       echo - display a line of text

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

DESCRIPTION
       Echo the STRING(s) to standard output.

       -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)

       NOTE: your shell may have its own version of echo, which usually supersedes the version described here.  Please refer to your shell’s documenta-
       tion for details about the options it supports.

AUTHOR
       Written by Brian Fox and Chet Ramey.

REPORTING BUGS
       Report echo bugs to bug-coreutils@gnu.org
       GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
       General help using GNU software: <http://www.gnu.org/gethelp/>
       Report echo translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2010 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       The full documentation for echo is maintained as a Texinfo manual.  If the info and echo programs are properly installed at your site, the  com-
       mand

              info coreutils 'echo invocation'

       should give you access to the complete manual.

GNU coreutils 8.4                  June 2014                           ECHO(1)