Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

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

bash --version
GNU bash, version 4.4.12(1)-release (arm-unknown-linux-gnueabihf)

With bash version 4.3.30 it's all OK

I understand the problem is the terminating \0 character in the file, but how can I suppress this stupid message? My whole script is messed up since I'm on bash 4.4

like image 204
SBF Avatar asked Sep 11 '17 20:09

SBF


2 Answers

If you just want to delete the null byte:

model=$(tr -d '\0' < /proc/device-tree/model)
like image 80
Kevin Avatar answered Nov 06 '22 22:11

Kevin


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
    
like image 23
Charles Duffy Avatar answered Nov 06 '22 22:11

Charles Duffy