0

I am on a Mac and I am trying to use environment variables as a reference holder to run commands.

For example, I have a file with a path like so:

/Applications/Android Studio.app/Contents/Info.plist

I'm trying to read it with the cat command. From this SO question, I can either use a backslash or quotes to use that path, like so:

cat /Applications/Android\ Studio.app/Contents/Info.plist

or

cat "/Applications/Android Studio.app/Contents/Info.plist"

Fantastic!

However, say I export the path as an env var, using export:

export AS_HOME="/Applications/Android Studio.app/Contents"

and then I want to read the Info.plist file using the env var:

//Restart or refresh terminal settings

source ~/.profile

cat $AS_HOME/Info.plist

This is throwing an error where it doesn't understand the path. I've tried setting the env var both ways (one with quotes and the other using backslahses). When I use the echo command with that env var, it prints out fine..

Any thoughts/work arounds on this?

2 Answers 2

3

cat "$AS_HOME/Info.plist" should do the trick.

3

You're learning one of the first rules of safe shell scripting: Always quote your paths that contain variables. So your last example line should be:

cat "$AS_HOME/Info.plist"

Note that double-quotes allow dollar-sign expressions to still be expanded, while most other special character meanings are escaped (ignored). Single-quotes wouldn't work here because they escape dollar-sign expressions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .