Linked Questions

50 votes
2 answers
186k views

Null & empty string comparison in Bash [duplicate]

I don't set any values for $pass_tc11; so it is returning null while echoing. How to compare it in if clause? Here is my code. I don't want "Hi" to be printed... -bash-3.00$ echo $pass_tc11 -bash-3....
logan's user avatar
  • 8,186
0 votes
1 answer
3k views

How to check for a null value in shell [duplicate]

I have a variable(which is a string) which can have two values: (1)variable = null (2)variable = "some string value" Now I have to apply a condition like: if (variable is equal to null) then execute ...
gulshan kumar's user avatar
0 votes
1 answer
2k views

Compare empty string inside if in bash (C style vs Bash) [duplicate]

I was learning bash when I stuck comparing strings inside if statement. Script: shiftDemo.sh 1 #!/bin/bash 2 3 #using shift keyword to shift CLA 4 5 while true 6 do 7 if [ "...
Prateek Joshi's user avatar
972 votes
320 answers
501k views

Strangest language feature

What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered? Please only one feature per answer.
2441 votes
38 answers
2.3m views

How to check if a variable is set in Bash

How do I know if a variable is set in Bash? For example, how do I check if the user gave the first parameter to a function? function a { # if $1 is set ? }
prosseek's user avatar
  • 188k
1495 votes
27 answers
1.8m views

How can I declare and use Boolean variables in a shell script?

I tried to declare a Boolean variable in a shell script using the following syntax: variable=$false variable=$true Is this correct? Also, if I wanted to update that variable would I use the same ...
hassaanm's user avatar
  • 15.3k
926 votes
11 answers
407k views

Are double square brackets [[ ]] preferable over single square brackets [ ] in Bash?

A coworker claimed recently in a code review that the [[ ]] construct is to be preferred over [ ] in constructs like if [ "`id -nu`" = "$someuser" ] ; then echo "I love ...
Leonard's user avatar
  • 13.6k
593 votes
8 answers
1.0m views

How to do a logical OR operation for integer comparison in shell scripting?

I am trying to do a simple condition check, but it doesn't seem to work. If $# is equal to 0 or is greater than 1 then say hello. I have tried the following syntax with no success: if [ "$#" == 0 -...
Strawberry's user avatar
  • 67.3k
534 votes
5 answers
809k views

How to check if an environment variable exists and get its value? [duplicate]

I am writing a shell script. In this shell script, I am have a variable that either takes a default value, or the value of an environment variable. However, the environment variable doesn't have to be ...
Nicolas El Khoury's user avatar
365 votes
11 answers
496k views

How to find whether or not a variable is empty in Bash

How can I check if a variable is empty in Bash?
Tree's user avatar
  • 10.1k
236 votes
7 answers
95k views

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

I'm reading bash examples about if, but some examples are written with single square brackets: if [ -f $param ] then #... fi others with double square brackets: if [[ $? -ne 0 ]] then start ...
rkmax's user avatar
  • 18k
28 votes
7 answers
50k views

Test for a Bash variable being unset, using a function

A simple Bash variable test goes: ${varName:? "${varName} is not defined"} I'd like to reuse this, by putting it in a function. How can I do it? The following fails # # Test a variable ...
DaveP's user avatar
  • 1,089
24 votes
4 answers
16k views

What does "plus colon" ("+:") mean in shell script expressions?

What does this mean? if ${ac_cv_lib_lept_pixCreate+:} false; then : $as_echo_n "(cached) " >&6 else ac_check_lib_save_LIBS=$LIBS Looks like ac_cv_lib_lept_pixCreate is some variable, so ...
Suzan Cioc's user avatar
  • 29.8k
20 votes
4 answers
30k views

Compound 'if' statements with multiple expressions in Bash

I would like to recreate something like this if ( arg1 || arg2 || arg 3) {} And I did got so far, but I get the following error: line 11: [.: command not found if [ $char == $';' -o $char == $'\\' -...
david's user avatar
  • 751
18 votes
3 answers
36k views

How does the keyword “if” test if a value is true or false?

In bash script if [ 1 ] then echo "Yes" else echo "No" fi Output: Yes It represents that '1' is treated as true value. But in code: word = Linux letter = nuxi if echo "$word" | grep -q "$...
kit.yang's user avatar
  • 2,788

15 30 50 per page