Re: Newbie-ish question about ( [ and [[
- From: "Kaz Kylheku" <kkylheku@xxxxxxxxx>
- Date: 19 Dec 2006 23:01:43 -0800
Michael wrote:
I guess I'm a little dense, but I just don't get it.
When, and most importantly why do you use ( [ and [[ in shell scripts?
[[ ... ]] is a Korn-shell-ism. It's purpose is similar to the POSIX [
.... ], but it's implemented differently. The POSIX [ ... ] syntax is
actually a command named [ which takes a bunch of arguments, the last
of which is the word ] . The [[ ... ]] syntax, on the other hand, is
parsed specially by the Korn shell. Its operators are different too.
[ ... ] is a command which parses a simple expression language equipped
with various operators for comparing numbers, strings, testing and
comparing file system objects for various conditions and such. The
command yields a successful termination status if the test is true, and
fails otherwise, so it's most commonly used as the controlling
expression of an if, or while:
if [ this_file -ot that_file ] ; then echo this_file is older than
that_file ; fi
( ... ) specifies a command sequence that is executed within a
subshell: that is to say, a child process. Useful if you want to
isolate a script from the effects of some command, such as variable
assignments, changes of the current working directory, process
termination, and such.
# cd that doesn't affect parent script
( cd project; ./configure )
# termination that doesn't affect parent script
( set -e ; cp bin/$PROGRAM $INSTALL_DIR ; cp lib/$LIBRARY
$INSTALL_DIR )
In the last example, the "set -e" turns on a mode which causes the
shell to terminate with a failed status if any of the commands fails.
Because this is in a subshell, it does not cause the parent to exit,
only the subshell. The parent can simply test for the overal result:
if ! ( set -e ; cp ... ) ; then
# subshell command sequence failed
fi
I'm trying to construct a simple:
if ( some-test-goes-here ) or [ some-test-goes-here ] or maybe even [[
some-test-goes-here ]]
then
blah
fi
construct and I am not sure when or why to use one of the three
different kinds of brackets.
Basically never use the [[ ]] if you care about portability to anything
but the Korn shell.
Use [ ] when you need the special test expression language for testing
strings for empty/nonempty/equal, simple numeric comparisons, or file
system tests.
Use ( ) to isolate multiple commands to a subshell.
Use no brackets or parentheses at all if you just want to test the
termination status of a command, or of the last command of a sequence:
if /bin/true ; then echo "true succeeded, as it should!" ; fi
Can someone point me to a resource?
http://www.opengroup.org/onlinepubs/007908799/xcu/chap2.html
.
- Follow-Ups:
- Re: Newbie-ish question about ( [ and [[
- From: Michael
- Re: Newbie-ish question about ( [ and [[
- From: peace07
- Re: Newbie-ish question about ( [ and [[
- References:
- Newbie-ish question about ( [ and [[
- From: Michael
- Newbie-ish question about ( [ and [[
- Prev by Date: Re: Strange ksh93 problem
- Next by Date: Re: How to make a stopped process return to run?
- Previous by thread: Re: Newbie-ish question about ( [ and [[
- Next by thread: Re: Newbie-ish question about ( [ and [[
- Index(es):
Relevant Pages
|