0

I am trying to configure the AWS CLI using a bash script. I have the below in my script and it will not set the values. When I echo out the variables names it just shows a blank line.

script code

export AWS_ACCESS_KEY_ID=<key>

export AWS_SECRET_ACCESS_KEY=<secret_key>

export AWS_DEFAULT_REGION=<region>
  • how are you trying to print the variables ... contents (not names)? After/outside the script, or within the script? – Jeff Schaller Aug 15 '18 at 13:15
  • You have to source the script instead of running it. – Rui F Ribeiro Aug 15 '18 at 13:16
  • I am trying to echo out the variables because they are not working. I these values are to be used by the AWS cli program. I am testing the echo outside the script once it has ran. – Josh Kirby Aug 15 '18 at 13:17
  • @RuiFRibeiro What do you mean? – Josh Kirby Aug 15 '18 at 13:17
  • source script.sh or . script.sh instead of script.sh – Rui F Ribeiro Aug 15 '18 at 13:19
  • @RuiFRibeiro the above scipt code is part of script not the whole thing, also what would I need to change if I put just the above into a script and included that in my main one? – Josh Kirby Aug 15 '18 at 13:21
  • 1
    @JoshKirby, please [edit] your question to include a complete example of a script that exhibits the issue, along with a sample of how you run the script. sh -c 'export ID=foo; echo "$ID"' should work with whatever sh you have, so there's not enough information here to tell what the issue is. – ilkkachu Aug 15 '18 at 13:21
  • @JoshKirby, I will expand Rui answer: the way how you execute your script matters. If you execute like this: ./script.sh - new shell process will be spawned, and when script terminates all is gone - variables will not persist in your current shell environment. If you execute via source script.sh, the script will run in a current shell process, thus all the environment changes will remain after script stops it's execution. – Andrejs Cainikovs Aug 15 '18 at 13:21

1 Answers1

0

Superuser: what-is-the-difference-between-executing-a-bash-script-vs-sourcing-it

Short answer: sourcing will run the commands in the current shell process. executing will run the commands in a new shell process.

More info in the original question/answer

The below example shows the difference between running the script and sourceing it:

$ cat a.sh
export AWS_ACCESS_KEY_ID=key
export AWS_SECRET_ACCESS_KEY=secret_key
export AWS_DEFAULT_REGION=region
$ ./a.sh 
$ echo $AWS_ACCESS_KEY_ID

$ source a.sh $ echo $AWS_ACCESS_KEY_ID key $

Yaron
  • 4,289