您的位置:首页 > 运维架构 > Shell

【Bash百宝箱】shell内建命令之echo、printf

2016-09-23 18:01 525 查看

1、echo

在shell中,内建(builtin)命令echo,格式如下:

echo [-neE] [arg ...]


echo命令用于输出各参数arg,参数间以空格分隔,结尾是个换行符。选项“-n”禁止输出结尾的换行符。对于一些反斜线“\”转义的特殊字符,在echo命令中默认不进行转义,选项“-e”启用转义,“-E”禁止转义。

下面是反斜线“\”转义的特殊字符。

\a     警告(响铃)
\b     退格删除
\c     禁止继续输出
\e     转义字符
\E     转义字符
\f     换页
\n     新行
\r     换行
\t     水平制表符
\v     垂直制表符
\\     反斜线
\0nnn  八进制数nnn表示的八位字符
\xHH   十六进制数HH表示的八位字符
\uHHHH  一到四个十六进制数表示的Unicode字符
\UHHHHHHHH  一到八个十六进制数表示的Unicode字符


例子如下:

$ echo "a\tb"
a\tb
$ echo -e "a\tb"
a   b
$ echo -E "a\tb"
a\tb


2、printf

在shell中,内建(builtin)命令printf,格式如下:

printf [-v var] format [arguments]


printf命令用于把格式化的参数arguments打印到标准输出,格式由参数format控制。如果指定了选项“-v var”,结果会保存到变量var中,而非打印到标准输出。

格式format有三种形式。第一种是原始字符串,直接打印到标准输出。第二种是转义字符序列,先转义后打印到标准输出。第三种是格式控制字符串,与后面的参数arguments对应。

格式控制有以下几种形式:

\"     双引号
\\     反斜线
\a     警告(响铃)
\b     退格删除
\c     禁止继续输出
\e     转义字符
\f     分页
\n     新行
\r     换行
\t     水平制表符
\v     垂直制表符
\NNN   八进制数
\xHH   十六进制数
\uHHHH  十六进制Unicode
\UHHHHHHHH  十六进制Unicode
%%     百分号
%b     扩展printf中参数arguments的反斜线转义序列,但“\c”例外,“\'”、“\"”和“\?”中的反斜线也不会去掉,而且以“\0”开头的八进制数可能包含四个数字。
%q     把对应参数arguments以能够重新作为shell输入的格式打印出来。
%(datefmt)T    以格式datefmt输出日期,对应的参数argument为从1970年1月1日开始的秒数,有两个特殊的参数,-1表示当前时间,-2表示shell运行时间,默认为-1。


上面提到的日期格式如下(摘录自“man 3 strftime”):

%a     The abbreviated name of the day of the week according to the current locale.
%A     The full name of the day of the week according to the current locale.
%b     The abbreviated month name according to the current locale.
%B     The full month name according to the current locale.
%c     The preferred date and time representation for the current locale.
%C     The century number (year/100) as a 2-digit integer. (SU)
%d     The day of the month as a decimal number (range 01 to 31).
%D     Equivalent to %m/%d/%y.  (Yecch—for Americans only.  Americans should note that in other countries %d/%m/%y is rather common.  This means that in international context this  format  is
ambiguous and should not be used.) (SU)
%e     Like %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (SU)
%E     Modifier: use alternative format, see below. (SU)
%F     Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
%G     The  ISO 8601  week-based year (see NOTES) with century as a decimal number.  The 4-digit year corresponding to the ISO week number (see %V).  This has the same format and value as %Y,
except that if the ISO week number belongs to the previous or next year, that year is used instead. (TZ)
%g     Like %G, but without century, that is, with a 2-digit year (00-99). (TZ)
%h     Equivalent to %b.  (SU)
%H     The hour as a decimal number using a 24-hour clock (range 00 to 23).
%I     The hour as a decimal number using a 12-hour clock (range 01 to 12).
%j     The day of the year as a decimal number (range 001 to 366).
%k     The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank.  (See also %H.)  (TZ)
%l     The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank.  (See also %I.)  (TZ)
%m     The month as a decimal number (range 01 to 12).
%M     The minute as a decimal number (range 00 to 59).
%n     A newline character. (SU)
%O     Modifier: use alternative format, see below. (SU)
%p     Either "AM" or "PM" according to the given time value, or the corresponding strings for the current locale.  Noon is treated as "PM" and midnight as "AM".
%P     Like %p but in lowercase: "am" or "pm" or a corresponding string for the current locale. (GNU)
%r     The time in a.m. or p.m. notation.  In the POSIX locale this is equivalent to %I:%M:%S %p.  (SU)
%R     The time in 24-hour notation (%H:%M).  (SU) For a version including the seconds, see %T below.
%s     The number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
%S     The second as a decimal number (range 00 to 60).  (The range is up to 60 to allow for occasional leap seconds.)
%t     A tab character. (SU)
%T     The time in 24-hour notation (%H:%M:%S).  (SU)
%u     The day of the week as a decimal, range 1 to 7, Monday being 1.  See also %w.  (SU)
%U     The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01.  See also %V and %W.
%V     The ISO 8601 week number (see NOTES) of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the new year.  See also %U  and
%W.  (SU)
%w     The day of the week as a decimal, range 0 to 6, Sunday being 0.  See also %u.
%W     The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01.
%x     The preferred date representation for the current locale without the time.
%X     The preferred time representation for the current locale without the date.
%y     The year as a decimal number without a century (range 00 to 99).
%Y     The year as a decimal number including the century.
%z     The +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC). (SU)
%Z     The timezone name or abbreviation.
%+     The date and time in date(1) format. (TZ) (Not supported in glibc2.)
%%     A literal '%' character.


对于标准C中printf函数的转换字符,百分号后跟“diouxXfeEgGcs”中的任意一个字符,在printf命令中也可以使用,而且标准C中printf函数的输出样式控制标记、输出字符串宽度、数字精度等也适用于printf命令。下面以例子说明。

打印十六进制整数:

$ printf "%x\n" 17
11
$ printf "%X\n" 17
11
$ printf "%#x\n" 17
0x11
$ printf "%#X\n" 17
0X11


设置输出字符串宽度和对齐方式:

$ printf "%d\n" 123
123
$ printf "%6d\n" 123
123
$ printf "%-6d\n" 123
123


设置输出字符串宽度和填充方式:

$ printf "%d\n" 123
123
$ printf "%6d\n" 123
123
$ printf "%06d\n" 123
000123


在正数前面添加空格:

$ printf "%d\n" 123
123
$ printf "% d\n" 123
123
$ printf "%d\n" -123
-123
$ printf "% d\n" -123
-123


在正数前面添加加号:

$ printf "%d\n" 123
123
$ printf "%+d\n" 123
+123
$ printf "%d\n" -123
-123
$ printf "%+d\n" -123
-123


设置数字输出格式为千分位:

$ printf "%d\n" 123456789
123456789
$ printf "%'d\n" 123456789
123,456,789


设置浮点数精度:

$ printf "%f\n" 123
123.000000
$ printf "%.f\n" 123
123
$ printf "%.1f\n" 123
123.0
$ printf "%.3f\n" 123
123.000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: