I add environment value in ~/.profile
export TEST1="abc"
I execute bash shell script go.sh
#!/bin/bash
source ~/.profile
but result of go.sh is not working.(can't find TEST1 variable).
set | grep TEST1
Otherwise when I execute go.sh in command line, it working. (can find TEST1) Why go.sh is not working?
答案 0 :(得分:1)
When you execute your script, a new process is created with its own environment. When you get the prompt back, that script is finished, and you cannot access its variables.
You have to source your script:
source go.sh
If all you want is a shortcut for reading the profile, use an alias
:
alias go='source ~/.profile'
答案 1 :(得分:0)
A script cannot affect the environment of its parent shell.
You can see the changes if you include the test in the script:
#!/bin/bash
source ~/.profile
set | grep TEST1
Or when you perform the source ...
in your current shell
$ source ~/.profile
$ set | grep TEST1
答案 2 :(得分:-1)
the command
set|grep TEST
should works even without $
If you're using bash, try to put your variable in .bashrc
in any case if you try to set the variable via command line it should works too.
bash-3.2$ export TEST1="abc"
bash-3.2$ set | grep TEST1
TEST1=abc
_=TEST1
bash-3.2$ set | grep $TEST1
TEST1=abc
bash-3.2$ echo $TEST1
abc
bash-3.2$