次のコードを使用して、OSXでbashの引数を比較しようとしています...
#!/bin/bash
if ["$1" == "1"]
then
echo $1
else
echo "no"
fi
しかし、私は次のエラーを受け取り続けます
$bash script.sh 1
script.sh: line 3: [1: command not found
no
「1」を評価しようとするのを防ぐにはどうすればよいですか?
[
is a test command, so you need a space between [
and "$1"
, as well as a space between "1"
and the closing ]
Edit
Just to clarify, the space is needed because [
is a different syntax of the test
bash command, so the following is another way of writing the script:
#!/bin/bash
if test "$1" == "1"
then
echo $1
else
echo "no"
fi
Which can be further simplified to
#!/bin/bash
[ "$1" == "1" ] && echo "$1" || echo "no"