0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

シェルスクリプトで空の変数と比較したかった

Posted at

地味に間違えるやつ。
※以下の処理内容は適当です。

変数が空じゃないとき

$ a="a"
$ if [ $a != "hoge" ]; then echo "not hoge"; fi
not hoge

変数が空のとき

$ a=""
$ if [ $a != "hoge" ]; then echo "not hoge"; fi
bash: [: !=: 単項演算子が予期されます

!= が怒られてて、単行演算子を予期していたとのことなので、おそらく if [ != "hoge" ] と解釈されていると思われます。

これで解決

$ a=""
$ if [ "$a" != "hoge" ]; then echo "not hoge"; fi
not hoge

変数って""で挟んでもいいんですね。

環境

Linux Mint 21.3
bash 5.1.16(1)-release (x86_64-pc-linux-gnu)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?