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
If you just want to delete the null byte:
model=$(tr -d '\0' < /proc/device-tree/model)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With