1

Basically I have a list of environmental variables that I need to set from an external file. Here's what i'm trying to do...

file: /usr/local/env

export EC2_PRIVATE_KEY=~/.ec2/pk-xxxxx.pem
export EC2_CERT=~/.ec2/cert-xxxxx.pem
export EC2_URL=https://ec2.us-east-1.amazonaws.com
export EC2_HOME=/opt/aws/apitools/ec2
export JAVA_HOME=/usr/lib/jvm/jre

file: /usr/local/test.sh

source /usr/local/env
ec2-describe-instances

When i run /usr/local/test.sh i get /bin/ec2-cmd: No such file or directorybe-instances: line 11: /opt/aws/apitools/ec2

So i check the variables and notice that when:

[root@ip-10-244-17-106 ~]# set | grep EC2
EC2_AMITOOL_HOME=/opt/aws/amitools/ec2
EC2_CERT=$'~/.ec2/cert-xxxxx.pem\r'
EC2_HOME=$'/opt/aws/apitools/ec2\r'
EC2_PRIVATE_KEY=$'~/.ec2/pk-xxxxx.pem\r'

and when i run:

[root@ip-10-244-17-106 ~]# env | grep EC2
EC2_HOME=/opt/aws/apitools/ec2
EC2_URL=https://ec2.us-east-1.amazonaws.com
EC2_PRIVATE_KEY=~/.ec2/pk-xxxxx.pem
EC2_CERT=~/.ec2/cert-xxxxx.pem

Why does source wrap $'<variable>\r' around all my values? I think this is why i'm getting the error. Anyone have any help?

2
  • 2
    because you created your file on a windows based machine? Try running dos2unix /usr/local/env . Good luck. Commented Mar 9, 2012 at 0:15
  • Looks like incorrect end-of-line characters. Does your /usr/local/env by any chance comes from Windows? Commented Mar 9, 2012 at 0:17

2 Answers 2

1

Unix terminates a line with a Line Feed character, frequently described with the C escaping convention as \n. Windows terminates a line with both a Carriage Return and a Line Feed character, or \r\n. So the actual first line in your sourced file is

export EC2_PRIVATE_KEY=~/.ec2/pk-xxxxx.pem\r\n

Unix knows to recognize the \n as the end-of-line, but thinks that the \r is just more of the text from that line.

You can strip that with

dos2unix /usr/local/env

Also, some text editors on Windows also offer the option to save with \n line termination.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Yes i created the file in windows. I already had the dos2unix command running but the file is actually in a subdirectory and the command wasn't recursively running on subdirectories. THANKS!
1

You need to run dos2unix on your /usr/local/env: right now it's using Windows line-endings (carriage-return + line-feed, \r\n) instead of Linux ones (just line-feed, \n).

Also, something seems to be preventing tilde expansion; I'm not sure what the problem with that is — as far as I can see, the Bash Reference Manual doesn't list anything that would turn it off completely — but you can most likely work around it by writing $HOME instead of ~. :-/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.