加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

变量结尾处的Bash变量嵌入换行符

发布时间:2020-12-15 21:23:49 所属栏目:安全 来源:网络整理
导读:我已经阅读了 Trying to embed newline in a variable in bash,我认为我理解新行作为IFS,以及bash有时如何将换行更改为空格,但我不明白这种情况: [prompt]$blah="$(printf "hinnnn")"[prompt]$echo "$blah"hi[prompt]$blah="$(printf "hinnnnx")"[p
我已经阅读了 Trying to embed newline in a variable in bash,我认为我理解新行作为IFS,以及bash有时如何将换行更改为空格,但我不明白这种情况:

[prompt]$blah="$(printf "hinnnn")"
[prompt]$echo "$blah"
hi
[prompt]$blah="$(printf "hinnnnx")"
[prompt]$echo "$blah"
hi



x

为什么第一个回声没有吐出一堆新行?谢谢.

解决方法

因为这就是 the spec所说的应该做的事情.即条带尾随新行的序列.

从规范(强调我的):

The shell shall expand the command substitution by executing command in a subshell environment (see Shell Execution Environment) and replacing the command substitution (the text of command plus the enclosing “$()” or backquotes) with the standard output of the command,removing sequences of one or more newline characters at the end of the substitution. Embedded newline characters before the end of the output shall not be removed; however,they may be treated as field delimiters and eliminated during field splitting,depending on the value of IFS and quoting that is in effect. If the output contains any null bytes,the behavior is unspecified.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读