Skip to main content
edited tags
Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k
added 254 characters in body
Source Link
Freedom_Ben
  • 4.7k
  • 4
  • 25
  • 31

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?

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

In Bash, how does one specify operator precedence 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 

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?

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?

Source Link
Freedom_Ben
  • 4.7k
  • 4
  • 25
  • 31

Bash - how to make explicit operator precedence without creating a subshell

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

In Bash, how does one specify operator precedence 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 

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?