Re: Shell Script's
Re: Shell Script's
- Subject: Re: Shell Script's
- From: "Mark J. Reed" <email@hidden>
- Date: Mon, 10 Mar 2008 08:47:26 -0400
As Skeeve so ably demonstrated, the two quotation marks are treated
differently by the shell.
First, in the shell, everything is a string automatically. Setting
aside some internal optimization for arithmetic in modern shells,
"string" is in fact the *only* data type. So you don't need quotation
marks for their usual programming function of marking strings. The
following three command lines are completely equivalent in that the
command being invoked (echo) will receive the same argument and have
no way of determining which form was used:
echo foo
echo "foo"
echo 'foo'
However, whitespace is significant to the shell. It's how it knows
where the command name stops and the arguments start, and it also
separates multiple arguments from each other. So this command:
touch foo bar
Will create two empty files, one named "foo" and one named "bar".
What if you want to create a single file with a space in the name? As
a rule Unix folks avoid such names, but Mac folks love 'em. You do
that by quoting the space in one of several ways:
touch foo\ bar
touch foo" "bar
touch foo' 'bar
Those are, again, all equivalent. But quotes in the middle look
strange to those used to English or other programming languages, so
most folks would type one of these instead:
touch "foo bar"
touch 'foo bar'
So far, the two quotation marks work the same, but there are important
differences. Double quotes prevent whitespace separation and wildcard
expansion, but *other shell rewriting still takes place*. So in
Skeeve's example:
echo "$PATH"
The $PATH is replaced by the value of the PATH variable, despite the
quotes. For this reason, double quotes mostly serve to group things
together that would otherwise be separated by space, and are therefore
sometimes called "grouping quotes".
Single quotes, on the other hand, cause the shell to do absolutely no
interpretation of their contents, but pass the text on verbatim to the
invoked command. There is not even any way to include a single quote
inside a single-quoted string; fortunately you can just concatenate
such strings with single quotes that are outside of but next to them,
as needed.
That's why "quoted form of" uses single quotes; they keep the shell
from doing anything at all funky with the result.
On 3/10/08, Skeeve <email@hidden> wrote:
> Paul Berkowitz wrote:
> > Since Unix shell scripts understand single 'quotes' as equivalent to
> double
> > quotes,
> Do they?
>
> $ echo '$PATH'
> $PATH
>
> $ echo "$PATH"
> /opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/bin:/sbin:/usr/bin:/usr/sbin:
> /Users/skeeve/bin:/sw/bin:/usr/local/bin:/usr/local/mysql/bin:
> /usr/local/groovy/bin:/usr/X11R6/bin
>
> Then please explain the difference. ;-)
> _______________________________________________
> 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
>
--
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