Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of parameter expansion

I always thought that with a parameter expansion such as $foo, the shell replaces $foo with the contents of the parameter before executing the command. If, for example, I set

foo='`'

Why then, does

ls $foo

have different behavior than

ls `

?


1 Answers

bash has several distinct phases in the processing of a command line. Relevant here are the parsing phase and the expansion phase. The phases occur in order, and one phase does not begin until the previous phase completes.

In the first example, the parser never sees the backquote; that is supplied by the expansion of $foo which occurs after parsing is complete. As a result, a literal backquote is passed to ls as an argument.

In the second example, the parser sees an unquoted backquote, which indicates the beginning of a command substitution.

like image 151
chepner Avatar answered Jan 19 '26 15:01

chepner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!