Re: Newbie-ish question about ( [ and [[
- From: Janis Papanagnou <Janis_Papanagnou@xxxxxxxxxxx>
- Date: Wed, 20 Dec 2006 19:11:32 +0100
Michael wrote:
Thanx for the great input. I think I'm beginning to get the light bulb
turned on.
Here's what I believe y'all said.
The construct ( blah ) means:
( # Open a new subshell
Blah # Execute the "Blah" command(s) and save the exit status
) # Close the subshell and return the exit status of Blah to
the calling shell
There's nothing like "closing" a subshell.
( ... ) will invoke a subshell that executes the commands '...'.
So, I can actually put any command or command string inside of the "(
)" that returns an exit status that the "if" then evaluates. Operators
are permitted, but only as they relate to the commands.
You don't need a subshell if you want an if-construct to check for the
return status; just use the commands as they are, for example...
if grep pattern <file.txt
then
: ...
fi
A subshell is helpful if you want your environment unchanged but the
commands want their own environment, e.g. as in
( cd /some/dir ; tar cf - ) | ( cd /another/dir ; tar xf - )
Here in both subshells the working directories are changed as needed.
Next, the construct [ foo ] means:
[ # Execute the alias of the test command on the following
Blah # something that the test command can legally evaluate
] # terminus of what is being tested
Not necessarily an alias. There might be a program /bin/[ or it might
also be a shell built-in, or both.
$ ls -l /usr/bin/[
lrwxrwxrwx 1 root root 4 2003-06-05 01:14 /usr/bin/[ -> test
$ type [
[ is a shell builtin
Janis
.
So the contents of the "[ ]" must contain operators, not commands;
unless an operator is operating on the result of a command.
Did I get it right?
Thanx!
- References:
- Newbie-ish question about ( [ and [[
- From: Michael
- Re: Newbie-ish question about ( [ and [[
- From: Kaz Kylheku
- Re: Newbie-ish question about ( [ and [[
- From: Michael
- Newbie-ish question about ( [ and [[
- Prev by Date: Re: Newbie-ish question about ( [ and [[
- Next by Date: piped to awk: how to print vars outside of line-in loop
- Previous by thread: Re: Newbie-ish question about ( [ and [[
- Next by thread: Re: Newbie-ish question about ( [ and [[
- Index(es):
Relevant Pages
|