- echo
echo 應該是linux用得最多的輸出命令,作用是將參數打印到標準輸出,或者重定向到目標位置。
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) #關閉轉義序列(默認)
比如簡單的輸出:
[cmd@localhost ~]$ echo hello world
hello world
[cmd@localhost ~]$ echo "hello world"
hello world
-n選項
[cmd@localhost ~]$ echo -n "hello world"
hello world[cmd@localhost ~]$
轉義序列
\\ backslash #反斜線
[cmd@localhost ~]$ echo -e "hello world\\"
hello world\
\a alert (BEL) #警告
[cmd@localhost ~]$ echo -e "\ahello world"
hello world #會聽到響一聲
\b backspace #退格
\c produce no further output #忽略後面的字符,包括換行符
[cmd@localhost ~]$ echo -e "hello world\chehe"
hello world[cmd@localhost ~]$
\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)
- printf
NAME
printf - format and print data
SYNOPSIS
printf FORMAT [ARGUMENT]...
printf OPTION
DESCRIPTION
#轉義序列跟echo差不多
\" double quote
\\ ackslash
\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
\NNN byte with octal value NNN (1 to 3 digits)
\xHH byte with hexadecimal value HH (1 to 2 digits)
\uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)
\UHHHHHHHH Unicode character with hex value HHHHHHHH (8 digits)
%% a single %
%b ARGUMENT as a string with '\' escapes interpreted,
except that octal escapes are of the form \0 or \0NNN
printf幾乎複製了c庫裏的printf()函數的所有功能,用法也類似。
[cmd@localhost ~]$ printf "hello world\n"
hello world
[cmd@localhost ~]$ printf "hello world %s %s %s\n" take it easy
hello world take it easy