Skip to main content
added 3 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

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.

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 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.

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.

Typo fixes
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

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. ImI'm testing some behaviours with non-exported variables, and what I dontdon't understand is how does it workworks 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 comandscommands, with (apparently) no parameter assignments prefixed.

PS: IveI've tried in bash 4.4.20 and 5.1.16, having the same behaviour on both.

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. Im testing some behaviours with non-exported variables, and what I dont understand is how does it work 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 comands, with (apparently) no parameter assignments prefixed.

PS: Ive tried in bash 4.4.20 and 5.1.16, having the same behaviour on both.

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 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.

Became Hot Network Question
deleted 11 characters in body
Source Link
Loading
added 17 characters in body
Source Link
Loading
Source Link
Loading