I have multiple Amazon EC2 accounts and want to quickly be able to switch variables, such as $EC2_HOME
, using a script.
I have have a shell script set up like this:
#!/bin/sh
export EC2_HOME=/home/me/.ec2
echo $EC2_HOME
When I run the script I know that EC2_HOME
is set, but I thought that using export
would make the variable stick around after the script completed. It does not, as running echo $EC_HOME
does not show anything.
I know this must be very rudimentary Linux scripting knowledge, but I don't know it. I tried looking for related questions without luck - so my apologies if this is a duplicate.
.
orsource
, you are not spawning a new child process, you are running the commands in the current shell. – glenn jackman Jan 27 '12 at 20:32.
orsource
. Why is this happening ? – Patryk Feb 21 '12 at 13:03exit
statement, so it is not suitable to be sourced. – enzotib Feb 21 '12 at 13:24source ./script
works completely fine,sudo source ./script.sh
sayssudo: source: command not found
. How can I do this using sudo? – 71GA Sep 27 '14 at 13:37sudo
and depending on configuration settings in/etc/sudoers
you can or cannot preserve your environment when running commands withsudo
. I suggest you to try to source your script, and then runsudo
with-E
option to preserve the environment. If it does not work, I suppose there is very little you can do. – enzotib Sep 27 '14 at 13:52sudo
to run a shell built-in (and it wouldn't make any sense if you could). – Wildcard Apr 01 '16 at 18:28$0
param - which should be the script name, but here it is the shell it self. It can produce some unexpected behavior(dirname $0)
... – Jiri Fornous May 10 '19 at 12:55