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

前端 未结 2 1314
暗喜
暗喜 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: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 
    • Read ignoring all NULs. This gets you equivalent behavior to the newer (4.4) release of bash.

      model=$(tr -d '\0' 

      You could also implement it using only builtins as follows:

      model=""
      while IFS= read -r -d '' substring || [[ $substring ]]; do
        model+="$substring"
      done 

提交回复
热议问题