0

I am currently trying to familiarise myself with shell line and command execution.

Could anybody explain to me the following behaviour? Why does shell only register an assignment variable when there is a separator before the command? Can assignment not be made in the same command?

sh-3.2$ x=5 echo ${x}


sh-3.2$ x=5; echo ${x}

5

sh-3.2$ x=5 && echo ${x}

5
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441

1 Answers1

0

When you don't have the separator, you're setting x as an environment variable while echo runs (only for the duration of that single command), without defining a shell variable for use in evaluating expansions before the command is started (which is what's happening in the x=5; echo "$x" case).

However, echo doesn't change its output based on which environment variables are defined (except sometimes very specific environment variables intended to modify its behavior -- POSIXLY_CORRECT and the like), so this has no visible effect.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441