0

Basically I have the next code:

  unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
  export AWS_ACCESS_KEY_ID=$(echo $OUTPUT_ROLE | jq -r '.Credentials''.AccessKeyId');\
  export AWS_SECRET_ACCESS_KEY=$(echo $OUTPUT_ROLE | jq -r '.Credentials''.SecretAccessKey');\
  export AWS_SESSION_TOKEN=$(echo $OUTPUT_ROLE | jq -r '.Credentials''.SessionToken');

  echo "AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID"
  echo "AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY"
  echo "AWS_SESSION_TOKEN: $AWS_SESSION_TOKEN"

in my bash script it starts with: #!/usr/bin/env sh

in the console after the script ends I can see the respective env vars with the correct values, such as:

AWS_ACCESS_KEY_ID: <value>
AWS_SECRET_ACCESS_KEY: <value>
AWS_SESSION_TOKEN: <value>

But if I do echo $AWS_ACCESS_KEY_ID for example direct in the console, or any other var y got an empty value, it seems like the variables are not set correctly in the current bash session and I am not able to execute new commands related with these vars.

Also doing other tests it seems like unset is not working properly neither, any guidance, I'm not able to figure out what is going on.

I am executing the script with ./script.sh also with source script.sh and bash script.sh with same results. I am using the git bash console for windows.

I did a short example such as:

var="Hello"
echo "start:$var"
# Delete the var
unset var
echo "final:$var"
var="new"
echo "new:$var"

the output is after execution ./example1.sh is:

start:Hello
final:
new:new 

But if I do echo $var direct in the console, after the script execution I got empty using both formats #!/usr/bin/env bash and #!/usr/bin/env shh.

7
  • 1
    Initial issue with the title and tagging: sh is not bash. A sh script is not a bash script. If you want bash features, you need to use bash (#!/usr/bin/env bash), not sh (#!/usr/bin/env sh). Commented May 7, 2024 at 23:35
  • Separate issue, which may or may not have any impact depending on how your input is formatted: Always quote your expansions: echo "$OUTPUT_RULE", not echo $OUTPUT_RULE -- see I just assigned a variable, but echo $variable shows something else! Commented May 7, 2024 at 23:37
  • 2
    Beyond that, of the three uses you show, only source script.sh is expected to set variables in the parent shell. Running ./script.sh or bash script.sh runs the script inside a new interpreter, sets the variables there... and then throws away the variables (and the effects of operations like unset) when that new interpreter exits at the end of the script. Commented May 7, 2024 at 23:37
  • 1
    Anyhow -- a good place to start would be to demonstrate with a transcript source not working; since two of your three usage attempts are things that aren't expected to work in the first place, a transcript showing how you tested the one that is expected to work would be a good place to start. Consider using set -x to enable trace logging, and possibly setting a PS4 that logs the PID of the current process so it can be determined when process boundaries are crossed. Commented May 7, 2024 at 23:39
  • 1
    When you set the variable in a child process, how should the parent process be aware of it? An environment variable is accessible in the process, where it is set, and is by default copied to its child processes (but even this can be suppressed). You are doing it the other way round: Setting it in a script, and trying to access it in the parent process of the script. Commented May 8, 2024 at 10:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.