Operating System - HP-UX
1748076 Members
5250 Online
108758 Solutions
New Discussion

Usage of braces in Shell Scripts?

 
SOLVED
Go to solution
Gulam Mohiuddin
Regular Advisor

Usage of braces in Shell Scripts?

What are the differences between following in UNIX shell scripting?

( ) vs (( )) and [ ] vs [[ ]]

For example:
if ( x >= y )
if (( x >= y ))
if [ x >= y ]
if [[ x >= y ]]

Also “if [ x >= y ] ; then” what is the reason of using “;” in the syntax?


Thanks,

Gulam.
Everyday Learning.
6 REPLIES 6
Steven E. Protter
Exalted Contributor
Solution

Re: Usage of braces in Shell Scripts?

Shalom Gulam,

I use square brackets [] to contain conditions to test for.

I think if your question is two sets of brakets versus one set it makes no differences.

The best thing to do here is to write a little script.

bvar=1

if [ $bvar -ge 1 ]
then
echo "bvar is ge 1"
fi

Then try it with braces and then two sets of braces.

You may find the results interesting.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Usage of braces in Shell Scripts?

Hi Gulam:

Single parentheses group expressions.

Double parentheses treat the enclosing characters as a quoted expression, and thus the form is equavalent to 'let ...'.

Single square brackets define 'test' conditions to be evaulated as true or false.

Double square brackets also frame conditional evaluations but word splitting and file name generation are not performed within the double brackets.

See the manpages for 'sh-posix(1)' for more information, for instance.

Regards!

...JRF...
Jonathan Fife
Honored Contributor

Re: Usage of braces in Shell Scripts?

Single parentheses isn't valid syntax.

(( )) is the same as saying

let "x >= y"

Here is the man ksh description of let:
let arg ... Each arg is a separate arithmetic expression to be
evaluated. See Arithmetic Evaluation above, for a
description of arithmetic expression evaluation. The
exit status is 0 if the value of the last expression is
non-zero, and 1 otherwise.

[ ] will execute the comparison using the test command.

[[ ]] will execute the comparison using the shell's builtin test command.

man ksh should give you most (if not all) of this information.
Decay is inherent in all compounded things. Strive on with diligence
V. Nyga
Honored Contributor

Re: Usage of braces in Shell Scripts?

Hi,

there are also differencies if you use ksh or csh. For '()' in c-shell you need '[]' in korn-shell.

For double brackets see also:
http://www.context-switch.com/reference/kshref/ch4.html

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
Ralph Grothe
Honored Contributor

Re: Usage of braces in Shell Scripts?

With shells that supply the double parentheses they should be prefered over single ones.
(e.g. shells like HP-UX sh, bash, ksh, zsh?)
Because for expressions within them word splitting isn't performed.
This comes handy if an assignment to a variable by e.g. command substituion (formerly backtick quotes) fails.
e.g.

$ supposed_content="$(ls /non/existing/dir 2>/dev/null)"
$ [[ -n $supposed_content ]] && echo we could stat dir

but this wouldn't work with single brackets

$ [ -n $supposed_content ] && echo we could stat dir
su: test: Specify a parameter with this command.

But be aware that filename globbing doesn't work within double brackets

$ file=/etc/h?sts
$ [ -f $file ] && echo "$file worked"
/etc/h?sts worked
$ [[ -f $file ]] && echo "$file worked"
$

Also note that the logical operators changed to the more c-like ones in [[ ]],
viz. -a becomes &&, -o || etc.

(( )) evaluates arithmetic expressions
In bash this goes as far as letting you write C-like for loops, like

for ((i=0;i<10;i++)); do
# something
done

single paretheses open a subshell for commands within them.
A common idiom used when copying directories with tar for instance

cd /opt && tar cf - perl5 | (cd /tmp && tar xf -)

The semicolon between the test operator and then is necessary due to syntax rules if you put it in one line to have a similar appearance as in other programming/scripting languages.

Madness, thy name is system administration
Mancboy
Valued Contributor

Re: Usage of braces in Shell Scripts?

Hi Gulam,
I take it that you are starting out on the scripting road.

I would suggest that you try and leave logic issues to programs like perl or awk.

Without wishing to start a flame-war, each shell has its strengths and weaknesses. And alas, similarities - which then make your hard grafted scripts, non-transferrable. The same script written in ksh, sh, csh, bash, zsh, tcsh etc, will have slight variations in what a bracket means, how and where to use a THEN or an ELSE, whether subroutines appear before or after they are called etc.

They are great for doing the basics, but soon you'll want these loops and other constructs. That's where I feel they let themselves down.


I spent years trying to take into UNIX what I learnt in VMS. I then discovered awk and then perl.

If only I knew then what I know now....

ps you could always bypass perl and awk and go straight to python, then tell me how it works ;-)