1

In the bash manual section 3.7.4, it states

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters [section 3.4].

And a trivial example of this is

MYVAR=MYVALUE mycommand

I have read section 3.4 and I still can't figure out how to specify multiple parameter assignments. Note that the statement in 3.7.4 is definitely plural, implying that it is possible.

The following does not seem to work:

MYVAR1=abc MYVAR2=xyz mycommand

I am using bash version 4.1, 23 December 2009.

1
  • 1
    Works here. You're screwing something else up. Commented Oct 22, 2011 at 18:30

3 Answers 3

2

It should work. That is acceptable syntax. Here's an example:

$ cat a
#!/bin/sh
echo $MYVAR1
echo $MYVAR2
$ ./a


$ MYVAR1=abc MYVAR2=xyz ./a
abc
xyz
$

UPDATE: Your updated example given in your answer will work if you precede the simple command with the variables as required:

mycommand () { echo MYVAR1=[$MYVAR1]; echo MYVAR2=[$MYVAR2]; }
for f in ~/*.txt ; do MYVAR1=abc MYVAR2=xyz mycommand; done 
Sign up to request clarification or add additional context in comments.

Comments

1

Oops, my example was over-simplified. This works fine:

mycommand () { echo MYVAR1=[$MYVAR1]; echo MYVAR2=[$MYVAR2]; }
MYVAR1=abc MYVAR2=xyz mycommand

but this does not:

mycommand () { echo MYVAR1=[$MYVAR1]; echo MYVAR2=[$MYVAR2]; }
MYVAR1=abc MYVAR2=xyz for f in ~/*.txt; do mycommand; done

and the key difference is this phrase:

for any simple command or function

A for command is a complex command, not "a simple command or function", according to section 3.2.

1 Comment

But this does: for f in ~/*.txt ; do MYVAR1=abc MYVAR2=xyz mycommand; done
0

I've never seen that method used.

You could always just pass in regular parameters to the script.

Updating for new example:

This works in both situations:

> mycommand () { echo MYVAR1=[$1]; echo MYVAR2=[$2]; }

> mycommand a b
MYVAR1=[a]
MYVAR2=[b]

> for f in ~/file*.txt; do mycommand a b; done
MYVAR1=[a]
MYVAR2=[b]
MYVAR1=[a]
MYVAR2=[b]

4 Comments

It's perfectly legal and defined in the POSIX standard (it's not even a bashism): 'A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.' pubs.opengroup.org/onlinepubs/9699919799/utilities/…
What should technically work and what is easy to make work are often two different things. Why beat yourself up over something that you can make work another way in two shakes of a lamb's tail?
Huh? Passing a parameter and setting a variable are not the same thing. And this is basic shell stuff. There's nothing tough about it.
If there wasn't anything tough about it, you wouldn't have the opportunity to answer a question on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.