What are [] in bash?

4

I'm starting in shell script now, and often reading other people's programs, I see the use of [] associated with if . For example:

if [condition]; then

fi

I have also often seen use as follows:

if [[condition]]; then

fi

I still do not quite understand the purpose of these characters ... I wanted to understand exactly how this works and when to use it in my scripts.

Does anyone know how to explain it to me?

    
asked by anonymous 30.08.2016 / 21:49

3 answers

3

Both [ and [[ " are used to evaluate what is between [..] or [[..]] , you can compare strings and check the file types.

[ is a synonym for test , [[ is a keyword .

By doing if [ condicao ] you can say equivalent to if test condicao , because [ is a token that will invoke the command test .

The right bracket ] is not strictly required, however in newer versions of Bash is required.

  
~$ type test
test is a shell builtin
~$ type '['
[ is a shell builtin
~$ type '[['
[[ is a shell keyword
~$ type ']]'
]] is a shell keyword
~$ type ']'
bash: type: ]: not found

[[..]] is more flexible than [..] , while using [[..]] you can avoid some logic errors in script . For example, the && , || , < , and > operators operate on [[..]] , but not [..] .

See an example:

if [ -f $foo && -f $bar && -f $baz ]          # Erro

if [ -f $foo ] && [ -f $bar ] && [ -f $baz ]  # OK. Cada expressão dentro de um [..] 
if [ -f $foo -a -f $bar -a -f $baz ]          # OK. Tem que usar "-a" na antes da expressão

if [[ -f $foo && -f $bar && -f $baz ]]        # OK!

The difference and when and which to use between [ and [[ according to BashFAQ - 031 is:

  

To cut a long story short: test implements the old, portable syntax of   the command. In almost all shells (the oldest Bourne shells are the   exception), [ is a synonym for test (but requires a final argument of    ] ).

     Although all modern shells have built-in implementations of [ ,   there is still an external executable of that name, e.g. /bin/[ .

     

[[ is a new improved version of it, which is a keyword, not a program.   This has beneficial effects on the ease of use, as shown below.

     

[[ is understood by KornShell and BASH (eg 2.03 ), but not by the older POSIX or Bourne shell .

     

[...]

     

When should the new test command [[ be used, and when the old one [ ?

     

If portability to the BourneShell is a concern, the old syntax should   be used. If on the other hand the script requires BASH or KornShell ,   the new syntax is much more flexible.

Although [ and [[ have a lot in common and share many expression operators like -f , -s , -n , -z , there are some notable differences.

Here is a comparison list:

Credits: BashFAQ - 031

    
30.08.2016 / 23:30
2

In summary: [] works the same as () .

A good explanation of this can be seen at Programming in shell script .

  

Flow control are commands that will test some alternatives,   and according to these alternatives, execute commands. One of the   most commonly used flow control commands is certainly the if.

if [ -e $linux ]
then
  echo 'A variável $linux existe.'
else
  echo 'A variável $linux não existe.'
fi
     

If test the following expression: If the $ linux variable exists, then   (then) it says that it exists with echo, if not (else), it says   does not exist. The -e operator is predefined, and you can find the   list of operators in the table:

     

Reference: Programming in shell script

    
30.08.2016 / 22:26
1

This is a condition test.

For example:

if [ -e arq.txt ]; then
   echo Arquivo existe
else
   echo Arquivo nao existe
fi

will display the message "File exists" or the message "File does not exist" depending on whether the file "arq.txt" exists in the current directory.

Double bracket can also be used:

if [[ -e arq.txt ]]; then
   echo Arquivo existe
else
   echo Arquivo nao existe
fi

But there are some differences between single bracket and double bracket. Roughly, the double bracket has more features.

To see the differences between using simple bracket and double bracket, the best thing is to see the documentation (man bash) or to do a search on the internet, there will be a lot (but almost everything in English).

    
30.08.2016 / 22:28