Get rid of “warning: command substitution: ignored null byte in input”

前端 未结 2 1298
暗喜
暗喜 2021-01-01 15:08

I\'m getting -bash: warning: command substitution: ignored null byte in input when I run model=$(cat /proc/device-tree/model)

bash          


        
相关标签:
2条回答
  • 2021-01-01 15:32

    If you just want to delete the null byte:

    model=$(tr -d '\0' < /proc/device-tree/model)
    
    0 讨论(0)
  • 2021-01-01 15:44

    There are two possible behaviors you might want here:

    • Read until first NUL. This is the more performant approach, as it requires no external processes to the shell. Checking whether the destination variable is non-empty after a failure ensures a successful exit status in the case where content is read but no NUL exists in input (which would otherwise result in a nonzero exit status).

      IFS= read -r -d '' model </proc/device-tree/model || [[ $model ]]
      
    • Read ignoring all NULs. This gets you equivalent behavior to the newer (4.4) release of bash.

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

      You could also implement it using only builtins as follows:

      model=""
      while IFS= read -r -d '' substring || [[ $substring ]]; do
        model+="$substring"
      done </proc/device-tree/model
      
    0 讨论(0)
提交回复
热议问题