Re: Problem Escaping osascript Command
Re: Problem Escaping osascript Command
- Subject: Re: Problem Escaping osascript Command
- From: "Mark J. Reed" <email@hidden>
- Date: Wed, 23 Jan 2008 06:53:33 -0500
> Rick Gordon wrote:
>
> > I don't understand how it can start with an unescaped apostrophe but end with an escaped one.
The shells actually don't have any way to insert a literal apostrophe
inside of a single-quoted string; single-quoted strings are 100%
literal, with no escape sequences at all that are recognized by the
shell. This simple truth is obscured by the fact that single quotes
are frequently used to *send* escape sequences unmolested to other
commands, like echo.
So if you want to use single quotes but also output single quotes, you
have to take advantage of another shell feature - automatic
concatenation. In the shells, unlike most programming languages,
quoted strings are not language tokens. That is, they don't begin or
end a new "word". All they do is switch among three different ways of
interpreting individual characters - normal, double-quoted,
single-quoted. The upshot is that you can jam things together
willy-nilly and it still works - here's a shell version of "Hello,
world", the hard way. Paste it into Terminal and it works:
e\c'h'"o" 'Hel'l"o, W"\o'r'l"d"
The secret to "quoting the single quote" then is that you don't.
Instead, you close the single-quoted string (first apostrophe) and
then use the \' sequence (now outside of any quotes) to add the
literal apostrophe. If that apostrophe is the end of the string, you
can just stop there; if not, go back into single quotes (third
apostrophe). The end result is the odd-looking sequence '\''.
Example:
echo 'This string ends with a '\''quoted quote'\'
That looks unbalanced (unescaped open quote, escaped close quote), but
it's not. it's actually four different strings, and the quoted ones
are balanced:
1. 'This string ends with a'
2. \'
3. 'quoted quote'
4. \'
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden