1

I have a file called env.sh:

#!/bin/bash
AWS_ACCESS_KEY_ID=******
AWS_SECRET_ACCESS_KEY=******
AWS_DEFAULT_REGION=us-east-1

I have the original script file test.sh:

#!/bin/bash
source env.sh

echo -n "Enter the Environment name you wish to choose [test|test1]: "
read env

case $env in

  test)
      export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
      export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
      export AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION
     echo "The values of ${env} are exported"
    ;;

  test1)
     export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
      export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
      export AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION
     echo "The values of ${env} are exported"
     ;;
  *)
    echo "***testing**"
   
    ;;
esac

It looks like the script is running in a child process. Is there a way to export the values?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Kalyan
  • 11
  • 1
  • 5
  • if you do not want to be ran in a child process, you need to use the command 'source' as you did for the env.sh; by default, any script is executed in a subshell. – OznOg Feb 08 '21 at 13:55
  • Thank you for info. But qucik question How can i export these values in the subshell ? – Kalyan Feb 08 '21 at 14:01
  • 1
    When you want to use the new settings, don't do `./test.sh; ./my_aws_script.sh`, that won;t help. You need `source ./test.sh; ./my_aws_script.sh`. – Walter A Feb 08 '21 at 14:04
  • c.f. https://ss64.com/bash/source.html – Paul Hodges Feb 08 '21 at 14:10
  • You might want to consider using the standard aws config and then use an AWS_PROFILE var instead. https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html. Heres how I switch around https://github.com/sanguis/.sanguis_settings/blob/master/zshrc#L162 – Josh Beauregard Feb 08 '21 at 14:11
  • 1
    @WalterA Thank you for your help !! I appriciate it sir it worked – Kalyan Feb 08 '21 at 14:22
  • You cannot pass environment variables from child process to parent. – M. Nejat Aydin Feb 08 '21 at 14:22
  • @Kalyan : _It looks like the script is running in a child process_ You did not specify, **how** you executed this script, so, technically speaking, this can't be answered. – user1934428 Feb 09 '21 at 09:03

0 Answers0