Jeremy
Jeremy

Reputation: 5311

Command-line variable assignments in Cygwin Bash not working?

According to section 3.7.1 of the Bash manual, variable assignments at the beginning of a command line should be visible to the invoked program.

e.g.

DIR=/tmp ls $DIR

should behave as if I've typed "ls /tmp" - and the variable DIR should not persist after executing the command.

Cygwin Bash (GNU bash, version 3.2.51(24)-release (i686-pc-cygwin)) appears not to do this - the above command behaves as if $DIR is not defined. This is confirmed by other tests such as "DIR=/tmp echo $DIR", "DIR=/tmp set" etc.

Note that adding a semicolon works ("DIR=/tmp ; ls $DIR"), but leaves the variable defined after the command.

Why is this not working as expected?

Upvotes: 3

Views: 1365

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753465

It does work - but not in the context that you are trying to make it work.

DIR=/tmp ls $DIR

The environment variable DIR is set for ls - but is not set when the shell expands the $DIR of the command. This is the way the Bourne shell behaved; it is the way its successors such as the Korn shell and Bash behave.

You could see that DIR is set by changing ls $DIR to env; that would show the environment of an external (not built-in) command.

In this example, think about it for a moment: what you've typed is 9 extra characters compared with:

ls /tmp

If you must have it set and removed, then this does the trick:

(DIR=/tmp; ls $DIR)

The variable is set before the shell evaluates ls $DIR, but the whole command is run in a sub-shell so it has no impact on the invoking shell.

Upvotes: 3

Related Questions