Hot answers tagged

628 votes

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

In Bourne-like shells, an if statement typically looks like if command-list1 then command-list2 else command-list3 fi The then clause is executed if the exit code of the command-list1 list ...
John1024's user avatar
  • 74.4k
157 votes

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

(…) parentheses indicate a subshell. What's inside them isn't an expression like in many other languages. It's a list of commands (just like outside parentheses). These commands are executed in a ...
Gilles 'SO- stop being evil''s user avatar
122 votes

Bash test: what does "=~" do?

The ~ is actually part of the operator =~, which performs a regular expression match of the string to its left to the extended regular expression on its right. [[ "string" =~ pattern ]] ...
Kusalananda's user avatar
  • 331k
70 votes

These two Bash lines are functionally equivalent. Can someone explain why one is faster?

time always times the directly following pipeline. A pipeline is a sequence of one or more commands (simple or compound) separated by one of the control operators | or |&. if ... fi is a single ...
pLumo's user avatar
  • 22.5k
66 votes
Accepted

How exactly does "/bin/[" work?

The [ command's job is to evaluate test expressions. It returns with a 0 exit status (that means true) when the expression resolves to true and something else (which means false) otherwise. It's not ...
Stéphane Chazelas's user avatar
59 votes

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

[ vs [[ This answer will cover the [ vs [[ subset of the question. Some differences on Bash 4.3.11: POSIX vs Bash extension: [ is POSIX [[ is a Bash extension inspired from Korn shell regular ...
Ciro Santilli OurBigBook.com's user avatar
56 votes
Accepted

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

I don't know if it is beautiful, but it is working for every version format I know. #!/bin/bash currentver="$(gcc -dumpversion)" requiredver="5.0.0" if [ "$(printf '%s\n' &...
Luciano Andress Martini's user avatar
52 votes
Accepted

Why does < (less than) not give the same result as -lt in bash [[ ]]?

From the bash man page. When used with [[, the < and > operators sort lexicographically using the current locale. From the output, it appears to be working as designed. In bash use: [[ $a <...
steve's user avatar
  • 21.9k
43 votes

How exactly does "/bin/[" work?

I am always surprised that in the folder /bin there is a [ program. You are right to be surprised. That's one of the rare POSIX commands, with the null utility (:), that doesn't respect the commands ...
jlliagre's user avatar
  • 61k
39 votes
Accepted

Does the syntax of not equal matter?

Beside the cosmetic/preference arguments, one reason could be that there are more implementations where [ ! "$a" = "$b" ] fails in corner cases than with [ "$a" != "$b" ]. Both cases should be safe ...
Stéphane Chazelas's user avatar
38 votes
Accepted

What is the purpose of square bracket executable

In most cases, [ is a shell builtin and is equivalent to test. However, like test, it also exists as a standalone executable: that's the /bin/[ you saw. You can test this with type -a [ (on an Arch ...
terdon's user avatar
  • 241k
38 votes

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

You don't actually need the quotes here. This is one of the very few cases where it is safe to use a variable unquoted. You can confirm this with set -x: $ var1="" $ var2="3" $ set -x $ if [[ $var1 -...
terdon's user avatar
  • 241k
36 votes
Accepted

Bash - Integer expression expected

The test command, also named [, has separate operators for string comparisons and integer comparisons: INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2 vs STRING1 = STRING2 the strings are equal ...
Jeff Schaller's user avatar
  • 67.1k
31 votes

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

I think you have have misunderstood what -w does. It does not check to see if the file has "Write permissions", it checks to see if the file is writable by the invoking user. More specifically, it ...
Stephen Harris's user avatar
30 votes
Accepted

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

It's not /usr/bin/[ in either of the shells. In Bash, you're using the built-in test/[ command, and similarly in zsh. The difference is that zsh also has an = expansion: =foo expands to the path to ...
Michael Homer's user avatar
29 votes
Accepted

Bash negation of -a (file exists) does not change result, whereas for ! -e changes result

-a is both a unary (for accessible, added for compatibility with the Korn shell, but otherwise non-standard and now redundant with -e) and binary (for and, in POSIX (with XSI) but deprecated there) ...
Stéphane Chazelas's user avatar
28 votes
Accepted

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

Test operators -v and -z are just not the same. Operator -z tells if a string is empty. So it is true that [[ -z "$a" ]] will give a good approximation of "variable a is unset", but not a perfect one:...
dhag's user avatar
  • 15.7k
27 votes

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

From the bash documentation: (list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's ...
Alexis Wilke's user avatar
  • 2,807
27 votes
Accepted

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

test -w aka [ -w doesn't check the file mode. It checks if it's writable. For root, it is. $ help test | grep '\-w' -w FILE True if the file is writable by you. The way I would test would ...
phemmer's user avatar
  • 71.6k
25 votes

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

This is a very obscure corner case that one might consider a bug in how the test [ built-in is defined; however, it does match the behaviour of the actual [ binary available on many systems. As far as ...
Nominal Animal's user avatar
24 votes
Accepted

What does [ -t 1 ] check?

[] is shortcut of test command. According to man test: -t FD True if FD is a file descriptor that is associated with a terminal. So if you running bash as interactive shell (terminal - see ...
mrc02_kr's user avatar
  • 2,003
20 votes

Comparing integers: arithmetic expression or conditional expression

Historically, the test command existed first (at least as far back to Unix Seventh Edition in 1979). It used the operators = and != to compare strings, and -eq, -ne, -lt, etc. to compare numbers. For ...
Gilles 'SO- stop being evil''s user avatar
19 votes

Checking if an input number is an integer

As the OP seems to want only positive integers: [ "$1" -ge 0 ] 2>/dev/null Examples: $ is_positive_int(){ [ "$1" -ge 0 ] 2>/dev/null && echo YES || echo no; } $ is_positive_int word ...
Tom Hale's user avatar
  • 30.1k
19 votes

Shell logical not

portability consideration. The ! keyword is POSIX but not Bourne while ! has been supported by the [/test command from the start. So [ ! ... ] is more portable than ! [ ... ]. Otherwise, as long as ...
Stéphane Chazelas's user avatar
19 votes
Accepted

if condition always evaluating true

The operators -eq and -ne are arithmetic operators for comparing numerical data. What you want here is == and !=: if [[ "${fin[2]}" == 'OK' ]] && [[ "${fin[7]}" == 'NA' ]]; then and ...
Kusalananda's user avatar
  • 331k
19 votes
Accepted

What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?

The important thing to understand here is that in most shells¹, [ is just an ordinary command parsed by the shell like any other ordinary command. Then the shell invokes that [ (aka test) command with ...
Stéphane Chazelas's user avatar
18 votes

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

Some examples: Traditional test: foo="some thing" # check if value of foo is not empty if [ -n "$foo" ] ; then... if test -n "$foo" ; then... test and [ are commands like any others, so the ...
ilkkachu's user avatar
  • 138k
18 votes
Accepted

Is it possible to have conditionals in /etc/rc.local?

The [[ ... ]] syntax isn't valid for /bin/sh. Try: if [ -e /usr/src/an-existing-file ] then echo "seen" >> /etc/rclocalmadethis fi Note that sometimes it works because /bin/sh -&...
Andy Dalton's user avatar
  • 13.9k
17 votes

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

A possible solution is also Bash builtin compgen. That command returns all possible matches for a globbing pattern and has an exit code indicating whether any files matched. compgen -G "/*.text" > ...
Daniel Böhmer's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible