Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: double vs single brackets in file test expression evaluation

Tags:

bash

shell

I have a question about file test expressions in bash. Here is a simple script to illustrate my question:

set -x
read -p "Enter a filename: " var1
if [ - e $var1 ]
then
    echo file exists
else
    echo file not found
fi

There are three scenarios:

  1. At the prompt, I enter foo , which is a file that exists in the directory from which I'm running the script. As expected, the output is file exists .
  2. At the prompt, I enter bar . No such file exists in the directory from which I'm running the script. As expected, the output is file not found .
  3. At the prompt, I hit <enter> without typing anything. Surprisingly, the output is file exists .

If I use if [[ -e $var1 ]] , i.e., double brackets instead of single, the behavior is correct: even in the third case, I get file not found.

I stuck a set -x at the top of the file to see what was going on. With single brackets, the variable is evaluated as: '[' -e ']' . With double, it is evaluated as [[ -e '' ]] . This is interesting. Why is the expression being evaluated differently in the two cases?

I would be grateful for an explanation. Sorry if I'm missing the obvious. Thanks!

like image 689
verbose Avatar asked Apr 28 '12 04:04

verbose


People also ask

What is the difference between single and double square brackets in Bash?

In this article, we discussed the differences between single and double brackets in Bash. The single bracket is a built-in command that's older than the double bracket. The double bracket is an enhanced version of the single bracket. If we want the portability of scripts, we should prefer single brackets.

What does double bracket mean in Bash?

Double brackets in bash are not a command but a part of the language syntax. This means they can react more tolerantly to 'disappearing' arguments: $ [[ $a = $b ]] || echo "unequal" unequal.

What does double brackets mean in Linux?

The double bracket is a “compound command” where as test and the single bracket are shell built-ins (and in actuality are the same command). Thus, the single bracket and double bracket execute different code. The test and single bracket are the most portable as they exist as separate and external commands.

What do the single brackets mean in Bash when used with a conditional statement?

Single and Double Brackets Bash provides the test command. This lets you test logical expressions. The expression will return an answer that indicates a true or false response. A true response is indicated by a return value of zero.


1 Answers

change to if [ -e "$var1" ]

You can find more details of [] and [[ ]] Here

like image 142
dpp Avatar answered Sep 20 '22 12:09

dpp