Why does this simple bash code give a syntax error?

I have the following bash code that is copied and pasted from a "bash cookbook" (1st edition):

#!/bin/bash VERBOSE=0; if [[ $1 =-v ]] then VERBOSE=1; shift; fi 

When I run this (bash 4.0.33), I get the following syntax error:

 ./test.sh: line 4: conditional binary operator expected ./test.sh: line 4: syntax error near `=-v' ./test.sh: line 4: `if [[ $1 =-v ]]' 

Is it as simple as a typo in a bash cookbook, or is there a version incompatibility or something else here? What would be the most obvious solution? I tried various combinations of operator change, but I am not very good at bash scripts.

+4
source share
1 answer

Bash uses spaces to tokenize scripts. Line:

 if [[ $1 =-v ]] 

it should be:

 if [[ $1 = -v ]] 
+11
source

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


All Articles