5

I'm sure this is posted somewhere, but I haven't been able to find it.

In Bash, how does one specify operator precedence (aka command grouping) without creating a subshell? In most other languages the() do this, but in Bash this runs the commands in a subshell which "discards" environment changes. I want to specify operator precedence without losing environment changes.

Specifically, I'd like to do something like this and have the entire script exit, not just the subshell in the ():

die ()
{
    echo "[DIE]: $1"
    exit 1
}

# When installChruby returns an error, print the error message and exit
[[ $CHRUBY =~ [Yy] ]] && (installChruby || die "Error installing chruby")

I figured out a "workaround" by doing this, but it's not a pretty one-liner like I want:

if [[ $CHRUBY =~ [Yy] ]]; then installChruby || die "Error installing Chruby"; fi 

The desired outcome is to do nothing and continue if CHRUBY is unset, to call the function installChruby if CHRUBY is either Y or y, and to call the die function only if the installChruby function returns false.

Is there an operator in Bash that does this besides (), or is there a way to tell the code inside the () to not run in a sub-shell?

  • 1
    Use command grouping {}. – jordanm Sep 22 '14 at 16:16
  • If your goal is When installChruby returns an error, print the error message and exit, so using [[ $CHRUBY =~ [Yy] ]] && installChruby || die "Error installing chruby" will work as you expected. – cuonglm Sep 22 '14 at 16:18
  • @Gnouc yes, but it also exits when CHRUBY is set to something else (like N or n), which is not what is wanted. – Freedom_Ben Sep 22 '14 at 16:20
  • More generally, see stackoverflow.com/questions/6270440/… – Gilles Sep 23 '14 at 22:53
  • Related: unix.stackexchange.com/q/247187/135943 – Wildcard Dec 3 '15 at 21:23
2

from man bash:

   { list; }
          list  is  simply executed in the current shell environment.  list must be terminated with a newline or semicolon.
          This is known as a group command.  The return status is the exit status of list.  Note that unlike the  metachar‐
          acters  (  and  ), { and } are reserved words and must occur where a reserved word is permitted to be recognized.
          Since they do not cause a word break, they must be separated from list by whitespace or another shell metacharac‐
          ter.
  • @Gnouc I think that is because the exit status of echo 123 is true, thus short-circuit avoids echoing qqwe – Freedom_Ben Sep 22 '14 at 16:24
  • @Freedom_Ben: Misunderstood your question, now I've got it. – cuonglm Sep 22 '14 at 16:26
  • @Gnouc No worries, thanks for speaking up! I think it's really helpful to get other perspectives. I often learn more from other people's questions and comments than I do from the answers, simply because other people think differently. Also, it helps me to know that I should reword the question to be more explicit. Thanks! – Freedom_Ben Sep 22 '14 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.