Cygwin Bash command line variable assignments not working?

According to section 3.7.1 of the Bash manual, destination variables at the beginning of the command line should be visible to the program being called.

eg.

DIR=/tmp ls $DIR

should behave as if I typed "ls / tmp" - and the DIR variable should not be saved after the command is executed.

Cygwin Bash (GNU bash, version 3.2.51 (24) -release (i686-pc-cygwin)) does not seem to do this - the above command behaves as if $ DIR was not detected. Other tests, such as "DIR = / tmp echo $ DIR", "DIR = / tmp set", etc., confirm this.

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

Why is this not working as expected?

+3
source share
1 answer

It works, but not in the context in which you are trying to make it work.

DIR=/tmp ls $DIR

The DIR environment variable is set to ls- but not set when the shell extends the $ DIR command. This is what the Bourne shell does; this is how his successors, such as the Korn and Bash shell, behave.

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

In this example, think about it for a moment: you typed 9 additional characters compared to:

ls /tmp

If you must install and remove it, then this does the trick:

(DIR=/tmp; ls $DIR)

, ls $DIR, -, .

+3

Source: https://habr.com/ru/post/1766779/


All Articles