-1

A) first I run export a=1&&echo $a in linux terminal and I get 1.

[root@ip-172-31-16-92 ec2-user]# export a=1&&echo $a
1

B) But when I run sh -c "export a=2&&echo $a" and I still get 1 instead of 2.

[root@ip-172-31-16-92 ec2-user]# sh -c "export a=2&&echo $a"
1

What happend? If sh -c "export a=2&&echo $a" itself is a child process of the terminal,it should have its own environment, first it should make a copy of his father environment,so a=1,but when it execute export a=2 ,environment a should be set to 2,then echo $a should be 2.But it returns 1,what happend?

傅继晗
  • 927
  • 1
  • 8
  • 14
  • See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus May 30 '20 at 09:37

1 Answers1

1

Try this one and and you'll see it works.

sh -c 'export a=2&&echo $a'

When enclosed in double-quotes, $a is expanded to its current value by the shell before invoking sh.

oguz ismail
  • 1
  • 16
  • 47
  • 69