Questions tagged [test]

This is about the Unix utility "test," also invoked as "[", or its shell syntax [[ … ]] variant. For questions about testing software and setups, use the "testing" tag.

The test, or [, utility is used in the shell and shell scripts to evaluate conditions, usually for conditional execution of code.

Ksh, bash and zsh also have a dedicated syntax [[ … ]].

Further reading

358 questions
534
votes
7 answers

What is the difference between the Bash operators [[ vs [ vs ( vs ((?

I am a little bit confused on what do these operators do differently when used in bash (brackets, double brackets, parenthesis and double parenthesis). [[ , [ , ( , (( I have seen people use them on if statements like this : if [[condition]] if…
RetroCode
  • 5,489
  • 3
  • 10
  • 8
123
votes
4 answers

Why does parameter expansion with spaces without quotes work inside double brackets "[[" but not inside single brackets "["?

I'm confused with using single or double brackets. Look at this code: dir="/home/mazimi/VirtualBox VMs" if [[ -d ${dir} ]]; then echo "yep" fi It works perfectly although the string contains a space. But when I change it to single…
Majid Azimi
  • 3,018
  • 6
  • 31
  • 37
117
votes
12 answers

How do I check if a variable exists in an 'if' statement?

I need to check a variable's existence in an if statement. Something to the effect of: if [ -v $somevar ] then echo "Variable somevar exists!" else echo "Variable somevar does not exist!" And the closest question to that was this, which…
user104976
111
votes
3 answers

How do I check if a file is a symbolic link to a directory?

I can check, if a file exists and is a symbolic link with -L for file in *; do if [[ -L "$file" ]]; then echo "$file is a symlink"; else echo "$file is not a symlink"; fi done and if it is a directory with -d: for file in *; do if [[ -d…
rubo77
  • 27,777
  • 43
  • 130
  • 199
96
votes
2 answers

Bash test: what does "=~" do?

#!/bin/bash INT=-5 if [[ "$INT" =~ ^-?[0-9]+$ ]]; then echo "INT is an integer." else echo "INT is not an integer." >&2 exit 1 fi What does the leading ~ do in the starting regular expression?
ragnarok
  • 969
  • 1
  • 7
  • 3
70
votes
11 answers

Checking if an input number is an integer

I'm trying to check if an input is an integer and I've gone over it a hundred times but don't see the error in this. Alas it does not work, it triggers the if statement for all inputs (numbers/letters) read scale if ! [[ "$scale" =~ "^[0-9]+$" ]] …
lonewarrior556
  • 2,033
  • 4
  • 16
  • 13
66
votes
1 answer

Bash if statement [: missing `]' error

I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string. This works perfectly. echo $(ipcs | grep Shared | awk '{print $2}') When I put it in an if statement I get some problems. $ if [ "$(ipcs…
cokedude
  • 1,081
  • 4
  • 11
  • 16
52
votes
5 answers

How exactly does "/bin/[" work?

I am always surprised that in the folder /bin there is a [ program. Is this what is called when we are doing something like: if [ something ]? By calling the [ program explicitly in a shell it asks for a corresponding ], and when I provide the…
Bregalad
  • 995
  • 3
  • 10
  • 14
48
votes
7 answers

Is test or [ or [[ more portable both between bash shells and between other shells?

I see I can do $ [ -w /home/durrantm ] && echo "writable" writable or $ test -w /home/durrantm && echo "writable" writable or $ [[ -w /home/durrantm ]] && echo "writable" writable I like using the third syntax. Are they equivalent in all ways and…
Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
44
votes
3 answers

Comparing integers: arithmetic expression or conditional expression

In Bash, two integers can be compared using conditional expression arg1 OP arg2 OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to,…
Tim
  • 98,580
  • 191
  • 570
  • 977
43
votes
13 answers

Test if there are files matching a pattern in order to execute a script

I am trying to write an if statement to test whether there are any files matching a certain pattern. If there is a text file in a directory it should run a given script. My code currently: if [ -f /*.txt ]; then ./script fi Please give some ideas;…
user40952
  • 467
  • 1
  • 5
  • 5
39
votes
3 answers

What is the difference between [[ $a == z* ]] and [ $a == z* ]?

Is there is any difference between these two. [[ $a == z* ]] and [ $a == z* ] Can I have an example where they would have different outputs? Furthermore, how does the working of [[ ]] differs from [ ]?
munish
  • 7,825
  • 24
  • 71
  • 97
35
votes
7 answers

How can I use bash's if test and find commands together?

I have a directory with crash logs, and I'd like to use a conditional statement in a bash script based on a find command. The log files are stored in this format: /var/log/crashes/app-2012-08-28.log /var/log/crashes/otherapp-2012-08-28.log I want…
cwd
  • 44,479
  • 71
  • 146
  • 167
33
votes
4 answers

Starting with bash: -lt and -gt arguments

I'm starting with bash and I found the following: if test $first -lt $second then echo $first is lower than $second else if test $first -gt $second then echo $first is higher than $second else echo $first and $second are equals …
user47579
  • 459
  • 1
  • 5
  • 5
32
votes
9 answers

How to compare a program's version in a shell script?

Suppose I want to compare gcc version to see whether the system has the minimum version installed or not. To check the gcc version, I executed the following gcc --version | head -n1 | cut -d" " -f4 The output was 4.8.5 So, I wrote a simple if…
Abhimanyu Saharan
  • 853
  • 4
  • 15
  • 24
1
2 3
23 24