lucasrizoli
lucasrizoli

Reputation: 638

Why do non-escaped, double-quoted paths work for a test, but not for an alias?

I'm trying to set an alias to a program only when there is an existing executable file at a certain path. I've been using double quotation marks for paths and aliases in all my scripts thus far, but have run across a problem using them with paths that include spaces.

For example, I've set a variable thusly: PATH_TO_SUBL="/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl". Testing it ([[ -x $PATH_TO_SUBL ]]) works, but using it in an alias doesn't (alias subl=$PATH_TO_SUBL results in bash returning /Applications/Sublime: No such file or directory).

When I set it with single quotes and escaped spaces (PATH_TO_SUBL='/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl') both the test and alias work. Why is that?

Upvotes: 1

Views: 130

Answers (1)

Mat
Mat

Reputation: 206841

Use:

alias subl="$PATH_TO_SUBL"

otherwise your your alias command will be parsed as:

alias suble=/some/thing else/here

which doesn't make a proper alias definition.

(It works without quotes in the [[ ... ]] conditional since those are processed by the shell directly with rules for it to work. It wouldn't work if you actually used test with a [ ... ] conditional.)

Upvotes: 4

Related Questions