bash printf 带新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15565605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:28:57  来源:igfitidea点击:

bash printf with new line

bashprintf

提问by idobr

I'm a bit confused with printing a variable that contain a new line symbol in bash.

我对在 bash 中打印包含换行符的变量有点困惑。

var="Age:\n20\ncolor:\nred"
echo -e $var
Age:
20
color:
red

This is working, but a lot of people say that echo with options is not portableand it is better to use printf.

这是有效的,但很多人说带有选项的 echo不可移植最好使用 printf

I never used prinf. According to manuals to emitate echo command:

我从来没有用过 prinf。根据手册发出 echo 命令:

printf '%s\n' "$var"
Age:\n20\ncoloe:\nred

But this doesn't parse \n inside variable. manuals usually have this example:

但这不会解析 \n 内部变量。手册通常有这个例子:

printf "Surname: %s\nName: %s\n" "$SURNAME" "$LASTNAME"

But it's not my case and from my point of view it not comfortable to use. I found out simply by typing that I can use this:

但这不是我的情况,从我的角度来看,使用起来并不舒服。我只是通过输入发现我可以使用它:

printf "$var\n"

printf "$var\n"

Is it portable? If I then pass $var to a mail command will it save new line breaks?

便携吗?如果我然后将 $var 传递给邮件命令,它会保存新的换行符吗?

printf "$var\n" | mail -s subj [email protected] 

回答by ormaaj

printf's %bformat specifier was meant specifically to replace echo -e(actually, the XSI extension to echowhich calls for special interpretation of the arguments by default. -ewas never specified and is disallowed by POSIX.), and is identical in virtually every way including a few differences from $'...'and the format string argument to printf.

printf%b格式说明符专门用于替换echo -e(实际上,echo默认情况下要求对参数进行特殊解释的 XSI 扩展。-e从未指定并且被 POSIX 禁止。),并且几乎在所有方面都相同,包括与$'...'和格式字符串参数printf

 $ ( var='Age:\n20\ncolor:\nred'; printf '%b\n' "$var" )
Age:
20
color:
red

You should generally avoid expanding variables into the format string unless your program controls the exact value and it is intended specifically to be a format string. Your last example in particular has the potential to be quite dangerous in Bash due to printf's -voption.

您通常应该避免将变量扩展到格式字符串中,除非您的程序控制了确切的值并且它专门用作格式字符串。由于printf's-v选项,您的最后一个示例特别有可能在 Bash 中非常危险。

# Bad!
var='-v_[$(echo "oops, arbitrary code execution" >&2)0]'
printf "$var" foo

It is usually good practice to avoid %bunless you have a special portability requirement. Storing the escape codes in a variable instead of the literal data violates principles of separation of code and data. There are contexts in which this is ok, but it is usually better to assign the the value using $'...'quoting, which is specified for the next version of POSIX, and has long been available in Bash and most ksh flavours.

%b除非您有特殊的可移植性要求,否则通常避免这种做法是很好的做法。将转义码存储在变量而不是文字数据中违反了代码和数据分离的原则。在某些情况下这是可以的,但通常最好使用$'...'引用来分配值,这是为下一版本的 POSIX 指定的,并且在 Bash 和大多数 ksh 版本中早已可用。

x=$'foo\nbar'; printf '%s\n' "$x"    # Good
x=(foo bar); printf '%s\n' "${x[@]}" # Also good (depending on the goal)
x='foo\nbar'; printf '%b\n' "$x"     # Ok, especially for compatibility
x='foo\nbar'; printf -- "$x"         # Avoid if possible, without specific reason