1

So I'm stuck doing a lot of cross-compiling and am typing stuff like this a lot

FOO=bar command --some-args
BAZ=qxt other-command --other-args

But obviously the real shell variables are long and cumbrous to type. Bash is not a dynamic language, so backticks won't work (it tries to run the command FOO=bar). xargs is wrong though something like it that works before command evaluation would be good. Setting the shell variables I want as env variables hits the same problem as backticks if I just put them before the command; changing envs explicitly every time leaves me with the same cumbrous typing problem as shell variables, with the added fun of polluting my later environment. Is there something like pushenv and popenv?

Is there any way I can store and retrieve through a short handle all the shell variables I might need, so I can do something like

shellvars1 command --some-args
shellvars2 other-command --other-args

The different combinations of shell variables is large but finite, so if there's a way of offloading that to a helper script I can type them all there once, I just can't figure out a way to do that.

Bonus points: does bash have something like tcsh's ability to do

!24:s/FOO/BAZ/

?

That could also help (though not as much). I mostly want to remove opportunities for my preternatural skill at typos to show itself.

2

You seem to be somewhat confused about what is a shell variable and what is not. All the variables in your post are environment variables, not shell variables. To set a shell variable:

foo=bar

To set an environment variable:

export FOO=BAR

(or setenv in c shells). But in

FOO=BAR command

the assignment counterintuitively has a different meaning than before and sets an environment variable in the environment of the command only. That said, this simple script should answer your needs:

#!/bin/bash
VAR1=VAL1 ... VALN=VARN "$@"

As for the bonus question: I don't think bash can do that, but zsh is awesome and can do it. Both

^FOO^BAR

and

!!:s/FOO/BAR/

have the same effect, and for your specific example:

!24:s/FOO/BAR/
  • "local" variable, if you prefer. I tried something like your suggestion earlier and it still tried to run FOO; let me retry – Bandrami May 17 '14 at 7:36
  • Thanks; that was it. When I had tried something similar I was putting the FOO=bar stuff on separate lines before the $@. – Bandrami May 17 '14 at 7:56
1

You can put variable assignments in an alias.

alias shellvars1='FOO=bar '
alias shellvars2='BAZ=qxt '

The space at the end of the variables tells the shell to consider subsequent words for alias expansion as well. For example, if a is an alias for somecommand --option, then shellvars1 shellvars2 a wibble runs FOO=bar BAZ=qxt somecommand --option a.

You can use functions as well. Use aliases if the assignments are fixed, and functions if you want more flexibility (e.g. conditional assignments).

shellvars1 () {
  local tmp=2
  FOO=$((tmp + tmp)) "$@"
}
…
shellvars1 mycommand

You can also make variables local to a function. If a variable is local to a function, then an export directive is also restricted to the function's scope.

shellvars1 () {
  local FOO=bar
  export FOO
  "$@"
}

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.