Bash: How to compare arguments with if statement?

I am trying to compare an argument in bash under OSX using the following code ...

#!/bin/bash if ["$1" == "1"] then echo $1 else echo "no" fi 

But I keep getting the following error:

 $bash script.sh 1 script.sh: line 3: [1: command not found no 

How to stop him from trying to evaluate "1"?

+6
source share
1 answer

[ - this is a test command, so you need a space between [ and "$1" , as well as a space between "1" and closing ]

Edit

Just for clarification, space is necessary because [ is a different syntax of the test bash command, so the following script method is written:

 #!/bin/bash if test "$1" == "1" then echo $1 else echo "no" fi 

What else can be simplified to

 #!/bin/bash [ "$1" == "1" ] && echo "$1" || echo "no" 
+18
source

Source: https://habr.com/ru/post/913042/


All Articles