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.

Filter by
Sorted by
Tagged with
253
votes
6answers
96k views

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 ...
86
votes
4answers
35k views

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 ...
71
votes
12answers
163k views

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!" ...
65
votes
3answers
101k views

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 ...
51
votes
5answers
8k views

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 ...
44
votes
7answers
9k views

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" ...
42
votes
1answer
112k views

Bash if statement [: missing `]' error [duplicate]

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 ...
40
votes
2answers
91k views

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 ...
35
votes
3answers
3k views

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 ...
31
votes
6answers
117k views

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)...
29
votes
8answers
80k views

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 /*...
27
votes
3answers
6k views

What is the purpose of square bracket executable

I see there is an executable called "[" in /usr/bin. What is its purpose?
27
votes
2answers
1k views

Error in shell bracket test when string is a left-parenthesis

I used to be confident about the fact that quoting strings is always a good practice in order to avoid having the shell parsing it. Then I came across this: $ x='(' $ [ "$x" = '1' -a "$y" = '1' ] ...
27
votes
4answers
79k views

Testing if a variable is empty in a shell script

I have seen the following technique used many times on many different shells, to test if a variable is empty: if [ "x$1" = "x" ]; then # Variable is empty fi Are there any advantages on using ...
24
votes
4answers
217k views

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 ...
24
votes
5answers
72k views

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 ...
23
votes
5answers
2k views

Why use double quotes in a [[ ]] test?

Let's say we have 2 integers in a bash script: value1=5 value2=3 Then why do we need to use double quotes in case of a test ? For example: if [[ "$value1" -eq "$value2" ]] Why not just use the ...
23
votes
4answers
11k views

Why does while [ 0 ] go into infinite loop?

I see the same behaviour for below loop as the loop with while [ 1 ]. Why is that so? while [ 0 ]; do echo "hello" done
22
votes
3answers
8k views

Does the syntax of not equal matter?

When scripting, I usually write my ifs with the following syntax as it is easier for me to understand that what comes next is not true. if [ ! "$1" = "$2" ]; then Others say that the way below is ...
22
votes
3answers
129k views

Comparing two strings in Bash

I have the following if block in my bash script: if [ ${PACKAGENAME} -eq kakadu-v6_4-00902C ]; then echo "successfully entered if block!!" fi The script execution is not entering my if block even ...
22
votes
3answers
14k views

Is there any major difference when comparing a variable as a string or as an int

Out of curiosity, when doing a bash variable comparison (its value being an integer) it's possible to test it against some predefined value either declared as an int or as a string. Sample script: #!...
21
votes
4answers
16k views

unix test when to use eq vs = vs == in test commands?

When should I use -eq vs = vs == e.g. [[ $num -eq 0 ]] [[ $num = 'zzz' ]] I've observed a pattern of using -eq (and -ne, etc.) for numbers and = for strings. Is there a reason for this and when ...
20
votes
2answers
786 views

Bracket expression (without ranges) matching unexpected character in bash

I'm using bash on Linux. I am getting a success from the following if statement, but shouldn't this return a fail code? if [[ ■ = [⅕⅖⅗] ]] ; then echo yes ; fi The square does NOT equal any of the ...
20
votes
4answers
21k views

Test command in unix doesn't print an output

When I type this in the terminal test 4 -lt 6 I don't get any output. Why not? I need that 0 or 1
19
votes
3answers
48k views

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 ...
16
votes
3answers
45k views

How to test if a variable is defined at all in Bash prior to version 4.2 with the nounset shell option?

For Bash versions prior to "GNU bash, Version 4.2" are there any equivalent alternatives for the -v option of the test command? For example: shopt -os nounset test -v foobar && echo foo || ...
15
votes
3answers
5k views

Brackets in if condition: why am I getting syntax errors without whitespace?

I am using the below script to move two days back when script runs at starting two days of the year and also check first and second days of every month and move two days back. if [$month="01"] &&...
14
votes
3answers
3k views

bash: I broke [[ < ]]

I was writing a bash script and suddenly this behaviour started: [[ 1 < 2 ]]; echo $? # outputs 0 [[ 2 < 13 ]]; echo $? # outputs 1 but -lt works soundly: [[ 1 -lt 2 ]]; echo $? # outputs 0 ...
13
votes
2answers
560 views

`test` and `[` - different binaries, any difference?

I noticed while answering another question that test and [ are different binaries, but the [ manpage pulls up test's. Besides the requirement for an ending ], is there any difference? If not, why are ...
13
votes
1answer
752 views

[[ and case equivalency in bash

Does if [[ "$1" = pattern ]]; then hook fi always behave the same as case "$1" in pattern) hook;; esac or are there any gotchas?
12
votes
3answers
4k views

What does [ -t 1 ] check?

I just found a way to start zsh when I start the bash on Windows from https://www.howtogeek.com/258518/how-to-use-zsh-or-another-shell-in-windows-10/. It recommended to add following code at the ...
11
votes
3answers
1k views

What is the difference of -a and -e in bash's conditional expressions?

From man bash: CONDITIONAL EXPRESSIONS [...] -a file True if file exists. [...] -e file True if file exists. So what is the difference between [ -a $FILE ] ...
11
votes
3answers
875 views

Performing -nt/-ot test in a POSIX sh

The built-in test and [ utilities have the -nt ("newer than") and -ot ("older than") tests in most shells, even when the shell is running in "POSIX mode" (also true for the external utilities of the ...
11
votes
1answer
640 views

never use the `-a` or `-o` operator with `[`

Stéphane Chazelas wrote: a few rules like always quote variables never use the -a or -o operator (use several [ commands and the && and || shell operators) Make [ reliable ...
10
votes
3answers
5k views

Why is a file with 400 permissions seen writable by root but read-only by user?

If I create a file as an unprivileged user, and change the permissions mode to 400, it's seen by that user as read-only, correctly: $ touch somefile $ chmod 400 somefile $ [ -w somefile ] && ...
10
votes
4answers
27k views

Why is the 'if [ $1=“1” ]' branch always selected even if $1 is not 1?

I have a shell script named 'teleport.sh' like this: if [ $1="1" ]; then shift mv "$@" ~/lab/Sun elif [ $1="2" ]; then shift mv "$@" ~/lab/Moon elif [ $1="3" ]; ...
10
votes
3answers
3k views

Schedule the last day of every month

I read from an instruction to schedule a script on the last day of the month: Note: The astute reader might be wondering just how you would be able to set a command to execute on the last day of ...
10
votes
5answers
32k views

How do I use an If-Else query based on the day of the week?

Problem: I need to check if today is Thursday and perform different actions based on the result of this condition. I tried two different approaches: Getting the Day Name: DAYOFWEEK=$(date +"%a") ...
10
votes
1answer
10k views

How do I test if a file does not exist using ZSH? [duplicate]

This question is about ZSH, not bash. I have the following lines in my .zshrc file. Whenever I open a terminal I get a no matches found error referencing the line with the if statement. if [[!( -a ~...
9
votes
2answers
1k views

Why does this bash conditional check work with [[ -n .. ]] but not [ -n .. ]? [duplicate]

I have a script that fails to detect zero length strings,the script uses [ -n $value ] in a bash conditional expression, i.e. #!/usr/bin/env bash value="" if [ -n $value ] then echo "value is ...
9
votes
3answers
13k views

String comparison in single brackets in zsh [duplicate]

Bash code to print all folders: for f in ~/*; do if [ $f == '/home/sk/.' -o $f == '/home/sk/..' ]; then true else echo "$f" fi done It works on bash. When i ran the code on z shell,...
9
votes
3answers
13k views

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 ...
9
votes
2answers
744 views

POSIX test and -a

I checked a script of mine with checkbashisms and I got the following warnings: possible bashism in check_ssl_cert line 821 (test -a/-o): if [ -n "${ALTNAMES}" -a -n "${COMMON_NAME}" ] ; then In ...
9
votes
3answers
26k views

waiting for network in a bash script

I'm running a script that relies on network being up and a network share be mounted. The script runs on login (which happens automatically after boot). The problem is that by the time the script runs, ...
8
votes
2answers
2k views

Why do [[ -z ]] and [[ -v ]] have different syntax?

If I want to perform some commands given variables aren't set I'm using: if [[ -z "$a" || -z "$v" ]] then echo "a or b are not set" fi Yet the same syntax doesn't work with -v, I have to use: if [...
8
votes
4answers
2k views

Why does `==` behave differently inside `[ … ]` in zsh and bash?

I get what I expected when doing this in bash: [ "a" == "a" ] && echo yes It gave me yes. But when I do this in zsh, I get the following: zsh: = not found Why does the same command (/usr/...
8
votes
3answers
613 views

What does `[ EXPRESSION ], [ ] and [OPTION` mean in `man test`?

Examining man test, I see that under synopsis for test are the possibilities test EXPRESSION and test. What does [ EXPRESSION ], [ ] and [OPTION mean below ? Why are the brackets empty and why does ...
8
votes
2answers
789 views

Difference between [[ $variable ]] and [[ -n $variable ]]?

In bash, are [[ $variable ]] and [[ -n $variable ]] completely equivalent? It appears to be the case judging by the output below, but I see both forms of usage prevalent in shell scripts. $ z="abra" $...
7
votes
2answers
4k views

Why are bash tests so picky about whitespace?

As a primarily Java programmer, I find the bash if-then construct quite confusing, especially regarding whitespace. Can anyone explain why the first one works, but not the second or third? #works if [...
7
votes
5answers
622 views

“test -n && echo not empty” prints “not empty”. Is that expected behaviour?

When I execute the following line test -n && echo not empty it prints "not empty". This occurs when you have an empty variable, and test for non-emptyness while forgetting to quote the value:...