From what I understand you can do C-style for and while loops in bash.
LIMIT=10
for ((a=1; a <= LIMIT ; a++)) # Double parentheses, and "LIMIT" with no "$".
do
echo -n "$a "
done # A construct borrowed from 'ksh93'.
And even ternary operators.
(( var0 = var1<98?9:21 ))
How would you do this with the if statement?
Also why don’t they implement braces like in C? What is the logic with using all of these keywords like done, do, if, and fi? I will be writing some scripts but bash appears very different.
Are there any bash styling techniques or bash alternatives/plugins? I would like to follow the standard, but coming from a C, Java and PHP background bash looks really weird. Maintainability and standards are important.
There are some syntactic specialties of bash (and maybe other shells of the
shfamily) related to if:The basic if-statement has the syntax
if condition-command ; then list ; fi(with the possibility to addelifandelsefor other branches).condition-commandcan 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.)falseand everything else istrue.)listis executed (and the return value of theifis the result of the list). If it is not-0 (false), the list is not executed (but theeliforelsebranches, if there are any). (If none of the lists is executed, the return value of the if is 0.)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:++,--,+,-,!,~(like in C)**(exponentation),*,/,%,+,-<<,>><=,>=,<,>,==,!=(I think these return either 1 (true) or 0 (false))&,^,|&&,||, and of course the ternary operator:... ? ... : ...=,*=,/=,%=,+=,-=,<<=,>>=,&=,^=,|=,(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.)trueis a buildin which does nothing and always returns 0.falseis 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 yourthenandfi:The braces here are simply superfluous, since the commands between
then,elseandfiare (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.