【Bash百宝箱】shell内建命令之echo、printf
来源:互联网 发布:中国十大军工集团知乎 编辑:程序博客网 时间:2024/05/16 06:41
原文地址:http://blog.csdn.net/iEearth/article/details/52640972
1、echo
在shell中,内建(builtin)命令echo,格式如下:
echo [-neE] [arg ...]
- 1
echo命令用于输出各参数arg,参数间以空格分隔,结尾是个换行符。选项“-n”禁止输出结尾的换行符。对于一些反斜线“\”转义的特殊字符,在echo命令中默认不进行转义,选项“-e”启用转义,“-E”禁止转义。
下面是反斜线“\”转义的特殊字符。
\a 警告(响铃) \b 退格删除 \c 禁止继续输出 \e 转义字符 \E 转义字符 \f 换页 \n 新行 \r 换行 \t 水平制表符 \v 垂直制表符 \\ 反斜线 \0nnn 八进制数nnn表示的八位字符 \xHH 十六进制数HH表示的八位字符 \uHHHH 一到四个十六进制数表示的Unicode字符 \UHHHHHHHH 一到八个十六进制数表示的Unicode字符
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
例子如下:
$ echo "a\tb"a\tb$ echo -e "a\tb"a b$ echo -E "a\tb"a\tb
- 1
- 2
- 3
- 4
- 5
- 6
2、printf
在shell中,内建(builtin)命令printf,格式如下:
printf [-v var] format [arguments]
- 1
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。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
上面提到的日期格式如下(摘录自“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.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
对于标准C中printf函数的转换字符,百分号后跟“diouxXfeEgGcs”中的任意一个字符,在printf命令中也可以使用,而且标准C中printf函数的输出样式控制标记、输出字符串宽度、数字精度等也适用于printf命令。下面以例子说明。
打印十六进制整数:
$ printf "%x\n" 1711$ printf "%X\n" 1711$ printf "%#x\n" 170x11$ printf "%#X\n" 170X11
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
设置输出字符串宽度和对齐方式:
$ printf "%d\n" 123123$ printf "%6d\n" 123 123$ printf "%-6d\n" 123123
- 1
- 2
- 3
- 4
- 5
- 6
设置输出字符串宽度和填充方式:
$ printf "%d\n" 123123$ printf "%6d\n" 123 123$ printf "%06d\n" 123000123
- 1
- 2
- 3
- 4
- 5
- 6
在正数前面添加空格:
$ printf "%d\n" 123123$ printf "% d\n" 123 123$ printf "%d\n" -123-123$ printf "% d\n" -123-123
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
在正数前面添加加号:
$ printf "%d\n" 123123$ printf "%+d\n" 123+123$ printf "%d\n" -123-123$ printf "%+d\n" -123-123
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
设置数字输出格式为千分位:
$ printf "%d\n" 123456789123456789$ printf "%'d\n" 123456789123,456,789
- 1
- 2
- 3
- 4
设置浮点数精度:
$ printf "%f\n" 123123.000000$ printf "%.f\n" 123123$ printf "%.1f\n" 123123.0$ printf "%.3f\n" 123123.000
阅读全文
0 0
- 【Bash百宝箱】shell内建命令之echo、printf
- 【Bash百宝箱】shell内建命令之echo、printf
- 【Bash百宝箱】shell内建命令之冒号
- 【Bash百宝箱】shell内建命令之句点与source
- 【Bash百宝箱】shell内建命令之break与continue
- 【Bash百宝箱】shell内建命令之cd、getopts
- 【Bash百宝箱】shell内建命令之hash
- 【Bash百宝箱】shell内建命令之times、umask、unset
- 【Bash百宝箱】shell内建命令之test
- 【Bash百宝箱】shell内建命令之trap
- 【Bash百宝箱】shell内建命令之alias与unalias
- 【Bash百宝箱】shell内建命令之builtin、command、caller
- 【Bash百宝箱】shell内建命令之declare、typeset、local
- 【Bash百宝箱】shell内建命令之read、readarray、mapfile
- 【Bash百宝箱】shell内建命令之bind、enable、ulimit
- 【Bash百宝箱】shell内建命令之set、shopt
- 【Bash百宝箱】shell内建命令之eval、exec、exit、export
- 【Bash百宝箱】shell内建命令之pwd、readonly、return、shift
- 多线程与IO密集/CPU密集
- 阿里巴巴开发公约节选(日常编程较常用的一些)
- Play2 for Java(二:开始)
- 判断map集合中是否存在某个key
- 160
- 【Bash百宝箱】shell内建命令之echo、printf
- i++与++i的区别
- Springboot+Maven+Tomcat部署
- numpy where 理解
- HttpClient(同步版本)
- Linux学习笔记(一)一些基础实用的东西
- Agri-Net Prim(Kruskal)的应用
- 10个JavaScript常见BUG及修复方法
- 《计算机科学导论(第2版)》读书笔记(2)第2章数字系统(关键词:计算机科学/数字系统)