10

Why does SOMETHING=1 && echo $SOMETHING need && to return 1
Why doesn't SOMETHING=1 echo $SOMETHING return 1

6

Because bash does variable expansion before interpreting variable assignment statements. So, since SOMETHING was not previously defined, your command becomes…

SOMETHING=1 echo ''

…then gets executed.

  • Is variable expansion the same as variable substitution? – Joel Apr 30 '15 at 20:55
  • 1
    @JoelL: Correct – Gordon Davisson May 1 '15 at 3:36
0

export and echo at the same time

root@kali:~# echo ${SOMETHING=1}
1
root@kali:~# echo $SOMETHING
1
root@kali:~# unset SOMETHING
root@kali:~# echo $SOMETHING

root@kali:~# echo ${SOMETHING=1}
1
root@kali:~# 

Another goofier example xD

root@kali:~# echo ${SOMETHING=1} ${PLUS=+} ${SUMTHIN=2} ${EQUALS==} && expr $SOMETHING $PLUS $SUMTHIN
1 + 2 =
3
root@kali:~# 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.