I'm using Bash on Linux and found an example where I compared strings this way but it doesn't seem to work.
In this case, $a
it is what the user types into the console.
#!/bin/bash
a=$1
if [ "${a}"=="static" ]; then
(instrucciones)
else
(instrucciones)
fi
You can do
help test
for more information:Empty string:
equal strings
different chains
In Bash you can compare strings using the syntax:
Note that you
$variable
don't need to use double quotes here.Here, as well
[[
as==
Bash's own features (what we call Bashisms ). The standard (that is, what POSIX defines) is to use[
and=
:In which case the variable must be enclosed in quotes to protect us from word splitting .
Note that the syntax is of the form:
That is, the spaces around
[
are necessary, since it[
is a command in itself. This command is calledtest
and therefore you can use[
andtest
interchangeably, as well as search for its instructions by doingman test
(as indicated by Trauma in his answer):Additional explanation regarding the use of double quotes within
[
and[[
.When we use
[
, the variables are expanded causing errors if we don't put double quotes:With
[
and quotes, the expansion is correct:If we remove the quotes, it gives an error:
Well, it's expanding to:
giving 5 arguments to
test
instead of the maximum 3 accepted.On the other hand, with Bashism
[[
you can omit the quotes:Examples:
each indicates what it does, it is easy to understand.