1

I am getting an error message for this alias:

alias ejectall='osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)''

And here is the error message:

-bash: .bashrc: line 15: syntax error near unexpected token `('
-bash: .bashrc: line 15: `alias ejectall='osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)'''

I got many aliases there, but i cannot understand this message! I found many websites with lots of different suggestions of the syntax of the String.

I am on OS X Mavericks (10.9.3).

My question is:

How to find out my bash version, and depending on that, the right syntax for my bash files?

ohboy21
  • 113

2 Answers2

2
bash --version

alias ejectall=$'osascript -e \'tell application "Finder" to eject (every disk whose ejectable is true)'\'
Hauke Laging
  • 90,279
2

The problem is your quoting—and it's irrelevant which version of bash you have (though you could run bash --version or echo "$BASH_VERSION" to find out). Those quoting rules haven't changed.

'-quotes are simple. They just quote until the next '. So in your example:

alias ejectall='osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)''
               ^             ^ 
            start           end

In other words, starting with "tell application" isn't quoted, to the shell.

You could do:

alias ejectall='osascript -e '\''tell application "Finder" to eject (every disk whose ejectable is true)'\'

Basically, that ends the quoted string, puts in a single quote (with \'), then starts a new quoted string. The adjacent quoted strings get merged together since there isn't a space. You can see:

$ echo 'I'\''ll work'
I'll work

Your other option is to use "-quotes, then you need to escape other other double-quotes with \" and also escape dollar signs with \$.

$ echo "That \"is\" \$3.00."
That "is" $3.00.

Or, as Hauke Laging points out in his answer, you can use Bash's $'-strings. Those enable various escape sequences inside a '' string, including \'.

All of this is detailed under in the "Quoting" section in the bash manpage.

derobert
  • 109,670
  • hm, who should i give my correctness? ;) Thanks a lot! I don't know why there are so many websites with "false" versions of this... i copied many examples, changed them, all wrong.... – ohboy21 May 27 '14 at 15:56