What are [ ] bash for?

Asked

Viewed 1,915 times

4

I’m starting 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 often seen the use as follows:

if [[condition]]; then

fi

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

Can someone explain to me?

3 answers

4


Both [ and [[ are used to assess what is between [..] or [[..]], can be compared strings and check file types.

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

In doing if [ condicao ] can be said to be equivalent to if test condicao, for [ is a token that will invoke the command test.

The right bracket ], is not strictly necessary, however in more recent 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 [..], when using [[..]] some logical errors can be avoided in the script. For example, the operators &&, ||, <, and > work in the [[..]], but not in [..].

Take 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 the 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 usually still is 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 (e. g. 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 much in common and share many expression operators such as -f, -s, -n, -z, there are some notable differences.

Here is a comparison list:

inserir a descrição da imagem aqui

Credits: Bashfaq - 031

2

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

A good explanation about this can be seen on the site Programming in shell script.

Flow control are commands that test some alternatives, and according to these alternatives, they execute commands. One of the most 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 tests the following expression: If the $linux variable exists, then (then) he says that which exists with the echo, if not (Else), he says that does not exist. The -e operator is predefined, and you can find the list of operators in the table:

inserir a descrição da imagem aqui

Reference: Programming in shell script

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 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 and double bracket. Roughly, the double bracket has more features.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.