While learning a bit about bash, I come to see four types of ways of working with if
statements:
What is the difference between Parenthesis and Square Brackets in bash.
3. Brackets are used to enclose parenthetical materials within the parentheses while parentheses are used to enclose words, numbers, phrases, sentences, symbols, and other items in a sentence.
( Single Parentheses ) This means that they run through all of the commands inside, and then return a single exit code. Any variables declared or environment changes will get cleaned up and disappeared.
Double Brackets i.e. [[]] is an enhanced (or extension) version of standard POSIX version, this is supported by bash and other shells(zsh,ksh). In bash, for numeric comparison we use eq , ne , lt and gt , with double brackets for comparison we can use == , !=
so you can use this if you want to print the name of shell script. $- $- (dollar hyphen) bash parameter is used to get current option flags specified during the invocation, by the set built-in command or set by the bash shell itself. Though this bash parameter is rarely used. $?
The tests you had listed :
test
test
but more powerful)are not exhaustive, you can use boolean logic
if command; then ...
too, because the commands have exit status. In bash
, 0
is true
and > 0
is false
.
You can see the exit status like this :
command echo $?
See :
http://wiki.bash-hackers.org/syntax/basicgrammar
http://wiki.bash-hackers.org/syntax/arith_expr
http://mywiki.wooledge.org/BashGuide/TestsAndConditionals
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With