Bash c-style if statement and styling techniques
There are some syntactic specialties of bash (and maybe other shells of the sh
family) related to if:
The basic if-statement has the syntax
if condition-command ; then list ; fi
(with the possibility to addelif
andelse
for other branches).- The
condition-command
can be any command that bash supports (even another if, I think), is executed and the return value is then evaluated. (Actually, it can also be a list of commands, but this might confuse readers. The return value of the last command counts for the condition.) - Bash (and Unix shells in general) interpret result codes of programs in a bit unusual way (for C programmers): a result of 0 is interpreted as true (or "no error"), and any other result is interpreted as false (or "some error occurred"). (In C and some other languages it is the other way around: 0 is
false
and everything else istrue
.) - If the result of the condition-command is 0 (true), the
list
is executed (and the return value of theif
is the result of the list). If it is not-0 (false), the list is not executed (but theelif
orelse
branches, if there are any). (If none of the lists is executed, the return value of the if is 0.)
- The
Some useful commands as conditions (these work also as conditions in
while loops
):[ ... ]
is in fact another way to writetest ...
- this is simply a command which may return 0 (true) or 1 (false) depending on what parameters you are giving. (This is a buildin of the shell.)[[ ... ]]
is a conditional expression command. It supports some of the same arguments that[
supports, and some more, like parentheses,<
,>
, and(
...)
for condition nesting, and handles expansions inside a bit different, since it is a special syntactic construct instead of a buildin command. The whole thing is a command that returns 0 (true) or 1 (false).(( ... ))
is a wrapper for an arithmetic expression (or several of them) as a command. The result is 0 (true) when the result of the (last) expression is non-0, and 1 (false) when the result of the (last) expression is 0. (You could instead writelet ...
.)In arithmetic expressions the shell variables can be used without
$
, and everything is interpreted as integers (of a fixed width, but I didn't find out which). You can use these operators:- unary:
++
,--
,+
,-
,!
,~
(like in C) - binary arithmetic:
**
(exponentation),*
,/
,%
,+
,-
- bit-shift:
<<
,>>
- comparison:
<=
,>=
,<
,>
,==
,!=
(I think these return either 1 (true) or 0 (false)) - bitwise:
&
,^
,|
- logical:
&&
,||
, and of course the ternary operator:... ? ... : ...
- assignment:
=
,*=
,/=
,%=
,+=
,-=
,<<=
,>>=
,&=
,^=
,|=
- comma:
,
(simply connects two expressions, returning the value of the last one)
I think in general this works a lot like C integer expressions. (These are roughly in order of precedence, but sometimes there are divisions inside of each group. Look in
info '(bash)Shell Arithmetic'
for details.)- unary:
true
is a buildin which does nothing and always returns 0.false
is a buildin which does nothing and always returns 1.- you can also call your external C (or anything else) programs as commands, their return values are interpreted the same way.
Lists:
( ... )
is a list of commands, which is executed in a subshell. Variable assignments in this subshell are not propagated back.{ ...; }
is a list of commands, which does not create a subshell.;
is a simple separator of commands in a list, but can also be replaced by a new-line.&&
is a separator of commands executing the second one only when the first returned 0.||
is a separator of commands executing the second one only when the first returned not-0.&
is a separator of commands, executing both commands in parallel (the first one in background).
So, you can write your if
-command with some braces and parentheses, but you still need your then
and fi
:
if (( i > 0 ))
then {
echo "i > 0"
}
else {
echo "i <= 0"
}
fi
The braces here are simply superfluous, since the commands between then
, else
and fi
are (each) one list anyway. (Also note the need of new-lines or ;
after and before the closing braces here.)
But as the others said, better use another language if you want to use a C-like syntax.
Use &&
and ||
command lists and curly-braced group commands
On examples below, true
, false
, or test 0 -eq 0
could be replaced by any command, including arithmetic expressions like (( i > 0 ))
Multi-line style
true && {
echo "true command returns zero (aka success)"
# more commands here
# and here
} || {
echo "true command never returns non-zero"
# more commands here
# and here
}
Obfuscated in-line style
# I don't like this one so much
false && echo "false never returns zero" || echo "false returns always 1"
Split in-line style
# here the draw-back are back slashes
test 0 -eq 0 \
&& echo "Surprise, zero is equal to zero" \
|| echo "Apocalypse is near if zero is not zero"
NOTE: I can assert the techniques above work, but I don't know the performance drawbacks, if there are.
The reason for the current syntax is largely historical. Most of what you're asking about is based on the original Bourne shell (sh). Bourne chose to base the syntax on Algol instead of C.
To have a more C-like syntax (and not be csh), someone would have to start from scratch.
As others have said, for many purposes you can write scripts in other languages.
You might want to look at Tiny C Compiler which can be used to run C source as a shell script.
#!/usr/bin/tcc -run
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
In bash, the if statement doesn't have brackets like ()
eg of some syntax in bash
if [[ ... ]];then
..
fi
if [ ... ];then
...
fi
if $(grep .. file) ;then #note the () is part of $().
...
fi
I have nothing against Bash, but IMO, you are better off using a programming language which can provide you all the things you need in your scripting/programming endeavor, such as Python, Ruby or Perl. Bash has some limitations, like performing floating points maths,etc.Also, if your task is complex enough, excessive use of *nix tools may lead to "bloatedness" of your script, hence hindering performance. If you want "clean" syntax and maintainability, either Ruby/Python may suit you. IMO, having brackets or not is just language design considerations and I personally would not want brackets if i have the choice.