export not working in my shell script

后端 未结 2 1554
刺人心
刺人心 2020-12-07 13:38

I have two scripts 1.sh and 2.sh.

1.sh is as follows:

#!/bin/sh
variable=\"thisisit\"
export variable

2.sh is as follows:



        
2条回答
  •  旧巷少年郎
    2020-12-07 13:47

    export puts a variable in the executing shell's environment so it is passed to processes executed by the script, but not to the process calling the script or any other processes. Try executing

    #!/bin/sh
    FOO=bar
    env | grep '^FOO='
    

    and

    #!/bin/sh
    FOO=bar
    export FOO
    env | grep '^FOO='
    

    to see the effect of export.

    To get the variable from 1.sh to 2.sh, either call 2.sh from 1.sh, or import 1.sh in 2.sh:

    #!/bin/sh
    . ./1.sh
    echo $variable
    

提交回复
热议问题