j2emanue
j2emanue

Reputation: 62519

alias wont work - embedding a quoted string in an alias definition

Im trying to make an alias on my mac terminal in zsh shell.

some info when i do echo $SHELL i get /bin/zsh proving im using the zsh shell.

mac has a command you can use to open things in chrome like this :

open -a "google chrome" myfile.html

I need to make an alias of this but it wont work but other aliases work.
i would like the alias to run like this

chrome myfile.html

here is my alias file:

alias x="echo \'y "
alias chrome="open -a \'google chrome\' "

so if i run x at the prompt y does get outputed but if i run chrome at the command line it says it cant find the file. Do i have to use xargs or something ?

Upvotes: 1

Views: 145

Answers (2)

mklement0
mklement0

Reputation: 437478

Note: This answer applies not only to zsh, but to bash and ksh as well.

Try:

alias chrome='open -a "Google Chrome"'

By using single quotes around the entire definition (which is generally preferable when defining an alias, so as to prevent premature expansion of the string at alias definition time rather than at execution time), you can use double quotes inside without escaping.


The problem with your definition was that \' literally became part of the alias (rather than just ') - there is no need to escape ' in a double-quoted string literal.

Thus, you can also use the following (but, as stated, using single quotes to enclose the entire definition is preferable):

alias chrome="open -a 'Google Chrome'"

Upvotes: 3

mata
mata

Reputation: 69032

You don't need to quote the single quotes within the double quotes, otherwise you'll end up with literal quotes. Just try it with echo:

$ alias chrome="echo open -a \'google chrome\' "
$ chrome
open -a 'google chrome'

The quotes that are shown here are literal parts of the argument.

Just use one of:

alias chrome="open -a 'google chrome'"
alias chrome='open -a "google chrome"'
alias chrome="open -a \"google chrome\""

All of them are equivalent.

Upvotes: 1

Related Questions