Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

36 votes
12 answers
56155 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 The output was 4.8.5 So, I wrote a simple `if` statement to check this version against some other...
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 statement to check this version against some other value if [ "$(gcc --version | head -n1 | cut -d" " -f4)" -lt 5.0.0 ]; then echo "Less than 5.0.0" else echo "Greater than 5.0.0" fi But it throws an error: [: integer expression expected: 4.8.5 I understood my mistake that I was using strings to compare and the -lt requires integer. So, is there any other way to compare the versions?
Abhimanyu Saharan (911 rep)
May 27, 2016, 02:01 PM • Last activity: Aug 5, 2025, 10:58 AM
2 votes
1 answers
133 views
Semicolon in conditional structures after the closing double bracket in a bash/zsh script?
Continuing https://unix.stackexchange.com/questions/48805/semicolon-in-conditional-structures (which handles single brackets), what's the point of having a semicolon after the closing DOUBLE bracket `]]`? In my tests, running ```zsh #!/bin/zsh -- if [[ "a" == "a" ]] then echo "true" else echo "false...
Continuing https://unix.stackexchange.com/questions/48805/semicolon-in-conditional-structures (which handles single brackets), what's the point of having a semicolon after the closing DOUBLE bracket ]]? In my tests, running
#!/bin/zsh --
if [[ "a" == "a" ]] then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "a" ]]; then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "a" ]]
then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "a" ]];
then
	echo "true"
else
	echo "false"
fi
yields
true
true
true
true
, and running
#!/bin/zsh --
if [[ "a" == "b" ]] then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "b" ]]; then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "b" ]]
then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "b" ]];
then
	echo "true"
else
	echo "false"
fi
yields
false
false
false
false
No error is reported. In zsh, what's the difference between the conditionals with a semicolon ; after the closing double bracket and the conditionals without a semicolon after the closing double bracket? The same question goes for bash.
user743115 (1 rep)
Jul 26, 2025, 12:42 AM • Last activity: Jul 26, 2025, 06:12 PM
2 votes
1 answers
65 views
Why can't I have a single quote in a bash associative array index when testing with `test -v`
When I run the following code ```bash declare -A X # get an associative array index="a'b" # a somewhat weird index X[$index]="this is set" echo " >" if test -v X[$index]; then # behaves in an unexpected way echo indeed, this is set else echo no, it is not fi ``` the result is `no, it is not` in my b...
When I run the following code
declare -A X # get an associative array
index="a'b" # a somewhat weird index
X[$index]="this is set"
echo ">"
if test -v X[$index]; then # behaves in an unexpected way
  echo indeed, this is set
else
  echo no, it is not
