Weird shell variable expansion in bash
5
votes
1
answer
438
views
The bash manual says "The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments".
I also understand that exported variables are by default passed to subprocesses that bash creates.
I'm testing some behaviours with non-exported variables, and what I don't understand is how it works when the assignments are followed by the
;
operator, and then some other commands are executed.
My case is the following:
VAR=hello; echo $VAR
prints "hello".
VAR=hello :; echo $VAR
prints a newline.
VAR=hello; echo $VAR; bash -c 'echo $VAR'
prints "hello" and a newline.
The question is, in the first and third command, why is VAR being expanded? They are different commands, with (apparently) no parameter assignments prefixed.
I've tried in bash 4.4.20 and 5.1.16, having the same behaviour on both.
Asked by carce-bo
(197 rep)
Feb 10, 2025, 03:27 PM
Last activity: Feb 16, 2025, 05:51 PM
Last activity: Feb 16, 2025, 05:51 PM