摆脱"warning: command substitution: ignored null byte in input"



当我运行model=$(cat /proc/device-tree/model)

时,我会得到-bash: warning: command substitution: ignored null byte in input
bash --version
GNU bash, version 4.4.12(1)-release (arm-unknown-linux-gnueabihf)

使用Bash版本4.3.30都可以,一切正常

我知道问题是文件中终止字符,但是如何抑制此愚蠢的消息?由于我在Bash 4.4

时,我的整个脚本都搞砸了

如果您只想删除null字节:

model=$(tr -d '' < /proc/device-tree/model)

您可能想要两种可能的行为:

  • 阅读直到第一次nul。这是更具性能的方法,因为它不需要外壳的外部过程。在失败后检查目标变量是否是非空的,可以在读取内容但输入中不存在NUL(否则会导致非零退出状态)。

    IFS= read -r -d '' model </proc/device-tree/model || [[ $model ]]
    
  • 阅读忽略所有nul。这使您与bash的较新(4.4)释放相同。

    model=$(tr -d '' </proc/device-tree/model)
    

    您也可以仅使用内置素来实现它:

    model=""
    while IFS= read -r -d '' substring || [[ $substring ]]; do
      model+="$substring"
    done </proc/device-tree/model
    

相关内容

最新更新