0

Why doesn't this work?

HELLO=WORLD echo $HELLO

In my shell it outputs

Fredster
  • 776
  • 1
  • 6
  • 16

2 Answers2

1

That command sets HELLO to the string WORLD only in the environment of echo. But echo ignores that environment variable and merely writes its arguments and a newline. In the shell, (presumably) the variable HELLO is not set at all, so echo just prints a single newline.

Perhaps you want:

HELLO=WORLD; echo "$HELLO"

which is two separate commands. The first sets HELLO in the shell, and the second passes that value as an argument to echo.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

The issue is that bash is evaluating and replacing $HELLO before your command executes. If you write a script like:

echo $HELLO

and run HELLO=WORLD ./script, you will see the expected output.

Carson
  • 2,700
  • 11
  • 24