fi
the result is no, it is not in my bash version 5.1.16(1). As soon as i remove the single quote from the index, it works as expected, telling that indeed, this is set. From the bash manual : > Indexed arrays are referenced using integers [...]; associative arrays use arbitrary strings. Given that I can properly use the index to set and retrieve the value, I wonder if this is a bug in test -v or if there is some explanation why this does not work or whether different quoting can get test -v to work. (I could use test -z in this particular case, but the question is about -v).
Harald (1030 rep)
Apr 24, 2025, 12:43 PM • Last activity: Apr 24, 2025, 05:29 PM
0 votes
2 answers
94 views
How to stop a running memtester (without risking to have to reboot)?
*Extremely "noob" question:* I have a running `sudo memtester ...` on an Ubuntu 22.04 machine, but I gave it too much memory to test and it's taking [too much time](https://unix.stackexchange.com/q/789492/515980) (I see it running and updating, I made sure the RAM given to test was less than the fre...
*Extremely "noob" question:* I have a running sudo memtester ... on an Ubuntu 22.04 machine, but I gave it too much memory to test and it's taking [too much time](https://unix.stackexchange.com/q/789492/515980) (I see it running and updating, I made sure the RAM given to test was less than the free one). I'd like to stop it. I was thinking of trying with a simple Ctrl-c, but I wonder if that would cause some sort of lock-up, forcing me to reboot. Unfortunately the [man pages](https://man.archlinux.org/man/memtester.8.en) don't say anything about how to stop it.
pglpm (142 rep)
Apr 13, 2025, 12:39 PM • Last activity: Apr 13, 2025, 12:55 PM
8 votes
3 answers
1169 views
Unary test -v on an array element
In bash, the conditional expression with the unary test `-v myvariable` tests wether the variable `myvariable` has been set. Note that `myvariable` should not be expanded by prefixing it with a dollar, so **not** `$myvariable`. Now I find that for array elements the conditional expression `-v myarra...
In bash, the conditional expression with the unary test -v myvariable tests wether the variable myvariable has been set. Note that myvariable should not be expanded by prefixing it with a dollar, so **not** $myvariable. Now I find that for array elements the conditional expression -v myarray[index] works well too, without the full expansion syntax ${myarray[$index]}. Try this: myarray=myvalue for i in 1 2 3 do [ -v myarray\[i] ] && echo element $i is set done (note the escape \[ to prevent globbing, as alternative to using quotes) gives the desired output: element 2 is set **Question** Is this behaviour safe to use *aka* is this documented behaviour? **Addendum** After reading the answer https://unix.stackexchange.com/a/677920/376817 of Stéphane Chazelas, I expanded my example: myarray=val myarray=val myarray=val myarray=val myarray=val myarray="" myarray="" unset myarray myarray myarray touch myarray4 myarrayi myarray4=val myarrayi=val then for i in {0..7}; do [ -v myarray\[i] ] && echo element $i is set; done gives element 1 is set element 2 is set element 6 is set Without quoting or escaping the index expression [i] : for i in {0..7}; do [ -v myarray[i] ] && echo element $i is set; done gives element 0 is set element 1 is set element 2 is set element 3 is set element 4 is set element 5 is set element 6 is set element 7 is set The same with the variable myarrayi unset : unset myarrayi for i in {0..7}; do [ -v myarray[i] ] && echo element $i is set; done gives %nothing% And finally with expansion of the index as $i (still without quoting or escaping the bracket) : for i in {0..7}; do [ -v myarray[$i] ] && echo element $i is set; done it gives element 1 is set element 2 is set element 4 is set because ls -l myarray* shows -rw-rw-r-- 1 me us 0 nov 17 15:37 myarray4 -rw-rw-r-- 1 me us 0 nov 17 15:37 myarrayi
db-inf (333 rep)
Nov 17, 2021, 11:01 AM • Last activity: Mar 15, 2025, 05:56 PM
0 votes
2 answers
151 views
How to check value inside a file using bash?
I'm trying to check if the value inside a file is "0". ``` COUNT_EXECUTION=$(< /tmp/count) if [ "$COUNT_EXECUTION" == "0" ]; then echo "Try restart service." else echo "There is execution in progress." fi ``` File content is just a `0`, but the test always falls into the else
I'm trying to check if the value inside a file is "0".
COUNT_EXECUTION=$(< /tmp/count)

if [ "$COUNT_EXECUTION" == "0" ]; then
  echo "Try restart service."
else
  echo "There is execution in progress."
fi
File content is just a 0, but the test always falls into the else
Aderbal Nunes (137 rep)
Feb 20, 2025, 11:54 AM • Last activity: Feb 21, 2025, 06:11 AM
592 votes
7 answers
314300 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 `if` statements like this: if [[ condition ]] if [ condition ] if ((condition)) if (condition)
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 [ condition ] if ((condition)) if (condition)
RetroCode (6069 rep)
Aug 27, 2016, 06:43 PM • Last activity: Jan 27, 2025, 07:40 PM
201 votes
3 answers
511398 views
Shell scripting: -z and -n options with if
I have a shell script where we have following lines `if [ -z "$xyz" ]` and `if [ -n "$abc" ]`, but I am not sure what their purpose is. Can anyone please explain?
I have a shell script where we have following lines if [ -z "$xyz" ] and if [ -n "$abc" ], but I am not sure what their purpose is. Can anyone please explain?
user3173953 (2119 rep)
Jan 16, 2014, 01:55 PM • Last activity: Jan 20, 2025, 11:48 AM
1 votes
1 answers
59 views
I fail to match the standard output of a posix shell function with the text I want to test it against
I want to test if the output of a function matches the text I envision it should write to standard output. But the code below never prints ```test PASS``` and I can't figure out why. I also tried putting various ```\n``` characters or deleting them in the variable ```GROUND_TRUTH``` but that didn't...
I want to test if the output of a function matches the text I envision it should write to standard output. But the code below never prints
PASS
and I can't figure out why. I also tried putting various
\n
characters or deleting them in the variable
but that didn't help.
error_out()
{
    printf "%s\n" "${ERROR}"
    printf "Return to exit\n"
    read -r THROW_AWAY
    exit
}

ERROR="Test output."
OUTPUT_TO_TEST=$(error_out)
GROUND_TRUTH="Test output.\nReturn to exit\n"
{ [ "${OUTPUT_TO_TEST}" = "${GROUND_TRUTH}" ] \
         && printf "error_out test PASS\n"; } \
    || { \
        printf "error_out test FAIL\n"; \
    }
branco (13 rep)
Dec 8, 2024, 09:25 PM • Last activity: Dec 9, 2024, 09:53 AM
1 votes
2 answers
695 views
Is my "escaping" wrong - or is it something else?
Here's what I'm trying to do (in a script): ```bash #!/usr/bin/env bash if [[ ! $("/usr/bin/scp seamus@fumier.local:/Users/seamus/Downloads/imgutils/image-utils* /home/pi/testscp") ]]; then printf "\nERROR & EXIT: 'scp' failed \n" exit 1 fi printf "success!\n" ``` I've tried doing this in a few ways...
Here's what I'm trying to do (in a script):
#!/usr/bin/env bash

if [[ ! $("/usr/bin/scp seamus@fumier.local:/Users/seamus/Downloads/imgutils/image-utils* /home/pi/testscp") ]]; then
    printf "\nERROR & EXIT: 'scp' failed \n"
    exit 1
fi
printf "success!\n"
I've tried doing this in a few ways, but nothing seems to work. Here's what I get from the above:
./testscp.sh: line 3: /usr/bin/scp seamus@fumier.local:/Users/seamus/Downloads/imgutils/image-utils* /home/pi/testscp: No such file or directory

ERROR & EXIT: 'scp' failed
The scp command runs OK when it's on a line by itself. If I do that, I can test for $0 - which works OK... but I'd prefer to do it all in a single line. What am I missing?
Seamus (3798 rep)
Nov 26, 2024, 09:20 AM • Last activity: Nov 26, 2024, 05:13 PM
32 votes
3 answers
9978 views
What is the purpose of square bracket executable
I see there is an executable called "[" in `/usr/bin`. What is its purpose? [![enter image description here][1]][1] [1]: https://i.sstatic.net/D3aUa.png
I see there is an executable called "[" in /usr/bin. What is its purpose? enter image description here
Alexandru Irimiea (601 rep)
Jan 22, 2016, 03:07 PM • Last activity: Jul 23, 2024, 08:47 AM
0 votes
1 answers
4579 views
Empty string is a file? ( if [ ! -f "" ] )
The script is called **isFile.sh** and looks like this: #!/bin/sh echo $1 echo $2 if [ ! -f $1 ]; then echo "$1 (arg1) is not a file" fi if [ ! -f $2 ]; then echo "$2 (arg2) is not a file" fi First I created a file by doing `touch file.exist`. And I ran `bash isFile.sh file.exist file.notexist ` The...
The script is called **isFile.sh** and looks like this:
#!/bin/sh

echo $1
echo $2

if [ ! -f $1 ]; then
  echo "$1 (arg1) is not a file"
fi

if [ ! -f $2 ]; then
  echo "$2 (arg2) is not a file"
fi
First I created a file by doing touch file.exist. And I ran bash isFile.sh file.exist file.notexist The output was: >file.exist > >file.notexist > >file.notexist (arg2) is not a file ---------------------------------------------------- Then I ran bash isFile.sh "" file.notexist The output was: >(# empty line) > >file.notexist > >file.notexist (arg2) is not a file Expected output is: >(# empty line) > >file.notexist > >(arg1) is not a file > >file.notexist (arg2) is not a file Can somebody explain why?
SoloKyo (41 rep)
Aug 7, 2018, 04:44 AM • Last activity: Jun 3, 2024, 10:33 AM
0 votes
0 answers
181 views
Linux From Scratch 12.1 GCC testsuite failing and timing out
I'm currently building Linux From Scratch 12.1 with the book, I've followed the instructions line by line. I'm struggling with the GCC testsuite, I have lots of FAIL and timeout. In addition to that, I'm running a Lubuntu VM with an LFS.vdi where I'm building LFS, during the test it is being filled...
I'm currently building Linux From Scratch 12.1 with the book, I've followed the instructions line by line. I'm struggling with the GCC testsuite, I have lots of FAIL and timeout. In addition to that, I'm running a Lubuntu VM with an LFS.vdi where I'm building LFS, during the test it is being filled to the point where the tests can't continue. My VM runs lubuntu 64bit with 4proc and 10Gb RAM. I have a 30Go drive dedicated to LFS. I've tried this for the "not enough space" problem but it didn't work: https://superuser.com/questions/529149/how-to-compact-virtualboxs-vdi-file-size I don't really know where to look, any help would be greatly appreciated. If I can access some helpful logs, let me know. Thanks in advance
noxen (1 rep)
May 24, 2024, 01:49 PM
0 votes
2 answers
1174 views
Test variable if its string or not
I'm writing a script which will have some arguments and so I am using `getopts` but i want to solve the problem with one argument. I use a switch, for example `-d`, and I want the argument for `-d` will be the path to a directory, like `./work`. I want to test the user's input for a string or path,...
I'm writing a script which will have some arguments and so I am using getopts but i want to solve the problem with one argument. I use a switch, for example -d, and I want the argument for -d will be the path to a directory, like ./work. I want to test the user's input for a string or path, not a number. Is there any solution to solve this problem? I want to solve it with something like: If (test) then echo it is string else echo it is not string
xpukm (13 rep)
Nov 11, 2013, 10:45 PM • Last activity: May 19, 2024, 11:13 AM
12 votes
1 answers
961 views
Why should one never use the `-a` or `-o` operators with `[`?
[St&#233;phane Chazelas][1] 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 with POSIX shells. Why "never use the `-a` or `-o` operator"? How can I do "use several `[...
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 with POSIX shells. Why "never use the -a or -o operator"? How can I do "use several [ commands and the && and || shell operators)"?
Tim (106422 rep)
Mar 19, 2016, 07:33 AM • Last activity: May 11, 2024, 11:03 AM
2 votes
2 answers
75 views
Bash complaints about missing ], while it is there
I have following in a bash script: echo "status: [$status] and message: [$message]" if [ "${status}" -eq 0 && "$message" = "ERROR" ]; then echo "Exit 0"; exit 0 fi # status: [0] message: [ERROR] However, it gives below error, and the logic under `then` i.e. `exit 0` never gets executed: line 2: [: m...
I have following in a bash script: echo "status: [$status] and message: [$message]" if [ "${status}" -eq 0 && "$message" = "ERROR" ]; then echo "Exit 0"; exit 0 fi # status: message: [ERROR] However, it gives below error, and the logic under then i.e. exit 0 never gets executed: line 2: [: missing `]' It seems to be complaining about the missing closing ], however, it is there. What am i missing here?
Nullpointer (199 rep)
Jan 9, 2024, 03:54 PM • Last activity: Jan 9, 2024, 04:09 PM
1 votes
1 answers
129 views
Why does -n with unquoted variable containing empty string return true?
From `man bash`: ``` -n string True if the length of string is non‐zero. ``` Examples: ``` # expected $ var=""; [ -n "$var" ]; echo $? 1 # unexpected? $ var=""; [ -n $var ]; echo $? 0 ``` Here we see that `-n` with unquoted variable containing empty string returns true. Why? Why is `$var` required t...
From man bash:
-n string
    True if the length of string is non‐zero.
Examples:
# expected
$ var=""; [ -n "$var" ]; echo $?
1

# unexpected?
$ var=""; [ -n $var ]; echo $?
0
Here we see that -n with unquoted variable containing empty string returns true. Why? Why is $var required to be quoted here?
pmor (665 rep)
Dec 28, 2023, 10:26 AM • Last activity: Dec 28, 2023, 04:35 PM
2 votes
2 answers
2524 views
Bash: Regex for comparing file path
I want to check if an input string matches whitelisted file paths, then only I want to perform some operation. I am doing something like this path1="mydir/**" path2="mydir/testing" [ [ "$path2" =~ "$path1" ] ] && echo "Matches" Where, if paths are like mydir/test/dir mydir/othertest/dir These should...
I want to check if an input string matches whitelisted file paths, then only I want to perform some operation. I am doing something like this path1="mydir/**" path2="mydir/testing" [ [ "$path2" =~ "$path1" ] ] && echo "Matches" Where, if paths are like mydir/test/dir mydir/othertest/dir These should pass. Above check is giving me an error assets/bin/copymain: line 8: [: too many arguments
Ankit (121 rep)
Jun 1, 2019, 04:26 PM • Last activity: Dec 5, 2023, 05:01 PM
31 votes
3 answers
42668 views
String comparison in single brackets in zsh
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, it threw error: = not found Then I converted `[` into `[[`, `]` into `]]` to avoid this error in z shell and ran it on...
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, it threw error: = not found Then I converted [ into [[, ] into ]] to avoid this error in z shell and ran it on z shell. It threw next error: condition expected: $f With [[ and ]], bash also throws error as: syntax error in conditional expression syntax error near `-o' Is there a POSIX standard to do string comparison in shell, that works across shells?
user93868
Jun 7, 2015, 02:23 PM • Last activity: Nov 5, 2023, 11:12 AM
7 votes
3 answers
3706 views
Which regular expression methods to validate input could be used in shell scripting?
#!/bin/sh re="\/$" if [ $1 =~ $re ]; then echo "${ATTENTION_PREFIX}$1 DIRECTORY MAY NOT CONTAIN A \"/\" OR LITERAL SLASH!${ATTENTION_POSTFIX}" exit 1 fi Executing `./file.sh hello/` results in `[: 29: hello: unexpected operator` It looks like that this regular expression method is incorrect for shel...
#!/bin/sh re="\/$" if [ $1 =~ $re ]; then echo "${ATTENTION_PREFIX}$1 DIRECTORY MAY NOT CONTAIN A \"/\" OR LITERAL SLASH!${ATTENTION_POSTFIX}" exit 1 fi Executing ./file.sh hello/ results in [: 29: hello: unexpected operator It looks like that this regular expression method is incorrect for shell scripting.
030 (1588 rep)
Jul 4, 2014, 08:22 AM • Last activity: Oct 7, 2023, 05:05 PM
Showing page 1 of 20 total questions