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

2023-12-06 20:38

本文主要是介绍【Bash百宝箱】shell内建命令之echo、printf,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址: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  isambiguous 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" 17
11
$ printf "%X\n" 17
11
$ printf "%#x\n" 17
0x11
$ printf "%#X\n" 17
0X11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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

$ printf "%d\n" 123
123
$ printf "%6d\n" 123123
$ printf "%-6d\n" 123
123   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

$ printf "%d\n" 123
123
$ printf "%6d\n" 123123
$ printf "%06d\n" 123
000123
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在正数前面添加空格:

$ printf "%d\n" 123
123
$ printf "% d\n" 123123
$ printf "%d\n" -123
-123
$ printf "% d\n" -123
-123
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在正数前面添加加号:

$ printf "%d\n" 123
123
$ 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" 123456789
123456789
$ printf "%'d\n" 123456789
123,456,789
  • 1
  • 2
  • 3
  • 4

设置浮点数精度:

$ printf "%f\n" 123
123.000000
$ printf "%.f\n" 123
123
$ printf "%.1f\n" 123
123.0
$ printf "%.3f\n" 123
123.000

这篇关于【Bash百宝箱】shell内建命令之echo、printf的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/463240

相关文章

postgresql数据库基本操作及命令详解

《postgresql数据库基本操作及命令详解》本文介绍了PostgreSQL数据库的基础操作,包括连接、创建、查看数据库,表的增删改查、索引管理、备份恢复及退出命令,适用于数据库管理和开发实践,感兴... 目录1. 连接 PostgreSQL 数据库2. 创建数据库3. 查看当前数据库4. 查看所有数据库

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

shell中set -u、set -x、set -e的使用

《shell中set-u、set-x、set-e的使用》本文主要介绍了shell中set-u、set-x、set-e的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录✅ 1. set -u:防止使用未定义变量 作用: 示例:❌ 报错示例输出:✅ 推荐使用场景:✅ 2. se

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Java -jar命令如何运行外部依赖JAR包

《Java-jar命令如何运行外部依赖JAR包》在Java应用部署中,java-jar命令是启动可执行JAR包的标准方式,但当应用需要依赖外部JAR文件时,直接使用java-jar会面临类加载困... 目录引言:外部依赖JAR的必要性一、问题本质:类加载机制的限制1. Java -jar的默认行为2. 类加

git stash命令基本用法详解

《gitstash命令基本用法详解》gitstash是Git中一个非常有用的命令,它可以临时保存当前工作区的修改,让你可以切换到其他分支或者处理其他任务,而不需要提交这些还未完成的修改,这篇文章主要... 目录一、基本用法1. 保存当前修改(包括暂存区和工作区的内容)2. 查看保存了哪些 stash3. 恢

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

Linux基础命令@grep、wc、管道符的使用详解

《Linux基础命令@grep、wc、管道符的使用详解》:本文主要介绍Linux基础命令@grep、wc、管道符的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录grep概念语法作用演示一演示二演示三,带选项 -nwc概念语法作用wc,不带选项-c,统计字节数-