【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

相关文章

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

30常用 Maven 命令

Maven 是一个强大的项目管理和构建工具,它广泛用于 Java 项目的依赖管理、构建流程和插件集成。Maven 的命令行工具提供了大量的命令来帮助开发人员管理项目的生命周期、依赖和插件。以下是 常用 Maven 命令的使用场景及其详细解释。 1. mvn clean 使用场景:清理项目的生成目录,通常用于删除项目中自动生成的文件(如 target/ 目录)。共性规律:清理操作

利用命令模式构建高效的手游后端架构

在现代手游开发中,后端架构的设计对于支持高并发、快速迭代和复杂游戏逻辑至关重要。命令模式作为一种行为设计模式,可以有效地解耦请求的发起者与接收者,提升系统的可维护性和扩展性。本文将深入探讨如何利用命令模式构建一个强大且灵活的手游后端架构。 1. 命令模式的概念与优势 命令模式通过将请求封装为对象,使得请求的发起者和接收者之间的耦合度降低。这种模式的主要优势包括: 解耦请求发起者与处理者

linux 判断某个命令是否安装

linux 判断某个命令是否安装 if ! [ -x "$(command -v git)" ]; thenecho 'Error: git is not installed.' >&2exit 1fi

jenkins 插件执行shell命令时,提示“Command not found”处理方法

首先提示找不到“Command not found,可能我们第一反应是查看目标机器是否已支持该命令,不过如果相信能找到这里来的朋友估计遇到的跟我一样,其实目标机器是没有问题的通过一些远程工具执行shell命令是可以执行。奇怪的就是通过jenkinsSSH插件无法执行,经一番折腾各种搜索发现是jenkins没有加载/etc/profile导致。 【解决办法】: 需要在jenkins调用shell脚

Linux命令(4):fg与bg命令

fg、bg、jobs、&、ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的 一.& 最经常被用到 这个用在一个命令的最后,可以把这个命令放到后台执行 二.ctrl + z 可以将一个正在前台执行的命令放到后台,并且暂停 三.jobs 查看当前有多少在后台运行的命令 四.fg 将后台中的命令调至前台继续运行 如果后台中有多个命令,可以

Linux命令(3):sz与rz命令

一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地; 与ssh有关的两个命令可以提供很方便的操作: sz:将选定的文件发送(send)到本地机器 rz:运行该命令会弹出一个文件选择窗口,从本地选择文件上传到服务器(receive) rz,sz是便是Linux

Detectorn2预训练模型复现:数据准备、训练命令、日志分析与输出目录

Detectorn2预训练模型复现:数据准备、训练命令、日志分析与输出目录 在深度学习项目中,目标检测是一项重要的任务。本文将详细介绍如何使用Detectron2进行目标检测模型的复现训练,涵盖训练数据准备、训练命令、训练日志分析、训练指标以及训练输出目录的各个文件及其作用。特别地,我们将演示在训练过程中出现中断后,如何使用 resume 功能继续训练,并将我们复现的模型与Model Zoo中的

Linux命令(11):系统信息查看命令

系统 # uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本# cat /proc/cpuinfo # 查看CPU信息# hostname # 查看计算机名# lspci -tv # 列出所有PCI设备# lsusb -tv

Android下执行linux命令

最近在开发过程中 使用了几个命令来对   手机的文件的权限进行修改;现在记录一下: 用到的方法: 1:判断是否有Root权限;  /**      * 判断当前手机是否有ROOT权限      * @return      */     public static boolean isRoot(){         boolean bool = false;         try{