Tuesday, December 25, 2012

Double Parantheses (( )) and Single Parantheses () in Bash shell


Let us see what is the difference between double parantheses - (( )) and single parantheses - $() in Bash shell.

Double parantheses is used as math operator in Bash shell. Double parantheses can be used as an extension for numerical for loops. This can be demonstrated as follows

for (( i = 1; i <= 10; i++ ))
do
      echo "$i"
done

$ abc=5
$ echo $((abc-1))
$ 4

Let us come to single parantheses now. What does $() operator stand for - For inline execution. The $() operator inserts the output of one command into the middle of a statement.The Bourne shell provides two operators for executing a command and placing its output in the middle of another command or string. These operators are the $() operator and the backtick (`) operator middle of a statement. This is demonstrated as

$ xyz=$(date)
$ echo $xyz
Tue Dec 25 20:56:30 GMT 2012

$ abc=`date`
$ echo $abc
Tue Dec 25 20:56:15 GMT 2012

No comments:

Post a Comment