command substitution: ignored null byte in input 備忘録

Environment
Photo by hyt.

command substitution: ignored null byte in input がよく分からないです.

Linux でいまログインしているのが tty* なのかそれとも sshd なのかを判定して,その結果に応じて実行コマンドを変更するスクリプト

if [ -f /proc/$PPID/cmdline ]; then
  if [ "$(command cut -d : -f1 < "/proc/$PPID/cmdline")" != "sshd" ] && [[ $- == *i* ]]; then
    echo "Hello!"
  fi
fi

を実行したところ,警告として,

command substitution: ignored null byte in input

が出力されました.

この警告,どうも Bash ver. 4.4 からのものらしく,書かれている通り,input された null byte を無視したとのもののよう.対策は,

if [ -f /proc/$PPID/cmdline ]; then
  if [ "$(command cut -d : -f1 < "/proc/$PPID/cmdline"|tr -d "\0")" != "sshd" ] && [[ $- == *i* ]]; then
    echo "Hello!"
  fi
fi

のように tr で NULL を削除すれば良いだけのよう.bash では null 文字の代入ができないということみたいですね.

-bash: 警告: command substitution: ignored null byte in input - Qiita
なンか鬱陶しいメッセージ出る。消えない😠$ MODEL=`cat /proc/device-tree/model`-bash: 警告: command substitution: ignored…

以上!

Environment
スポンサーリンク
Following hyt!
タイトルとURLをコピーしました