Centos – Use of Export in Bash Script on CentOS

amazon-web-servicesbashcentosenvironment-variablesexport

I am very new to Linux and am having problems with what should be a very simple Bash script in CentOS.

#! /bin/bash
# script to restore the cognos rds from snapshot
export AWS_RDS_HOME=/opt/aws/apitools/rds
export PATH=$PATH:$AWS_RDS_HOME/bin
export AWS_CREDENTIAL_FILE=$AWS_RDS_HOME/credential-file-path.template
echo $AWS_RDS_HOME
echo $PATH
echo $AWS_CREDENTIAL_FILE
rds-delete-db-instance mydb --final-db-snapshot-identifier mydb-daily-$(date +%Y)-$(date +%m)-$(date +%d) --force -region eu-west-1

I added the echo statements so I could see what was going on. The output when I run with

sh myscript.sh

is:

/opt/aws/apitools/rds
/bin/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/apitools/rds
/credential-file-path.template
rds-delete-db-instance: command not found

So it would appear that the export commands are not working when I reference other environment variables which have been set in the same script.

Where am I going wrong?

Thanks

Update: I've tried the suggestions below but stll no joy. Running bash -x was interesting. Could the "\r"s below whenever I try to concatenate be behind this? e.g.

#! /bin/bash
# test script
export AWS_RDS_HOME=/opt/aws/apitools/rds
export PATH=$PATH:$AWS_RDS_HOME/bin
export AWS_CREDENTIAL_FILE=$AWS_RDS_HOME/credential-file-path.template
echo $AWS_RDS_HOME
echo $PATH
echo $AWS_CREDENTIAL_FILE

Results in:

+ export $'AWS_RDS_HOME=/opt/aws/apitools/rds\r'
+ AWS_RDS_HOME=$'/opt/aws/apitools/rds\r'
+ export $'PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/apitools/rds\r/bin\r'
+ PATH=$'/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/apitools/rds\r/bin\r'
+ export $'AWS_CREDENTIAL_FILE=/opt/aws/apitools/rds\r/credential-file-path.template\r'
+ AWS_CREDENTIAL_FILE=$'/opt/aws/apitools/rds\r/credential-file-path.template\r'
+ echo $'/opt/aws/apitools/rds\r\r'
/opt/aws/apitools/rds
+ echo $'/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin:/opt/aws/apitools/rds\r/bin\r\r'
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home//binuser/bin:/opt/aws/apitools/rds
+ echo $'/opt/aws/apitools/rds\r/credential-file-path.template\r'
/credential-file-path.template

I'm using the default Amazon Linux AMI on EC2 so there shouldn't be anything funky with my settings

Best Answer

You're executing the script by running "sh myscript.sh". If you invoke bash with "sh", bash will try "to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well" (according to the man page for bash, in the Invocation section).

The historic Bourne shell doesn't recognize "export VARIABLE=value". The typical way of exporting variables in Bourne shell is "VARIABLE=value ; export VARIABLE", i.e., the setting of the value and the export are separate commands. Doing it all as one command is a bash-ism.

So, you can try invoking your script as "bash myscript.sh", which should work (it works for me, though admittedly it also works with the "sh myscript.sh" invocation; there might be something funny with my shell settings somewhere, or something funny in yours). You can also make the file executable with "chmod +x myscript.sh" and just run it directly with "./myscript.sh" since it should invoke bash, according to the "#!" in the first line.