0

I see people writing their bash scripts with conditional statements in different formats. For example here are a few instances:

x=20;
if [ "$x" -eq "20" ];
then
 echo "1. yes!";
fi
if [[ "$x" -eq "20" ]];
then
 echo "2. yes!";
fi
if (( "$x" == "20" ));
then
 echo "3. yes!";
fi

The above code gives me all three as YES, in a bash shell.

Clearly, I see various differences in using each of these syntaxes for example when I need compound-logical-statements (using things such as && ||) OR string/numerical comparisons.

Adding to this confusion, even variable access seems to have this difference $x $((x)) ${x} etc.

Can somebody please clarify how each of above syntaxes important and demystify my thoughts?

Loves Probability
  • 929
  • 1
  • 9
  • 15
  • This questions seems rather broad and a possible duplicate of a number of questions. To start off, refer to [Compound Commands](http://www.gnu.org/software/bash/manual/bashref.html#Compound-Commands) in the manual. – devnull Dec 04 '13 at 06:10
  • @devnull I've cut down two of too-broad looking statements. Hope it is specific now. Well, after all its about one issue: "What is the difference between 3 statements.." – Loves Probability Dec 04 '13 at 06:24
  • This should answer 70% of your question: http://stackoverflow.com/a/3427931/1251660 – slayedbylucifer Dec 04 '13 at 06:34
  • Moreover have a look at http://stackoverflow.com/questions/2188199/how-to-use-double-or-single-bracket-parentheses-curly-braces – devnull Dec 04 '13 at 06:51

0 Answers0