Bash - Conditional Expression

Bash Liste Des Attaques Ovh

About

A predicate or boolean expression is known in Bash as a Conditional Expression.

It is a command that returns an exit status of 0 or 1.

The conditional expression is part of the compound expression.

Syntax

[ conditional_expression ]
# or without word spliting and pathname expansion
[[ conditional_expression ]]
# or 

where:

  • [] defines a conditional expression evaluation
  • [[]] defines a conditional expression evaluation where Word splitting and pathname expansion are not performed
  • conditional_expression is a conditional expression
  • Return a status of 0 or 1

Note:

Test

To test a conditional expression, see the test command

test conditional_expression

Expression

Boolean Expression (And/Or)

Name Double Bracket operator Single Bracket Operator
And && -a (**)
Or || -o (**)

Example:

  • Double Bracket And
[[ -n $var && -f $var ]] && echo "$var is a file''
  • Double Bracket Or
[[ -b $var || -c $var ]] && echo "$var is a device"
  • Single Bracket And
if [ ${PATH_FILE:0:1} != "/" -a ${PATH_FILE:0:1} != "." ] 
then
   PATH_FILE="`pwd`/${PATH_FILE}";
fi

Date Comparison

How to use the date / time variable in Bash?

Name Double Bracket operator Single Bracket Operator
RegularExpression matching =~ (not available)

Double Bracket example

[[ $(date) =~ ^Fri\ ...\ 13 ]] && echo "It's Friday the 13th!"

Comparison Operator

Ordinal Comparison operator. They works on string and integer

Double Bracket operator Single Bracket Operator Example
-lt -lt [[ 8 -lt 9 ]] && echo "8 is less than 9"
-ge -ge [[ 3 -ge 3 ]] && echo "3 is greater than or equal to 3"
-gt -gt [[ 5 -gt 10 ]] || echo "5 is greater than than 10"
-le -le [[ 3 -le 8 ]] && echo "3 is less than or equal to 8"
-eq -eq [[ 5 -eq 05 ]] && echo "5 equals 05"
-ne -ne [[ 6 -ne 20 ]] && echo "6 is not equal to 20"

File System Path

On the Path works on file and directory path

Name Double Bracket operator Single Bracket Operator Example
entry (file or directory) exists -e Na [[ -e $config ]] && echo "config file exists: $config"
file is newer/older than other file -nt / -ot Na [[ $file0 -nt $file1 ]] && echo "$file0 is newer than $file1"
two files are the same -ef Na [[ $input -ef $output ]] && { echo "will not overwrite input file: $input"; exit 1; } 
negation ! Na [[ ! -u $file ]] && echo "$file is not a setuid file"

Exit Status / Command

The exit status is a boolean 0 for success and 1 for error.

We can then use it in a conditional statement.

Example:

  • With the command.
if ! (command args); then
    echo "The command has error"
fi
commando

# The output
if [ $? != 0 ]; then
	echo "Commando goes bad"
	else
	echo "Commando goes good"
fi





Discover More
Bash Liste Des Attaques Ovh
Bash - (Argument|Positional Parameter)

An argument is a parameter given: to a command to a function or to the bash shell They are referenced by position. A positional parameter is a parameter denoted: by one or more digits, ...
Bash Liste Des Attaques Ovh
Bash - Arithmetic Expression

arithmetic in bash part of compound expression . Counter Simple addition where let is the let command If the value of the expression is: non-zero, the return status is 0; otherwise ...
Bash Liste Des Attaques Ovh
Bash - Conditional Operator

Conditional Operator are option like syntax (ie -x) that are used in a conditional expression -f to check if the path is a file -d to check if the path is a directory -n to check if a variable/parameter...
Bash Liste Des Attaques Ovh
Bash - If then else

if execute conditionally a block with a list of command based on the exit status of a list of command The if list. The command list) is executed (Generally there is one or more conditional command...
Bash Liste Des Attaques Ovh
Bash Shell and (Unix|Linux) Utilities (XCU)

Bash is: Bourne-Again shell (Os Shell). It means that this is scripting language used in a terminal based around The articles of this section are over: the Bash (Bourne-Again Shell) the...
Card Puncher Data Processing
Datacadamia - Data all the things

Computer science from a data perspective
Bash Liste Des Attaques Ovh
How to use the date / time variable in Bash?

This page is the management of time variable in Bash. Format Conditional Double Bracket example Name Double Bracket operator Single Bracket Operator RegularExpression matching =~ ...
Bash Liste Des Attaques Ovh
Sh - String Variable

String Variable, see also character string in bash. When calling a function, quote the variable otherwise bash will not see the string as atomic but as an array Sh with Bash “” The...
Bash Liste Des Attaques Ovh
Shell - Compound Commands

A compound command is one of the following: where () defines the execution environment as a subshell Variable assignments and builtin commands that affect the shell’s environment do not...



Share this page:
Follow us:
Task Runner