Shell spacing in square brackets [duplicate] - shell

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 4 years ago.
As a beginner, I did not find answered anywhere, the rules about spacing (grammar) and parsing.
For example.
Can I do
if [$# -eq 2] ;
then
llll
fi
or must I always have a blank or two between the objects, as
if [ $# -eq 2 ] ;
then
llll
fi
and the second related question is about the difference between
if [[ $# -eq 2 ]] ;
then
wafwaf
fi
The concern I have is about spacing before/after [, ].
No searching has provided me with a set of rules.

Spaces are required after [ and before ].
[ is actually the name of a command, an alias for test. It's not a special symbol, it's just a command with an unusual name.
$ help '['
[: [ arg... ]
Evaluate conditional expression.
This is a synonym for the "test" builtin, but the last argument must
be a literal `]', to match the opening `['.
Because it's an ordinary command name and not a special character, a space is required after the [. If you omit the space and write [foo the shell will search the $PATH for a command named [foo.
$ [ foo = foo ] && echo true
true
$ [foo = foo] && echo true
[foo: command not found
For readability's sake, [ expects its last argument to be exactly ]. Being an ordinary command-line argument, ] must have a space before it. If there's no space then the bracket will become the last character of the previous argument, and [ will complain about its last argument not being ].
$ [ foo = foo]
bash: [: missing `]'
$ [ foo = 'foo]'
bash: [: missing `]'
[[ is a bash enhancement with more features than [, namely saner handling of unquoted variable names. It requires the a space on both ends, same as [. However [[ is in fact special shell syntax and is parsed differently. It's not an "ordinary command" the way [ is.
For a detailed explanation of the difference between [ and [[, see:
What's the difference between [ and [[ in Bash?

Related

Why won't my bash script work even though I think I am doing everything right? No matter where I look I cannot find an answer [duplicate]

I tried the following script
#!/bin/bash
var1="Test 1"
var2="Test 2"
if [ "$var1"="$var2" ]
then
echo "Equal"
else
echo "Not equal"
fi
It gave me Equal. Although it should have printed Not equal
Only when I inserted space around = it worked as intended
if [ "$var1" = "$var2" ]
and printed Not equal
Why is it so? Why "$var1"="$var2" is not same as "$var1" = "$var2"?
Moreover, when I wrote if [ "$var1"= "$var2" ], it gave
line 4: [: Test 1=: unary operator expected
What does it it mean? How come its expecting unary operator?
test (or [ expr ]) is a builtin function. Like all functions in bash, you pass its arguments as whitespace separated words.
As the man page for bash builtins states: "Each operator and operand must be a separate argument."
It's just the way bash and most other Unix shells work.
Variable assignment is different.
In bash a variable assignment has the syntax: name=[value]. You cannot put unquoted spaces around the = because bash would not interpret this as the assignment you intend. bash treats most lists of words as a command with parameters.
E.g.
# call the command or function 'abc' with '=def' as argument
abc =def
# call 'def' with the variable 'abc' set to the empty string
abc= def
# call 'ghi' with 'abc' set to 'def'
abc=def ghi
# set 'abc' to 'def ghi'
abc="def ghi"
When the shell reads
if [ "$var1" = "$var2" ]
it invokes the command [ with 4 arguments. Whether [ is a builtin or an external command is irrelevant, but it may help to understand that it may be the external command /bin/[. The second argument is the literal '=' and the fourth is ']'. However, when the shell reads
if [ "$var1"= "$var2" ]
[ only gets 3 arguments: the expansion of $var1 with '=' appended, the expansion of $var2, and ']'. When it gets only 3 arguments, it expects the last argument to be ']' and the first argument to be a unary operator.
To add to the existing explanation, "$var1"="$var2" is just a single non-empty string, and thus always evaluates as true in a conditional.
[ "$var1"="$var2" ] && echo true
The above command will always print out true (even if var1 and var2 be empty).
In bash the best is to use [[ ]]:
x="test"
y="test"
if [[ "${x}" = "${y}" ]]; then
echo "Equals"
else
echo "No equals"
fi

A bash script to reverse the array [duplicate]

I tried the following script
#!/bin/bash
var1="Test 1"
var2="Test 2"
if [ "$var1"="$var2" ]
then
echo "Equal"
else
echo "Not equal"
fi
It gave me Equal. Although it should have printed Not equal
Only when I inserted space around = it worked as intended
if [ "$var1" = "$var2" ]
and printed Not equal
Why is it so? Why "$var1"="$var2" is not same as "$var1" = "$var2"?
Moreover, when I wrote if [ "$var1"= "$var2" ], it gave
line 4: [: Test 1=: unary operator expected
What does it it mean? How come its expecting unary operator?
test (or [ expr ]) is a builtin function. Like all functions in bash, you pass its arguments as whitespace separated words.
As the man page for bash builtins states: "Each operator and operand must be a separate argument."
It's just the way bash and most other Unix shells work.
Variable assignment is different.
In bash a variable assignment has the syntax: name=[value]. You cannot put unquoted spaces around the = because bash would not interpret this as the assignment you intend. bash treats most lists of words as a command with parameters.
E.g.
# call the command or function 'abc' with '=def' as argument
abc =def
# call 'def' with the variable 'abc' set to the empty string
abc= def
# call 'ghi' with 'abc' set to 'def'
abc=def ghi
# set 'abc' to 'def ghi'
abc="def ghi"
When the shell reads
if [ "$var1" = "$var2" ]
it invokes the command [ with 4 arguments. Whether [ is a builtin or an external command is irrelevant, but it may help to understand that it may be the external command /bin/[. The second argument is the literal '=' and the fourth is ']'. However, when the shell reads
if [ "$var1"= "$var2" ]
[ only gets 3 arguments: the expansion of $var1 with '=' appended, the expansion of $var2, and ']'. When it gets only 3 arguments, it expects the last argument to be ']' and the first argument to be a unary operator.
To add to the existing explanation, "$var1"="$var2" is just a single non-empty string, and thus always evaluates as true in a conditional.
[ "$var1"="$var2" ] && echo true
The above command will always print out true (even if var1 and var2 be empty).
In bash the best is to use [[ ]]:
x="test"
y="test"
if [[ "${x}" = "${y}" ]]; then
echo "Equals"
else
echo "No equals"
fi

Looping through cli arguments in bash with if elif and else statements [duplicate]

I tried the following script
#!/bin/bash
var1="Test 1"
var2="Test 2"
if [ "$var1"="$var2" ]
then
echo "Equal"
else
echo "Not equal"
fi
It gave me Equal. Although it should have printed Not equal
Only when I inserted space around = it worked as intended
if [ "$var1" = "$var2" ]
and printed Not equal
Why is it so? Why "$var1"="$var2" is not same as "$var1" = "$var2"?
Moreover, when I wrote if [ "$var1"= "$var2" ], it gave
line 4: [: Test 1=: unary operator expected
What does it it mean? How come its expecting unary operator?
test (or [ expr ]) is a builtin function. Like all functions in bash, you pass its arguments as whitespace separated words.
As the man page for bash builtins states: "Each operator and operand must be a separate argument."
It's just the way bash and most other Unix shells work.
Variable assignment is different.
In bash a variable assignment has the syntax: name=[value]. You cannot put unquoted spaces around the = because bash would not interpret this as the assignment you intend. bash treats most lists of words as a command with parameters.
E.g.
# call the command or function 'abc' with '=def' as argument
abc =def
# call 'def' with the variable 'abc' set to the empty string
abc= def
# call 'ghi' with 'abc' set to 'def'
abc=def ghi
# set 'abc' to 'def ghi'
abc="def ghi"
When the shell reads
if [ "$var1" = "$var2" ]
it invokes the command [ with 4 arguments. Whether [ is a builtin or an external command is irrelevant, but it may help to understand that it may be the external command /bin/[. The second argument is the literal '=' and the fourth is ']'. However, when the shell reads
if [ "$var1"= "$var2" ]
[ only gets 3 arguments: the expansion of $var1 with '=' appended, the expansion of $var2, and ']'. When it gets only 3 arguments, it expects the last argument to be ']' and the first argument to be a unary operator.
To add to the existing explanation, "$var1"="$var2" is just a single non-empty string, and thus always evaluates as true in a conditional.
[ "$var1"="$var2" ] && echo true
The above command will always print out true (even if var1 and var2 be empty).
In bash the best is to use [[ ]]:
x="test"
y="test"
if [[ "${x}" = "${y}" ]]; then
echo "Equals"
else
echo "No equals"
fi

String comparison in Bash for loop ignored [duplicate]

I tried the following script
#!/bin/bash
var1="Test 1"
var2="Test 2"
if [ "$var1"="$var2" ]
then
echo "Equal"
else
echo "Not equal"
fi
It gave me Equal. Although it should have printed Not equal
Only when I inserted space around = it worked as intended
if [ "$var1" = "$var2" ]
and printed Not equal
Why is it so? Why "$var1"="$var2" is not same as "$var1" = "$var2"?
Moreover, when I wrote if [ "$var1"= "$var2" ], it gave
line 4: [: Test 1=: unary operator expected
What does it it mean? How come its expecting unary operator?
test (or [ expr ]) is a builtin function. Like all functions in bash, you pass its arguments as whitespace separated words.
As the man page for bash builtins states: "Each operator and operand must be a separate argument."
It's just the way bash and most other Unix shells work.
Variable assignment is different.
In bash a variable assignment has the syntax: name=[value]. You cannot put unquoted spaces around the = because bash would not interpret this as the assignment you intend. bash treats most lists of words as a command with parameters.
E.g.
# call the command or function 'abc' with '=def' as argument
abc =def
# call 'def' with the variable 'abc' set to the empty string
abc= def
# call 'ghi' with 'abc' set to 'def'
abc=def ghi
# set 'abc' to 'def ghi'
abc="def ghi"
When the shell reads
if [ "$var1" = "$var2" ]
it invokes the command [ with 4 arguments. Whether [ is a builtin or an external command is irrelevant, but it may help to understand that it may be the external command /bin/[. The second argument is the literal '=' and the fourth is ']'. However, when the shell reads
if [ "$var1"= "$var2" ]
[ only gets 3 arguments: the expansion of $var1 with '=' appended, the expansion of $var2, and ']'. When it gets only 3 arguments, it expects the last argument to be ']' and the first argument to be a unary operator.
To add to the existing explanation, "$var1"="$var2" is just a single non-empty string, and thus always evaluates as true in a conditional.
[ "$var1"="$var2" ] && echo true
The above command will always print out true (even if var1 and var2 be empty).
In bash the best is to use [[ ]]:
x="test"
y="test"
if [[ "${x}" = "${y}" ]]; then
echo "Equals"
else
echo "No equals"
fi

Why equal to operator does not work if it is not surrounded by space?

I tried the following script
#!/bin/bash
var1="Test 1"
var2="Test 2"
if [ "$var1"="$var2" ]
then
echo "Equal"
else
echo "Not equal"
fi
It gave me Equal. Although it should have printed Not equal
Only when I inserted space around = it worked as intended
if [ "$var1" = "$var2" ]
and printed Not equal
Why is it so? Why "$var1"="$var2" is not same as "$var1" = "$var2"?
Moreover, when I wrote if [ "$var1"= "$var2" ], it gave
line 4: [: Test 1=: unary operator expected
What does it it mean? How come its expecting unary operator?
test (or [ expr ]) is a builtin function. Like all functions in bash, you pass its arguments as whitespace separated words.
As the man page for bash builtins states: "Each operator and operand must be a separate argument."
It's just the way bash and most other Unix shells work.
Variable assignment is different.
In bash a variable assignment has the syntax: name=[value]. You cannot put unquoted spaces around the = because bash would not interpret this as the assignment you intend. bash treats most lists of words as a command with parameters.
E.g.
# call the command or function 'abc' with '=def' as argument
abc =def
# call 'def' with the variable 'abc' set to the empty string
abc= def
# call 'ghi' with 'abc' set to 'def'
abc=def ghi
# set 'abc' to 'def ghi'
abc="def ghi"
When the shell reads
if [ "$var1" = "$var2" ]
it invokes the command [ with 4 arguments. Whether [ is a builtin or an external command is irrelevant, but it may help to understand that it may be the external command /bin/[. The second argument is the literal '=' and the fourth is ']'. However, when the shell reads
if [ "$var1"= "$var2" ]
[ only gets 3 arguments: the expansion of $var1 with '=' appended, the expansion of $var2, and ']'. When it gets only 3 arguments, it expects the last argument to be ']' and the first argument to be a unary operator.
To add to the existing explanation, "$var1"="$var2" is just a single non-empty string, and thus always evaluates as true in a conditional.
[ "$var1"="$var2" ] && echo true
The above command will always print out true (even if var1 and var2 be empty).
In bash the best is to use [[ ]]:
x="test"
y="test"
if [[ "${x}" = "${y}" ]]; then
echo "Equals"
else
echo "No equals"
fi

Resources