Why am I getting an unexpected operator error in the w bash equality test?

Where is the error in line four?

if [ $bn == README ]; then 

which i still get if i write it like

 if [ $bn == README ] then 

or

 if [ "$bn" == "README" ]; then 

Context:

 for fi in /etc/uwsgi/apps-available/* do bn=`basename $fi .ini` if [ $bn == "README" ] then echo "~ ***#*** ~" else echo "## Shortend for convience ##" fi done 
+6
source share
2 answers

You cannot use == to compare single brackets ([]). Use single = instead. You must also specify variables to prevent expansion.

 if [ "$bn" = README ]; then 

If you use [[]], this may apply, and you will not need to specify the first argument:

 if [[ $bn == README ]]; then 
+8
source

Add the following to the top of the script:

 #! /bin/bash 

In bash, == same as = when used inside separate brackets. This, however, is not portable. Therefore, you must explicitly tell the shell to use bash as the script interpreter by placing #! /bin/bash #! /bin/bash at the beginning of the script.

Alternatively, compare the lines with = . Note that the == operator behaves differently when used inside double brackets than in the case inside brackets (see Link).

+3
source

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


All Articles