Linked Questions
29 questions linked to/from Test for non-zero length string in Bash: [ -n "$var" ] or [ "$var" ]
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....
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 ...
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 [ "...
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 ?
}
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 ...
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 ...
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 -...
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 ...
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?
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 ...
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 ...
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 ...
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 == $'\\' -...
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 "$...