Linked Questions
28 questions linked to/from How can I declare and use Boolean variables in a shell script?
127
votes
2
answers
208k
views
How to evaluate a boolean variable in an if block in bash? [duplicate]
I have defined the following variable:
myVar=true
now I'd like to run something along the lines of this:
if [ myVar ]
then
echo "true"
else
echo "false"
fi
The above code does work, but if ...
4
votes
5
answers
11k
views
How can I write if/else with Boolean in Bash? [duplicate]
How can I write an 'if then' statement to switch between these to variables as in the following?
if(switch){
server_var_shortname=$server_shared_shortname
server_var=$server_shared
...
2
votes
3
answers
7k
views
Boolean variables in a shell script [duplicate]
I follow this post
How to declare and use boolean variables in shell script?
and developed a simple shell script
#!/bin/sh ...
1
vote
1
answer
5k
views
Negate a boolean variable and assign it to a new variable [duplicate]
In a Bash script, I'd like to define a boolean variable y to store the negated value of another boolean variable x. The following script,
#!/bin/bash
x=true
y=$(( ! "${x}" ))
echo "${y}...
-1
votes
2
answers
1k
views
Use if condition within an echo in shell [duplicate]
I am automating a script in real-time and based on some variables' values I want to append different string versions into the script I am building.
to simplify the case, here is an example:
someenvvar=...
-1
votes
1
answer
310
views
Simple condition getting wrong evaluation result in bash [duplicate]
I have this bash script getting a Boolean parameter, based on the answer here
I created this simple condition, for some reason I am getting false results
#!/bin/bash
copyFile() {
echo "...
0
votes
0
answers
19
views
Booleans in shell script [duplicate]
I am using a optional variable BUILD_X flag to decide whether to build X based on user input.
BUILD_X=true
while getopts "B:" option; do
case "$option" in
B) BUILD_X=$OPTARG;;...
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 ...
314
votes
11
answers
334k
views
Returning a boolean from a Bash function
I want to write a bash function that check if a file has certain properties and returns true or false. Then I can use it in my scripts in the "if". But what should I return?
function myfun(){ ... ...
260
votes
7
answers
214k
views
Test for non-zero length string in Bash: [ -n "$var" ] or [ "$var" ]
I've seen Bash scripts test for a non-zero length string in two different ways. Most scripts use the -n option:
#!/bin/bash
# With the -n option
if [ -n "$var" ]; then
# Do something when var is ...
61
votes
1
answer
5k
views
Why does Bash treat undefined variables as true in an 'if' statement?
In the below simple example, if CONDITION is not set in the script, running the script prints "True". However, one would expect it to print "False". What is the reason for this ...
2
votes
3
answers
20k
views
booleanParam in jenkins dsl
I have a jenkins groovy script like this:
freeStyleJob(“test”) {
properties { githubProjectUrl(‘…’) }
description(‘’’job description’’’.stripMargin('|'))
logRotator{ ...
2
votes
3
answers
8k
views
Operations on boolean variables
In this question it has been shown how to use neat boolean variables in bash. Is there a way of performing logic operations with such variables? E.g. how to get this:
var1=true
var2=false
# ...do ...
0
votes
5
answers
4k
views
How to make the script exit with exit code 1 but only after last iteration of the loop
I want to check if an application up and running by accessing the "health check" link, so I want to implement the following logic:
if it returns 200 the script stops working with exit code 0,...
8
votes
3
answers
8k
views
bash: error handling and functions
I am trying to call a function in a loop and gracefully handle and continue when it throws.
If I omit the || handle_error it just stops the entire script as one would expect.
If I leave || ...