深入探究Shell字符串I/O与命令行处理
1. 额外的bash printf说明符
在标准说明符之外,bash shell(以及其他符合POSIX标准的shell)还接受两个额外的说明符,虽然这可能会牺牲在其他shell或UNIX其他地方的printf命令版本中的可移植性,但它们提供了实用的功能:
-%b:当使用%b代替%s时,它会展开参数字符串中的echo风格的转义序列。例如:
$ printf "%s\n" 'hello\nworld' hello\nworld $ printf "%b\n" 'hello\nworld' hello world- %q:当使用
%q代替%s时,它会以一种可以用于shell输入的方式打印字符串参数。例如:
$ printf "%q\n" "greetings to the world" greetings\ to\ the\ world2. read命令
read命令是shell字符串I/O工具的另一半,它允许将值读入shell变量。基本语法如下: