MACSCRPT Archives

August 2007

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
John Baxter <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Fri, 3 Aug 2007 08:37:12 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (71 lines)
On Aug 2, 2007, at 9:40 PM, David Livesay wrote:

> On Aug 2, 2007, at 11:58 PM, John Baxter wrote:
>
>> As to your missing whitespace, you could try replacing the
>> echo $i
>> with
>> echo "$i"
>>
>> That has to be double-quotes--single quotes would cause your  
>> output to be a series of lines containing the literal $i
>
> Keeping all the different quotes straight--single quotes, double  
> quotes, backquotes--has to be one of the hardest things about  
> learning shell scripting.
>
>> That should present the whole line to echo as a single argument,  
>> and in that case echo won't mess with whitespace inside the  
>> argument.  With just $i, the $i is indeed the whole line, but echo  
>> sees it as a series of arguments (word-like things), and emits  
>> them with a space separating them.
>
> It's not what echo is doing to i; it's how for is interpreting the  
> output of cat and putting it into i. When you pass a list to for,  
> that list can be space-, tab- or return-delimited, or any  
> combination thereof, so it sees each line as a list if it has  
> spaces in it.

Compare these two:

$echo This     is      a     test.
This is a test.
$echo "This     is      a     test."
This     is      a     test.


In the first, echo gets four "words"---in the second it gets one.

In the loop in your original message, echo has no idea about anything  
named $i--it only knows what the system has given it--a bunch of  
"words" when the quotes were absent vs a single "word" with the  
quotes present (it doesn't see the quotes).  Thus...

$i="This     is      a     test."
$echo $i
This is a test.
$echo "$i"
This     is      a     test.

(In all cases there INCLUDING the assignment, the leading $ on the  
lines that have it is the shell prompt.  So in the first one, the  
command is
    i = ....
)

Continuing...
$j="'Twas brillig,               and the slithy toves"
$echo "$i" "$j"
This     is      a     test. 'Twas brillig,               and the  
slithy toves
$echo "$i$j"
This     is      a     test.'Twas brillig,               and the  
slithy toves

and even
$echo "$i      $j"
This     is      a     test.      'Twas brillig,               and  
the slithy toves

    --John

ATOM RSS1 RSS2