----- Original Message -----
From: "David C. Rankin"
Listmates,
Stuck on a stupid bash problem again. Why can't I set a variable within a set of parenthesis?
#!/bin/bash
# This works
[[ $1 == "--verbose" || $1 == "-v" ]] && verbose='y'
[[ $1 == "--verbose" || $1 == "-v" ]] && ( echo "this"; echo "that" )
# This _doesn't_ work
[[ $1 == "--verbose" || $1 == "-v" ]] && ( verbose='y'; echo "that" )
No matter what I do, if I try to set a variable inside the ( ) expression, it never gets set -- Why?
Because that is the very definition of () vs {} () creates a fully seperate child process. Same as if you had used a pipe or backticks or $() run another script by it's file name. Child processes inherit their environment from the parent. The parent gets to set up the env that the child will inherit, so the parent can set variables that the child will see. But the new process is a new, distinct process, it has it's own seperate environment, and except for the case of a parent spawning a child and creating that childs initial environment, no process can alter or read another processes environment. So the child process can't modify the parents environment. Nor can the parent further modify or even read the childs (there are exceptions, especially if we weren't talking about shell scripts, but for this question you may think of it as a rule or truth) {} can be used to group commands and to capture stdin/out/err ,yet have the commands still be running in the same process as the rest of the script, meaning they still share the same environment. Generally you only use () when you specifically require the effects if a seperate process, such as environment variables that won't affect the rest of the script, or the canonical example is to cd somewhere to do something and not have to worry about cd-ing back. cd from-dir ;tar cf - * |(cd to-dir ;tar xf -) For your example above you could just use {} to get what you want. That's exactly what {} is for. But really I would have used case instead of [[]] for that anyways, for two reasons. 1) case makes it easy to test for several things at once in a more convenient way than the [] tests. 2) case syntax obviates the need for {} to group commands to hinge off the same test. so it's like case was made for the task you are doing above. case "$1" in -v|--verbose) verbose=y ; echo "verbose mode" ;; esac -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org