how to prevent back quote expansion?

  • Follow


The following script will output one line. the carriage return is
removed by the back quote operation. Any way to prevent that carriage
is removed?

set a=`ls -al`
echo $a
0
Reply kirby 3/26/2011 7:55:36 PM

On Sat, 26 Mar 2011 12:55:36 -0700 (PDT)
kirby <chou2008@gmail.com> wrote:

> The following script will output one line. the carriage return is
> removed by the back quote operation.

False (I think).

> Any way to prevent that carriage is removed?
> 
> set a=`ls -al`
> echo $a

Don't know about C shell, but this should work on a sane shell:

echo "$a"
0
Reply pk 3/26/2011 7:51:54 PM


Forgot to mention, it is C shell.

On Mar 26, 3:55=A0pm, kirby <chou2...@gmail.com> wrote:
> The following script will output one line. the carriage return is
> removed by the back quote operation. Any way to prevent that carriage
> is removed?
>
> set a=3D`ls -al`
> echo $a

0
Reply kirby 3/26/2011 7:57:45 PM

On Sat, 26 Mar 2011 18:02:37 -0400
"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:

> On 2011-03-26, pk wrote:
> > On Sat, 26 Mar 2011 12:55:36 -0700 (PDT)
> > kirby <chou2008@gmail.com> wrote:
> >
> >> The following script will output one line. the carriage return is
> >> removed by the back quote operation.
> >
> > False (I think).
> >
> >> Any way to prevent that carriage is removed?
> >> 
> >> set a=`ls -al`
> >> echo $a
> >
> > Don't know about C shell, but this should work on a sane shell:
> >
> > echo "$a"
> 
>    That doesn't do it. Any trailing newlines are stripped by command
>    substitution.

Right. So, under which circumstances the output of "ls -al" has more than a
trailing newline?

0
Reply pk 3/26/2011 9:51:35 PM

On Sat, 26 Mar 2011 21:51:35 +0000 pk <pk@pk.invalid> wrote:

> > > echo "$a"
> > 
> >    That doesn't do it. Any trailing newlines are stripped by command
> >    substitution.
> 
> Right. So, under which circumstances the output of "ls -al" has more than
> a trailing newline?

I mean, except the pathological case where the last filename happens to
have a trailing \n in its name.


0
Reply pk 3/26/2011 9:53:19 PM

On 2011-03-26, pk wrote:
> On Sat, 26 Mar 2011 12:55:36 -0700 (PDT)
> kirby <chou2008@gmail.com> wrote:
>
>> The following script will output one line. the carriage return is
>> removed by the back quote operation.
>
> False (I think).
>
>> Any way to prevent that carriage is removed?
>> 
>> set a=`ls -al`
>> echo $a
>
> Don't know about C shell, but this should work on a sane shell:
>
> echo "$a"

   That doesn't do it. Any trailing newlines are stripped by command
   substitution. My method is:

a=`ls -al; echo .`
a=${a%?}

-- 
   Chris F.A. Johnson, author           <http://shell.cfajohnson.com/>
   ===================================================================
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

0
Reply Chris 3/26/2011 10:02:37 PM

On Sat, 26 Mar 2011 18:20:12 -0400 "Chris F.A. Johnson"
<cfajohnson@gmail.com> wrote:

> > Right. So, under which circumstances the output of "ls -al" has more
> > than a trailing newline?
> 
>     It doesn't; that's why I append another character to the command.
>     The newline from ls is no longer trailing, and is therefore
>     preserved. The extra character can then be removed with parameter
>     expansion, leaving the variable with a trailing newline.

Yes I am aware of that, but I don't think that's what the OP was referring
to, since the echo command readds the trailing newline, so he wouldn't see
it missing in the output.
I think he was asking why the other newline characters (ie those separating
the ls -l entries), and those were removed by the shell (I think) because
of the missing quotes when echoing $a.

>     Which doesn't help the OP as he wants a csh answer. There may be
>     one, but I'd prefer to convince him to switch to a real shell.

Agreed.

0
Reply pk 3/26/2011 10:09:21 PM

On 2011-03-26, pk wrote:
> On Sat, 26 Mar 2011 18:02:37 -0400
> "Chris F.A. Johnson" <cfajohnson@gmail.com> wrote:
>
>> On 2011-03-26, pk wrote:
>> > On Sat, 26 Mar 2011 12:55:36 -0700 (PDT)
>> > kirby <chou2008@gmail.com> wrote:
>> >
>> >> The following script will output one line. the carriage return is
>> >> removed by the back quote operation.
>> >
>> > False (I think).
>> >
>> >> Any way to prevent that carriage is removed?
>> >> 
>> >> set a=`ls -al`
>> >> echo $a
>> >
>> > Don't know about C shell, but this should work on a sane shell:
>> >
>> > echo "$a"
>> 
>>    That doesn't do it. Any trailing newlines are stripped by command
>>    substitution.
>
> Right. So, under which circumstances the output of "ls -al" has more than a
> trailing newline?

    It doesn't; that's why I append another character to the command.
    The newline from ls is no longer trailing, and is therefore
    preserved. The extra character can then be removed with parameter
    expansion, leaving the variable with a trailing newline.

    Which doesn't help the OP as he wants a csh answer. There may be
    one, but I'd prefer to convince him to switch to a real shell.


-- 
   Chris F.A. Johnson, author           <http://shell.cfajohnson.com/>
   ===================================================================
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

0
Reply Chris 3/26/2011 10:20:12 PM

On Sat, 26 Mar 2011 18:09:21 -0400, pk <pk@pk.invalid> wrote:

> I think he was asking why the other newline characters (ie those separating
> the ls -l entries), and those were removed by the shell (I think) because
> of the missing quotes when echoing $a.

Agreed.  From info bash ...


3.5.7 Word Splitting
--------------------

The shell scans the results of parameter expansion, command
substitution, and arithmetic expansion that did not occur within double
quotes for word splitting.

    The shell treats each character of `$IFS' as a delimiter, and splits
the results of the other expansions into words on these characters.  If
`IFS' is unset, or its value is exactly `<space><tab><newline>', the
default, then sequences of ` <space>', `<tab>', and `<newline>' at the
beginning and end of the results of the previous expansions are
ignored, and any sequence of `IFS' characters not at the beginning or
end serves to delimit words.

-----------------------------

So the new lines are removed unless the IFS is changed to not include
the new line character, or the variable is inside of double quotes.

Regards, Dave Hodgins

-- 
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)
0
Reply David 3/26/2011 11:06:07 PM

On 26.03.2011 23:09, pk wrote:
> On Sat, 26 Mar 2011 18:20:12 -0400 "Chris F.A. Johnson"
> <cfajohnson@gmail.com> wrote:
> 
>>> Right. So, under which circumstances the output of "ls -al" has more
>>> than a trailing newline?
>>
>>     It doesn't; that's why I append another character to the command.
>>     The newline from ls is no longer trailing, and is therefore
>>     preserved. The extra character can then be removed with parameter
>>     expansion, leaving the variable with a trailing newline.
> 
> Yes I am aware of that, but I don't think that's what the OP was referring
> to, since the echo command readds the trailing newline, so he wouldn't see
> it missing in the output.
> I think he was asking why the other newline characters (ie those separating
> the ls -l entries), and those were removed by the shell (I think) because
> of the missing quotes when echoing $a.

You can also do without quotes if you clear the IFS

  IFS= a=`ls -al`
  echo $a


Janis

> 
>>     Which doesn't help the OP as he wants a csh answer. There may be
>>     one, but I'd prefer to convince him to switch to a real shell.
> 
> Agreed.
> 

0
Reply Janis 3/26/2011 11:24:07 PM

>I think he was asking why the other newline characters (ie those separatin=
g
>the ls -l entries), and those were removed by the shell (I think) because
>of the missing quotes when echoing $a.
Yes. This is what I want.

unfortunately, both
echo "$a"
and
a=3D`ls -al; echo .`
a=3D${a%?}

don't do it.

Thanks any way!

On Mar 26, 6:09=A0pm, pk <p...@pk.invalid> wrote:
> On Sat, 26 Mar 2011 18:20:12 -0400 "Chris F.A. Johnson"
>
> <cfajohn...@gmail.com> wrote:
> > > Right. So, under which circumstances the output of "ls -al" has more
> > > than a trailing newline?
>
> > =A0 =A0 It doesn't; that's why I append another character to the comman=
d.
> > =A0 =A0 The newline from ls is no longer trailing, and is therefore
> > =A0 =A0 preserved. The extra character can then be removed with paramet=
er
> > =A0 =A0 expansion, leaving the variable with a trailing newline.
>
> Yes I am aware of that, but I don't think that's what the OP was referrin=
g
> to, since the echo command readds the trailing newline, so he wouldn't se=
e
> it missing in the output.
> I think he was asking why the other newline characters (ie those separati=
ng
> the ls -l entries), and those were removed by the shell (I think) because
> of the missing quotes when echoing $a.
>
> > =A0 =A0 Which doesn't help the OP as he wants a csh answer. There may b=
e
> > =A0 =A0 one, but I'd prefer to convince him to switch to a real shell.
>
> Agreed.

0
Reply kirby 3/27/2011 12:36:39 AM

This works for bash, but not for csh.
there's no IFS equivalent var for csh.
> You can also do without quotes if you clear the IFS
>
> =A0 IFS=3D a=3D`ls -al`
> =A0 echo $a
>
> Janis
>
>
>
>
>
>
>
>
>
> >> =A0 =A0 Which doesn't help the OP as he wants a csh answer. There may =
be
> >> =A0 =A0 one, but I'd prefer to convince him to switch to a real shell.
>
> > Agreed.

0
Reply kirby 3/27/2011 12:41:17 AM

On 27.03.2011 01:41, kirby wrote:
> This works for bash, but not for csh.
> there's no IFS equivalent var for csh.

This was not a response to your csh question.

(You should anyway abandon C shell scripting if you can.)

>> You can also do without quotes if you clear the IFS
>>
>>   IFS= a=`ls -al`
>>   echo $a
>>
>> Janis

0
Reply Janis 3/27/2011 12:51:04 AM

On 2011-03-27, kirby wrote:
>>I think he was asking why the other newline characters (ie those separating
>>the ls -l entries), and those were removed by the shell (I think) because
>>of the missing quotes when echoing $a.
> Yes. This is what I want.
>
> unfortunately, both
> echo "$a"
> and
> a=`ls -al; echo .`
> a=${a%?}
>
> don't do it.

  The following *does* do it (in a real shell, not csh):

a=`ls -al; echo .`
a=${a%?}
echo "$a"


-- 
   Chris F.A. Johnson, author           <http://shell.cfajohnson.com/>
   ===================================================================
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

0
Reply Chris 3/27/2011 1:53:03 AM

In article 
<0a4df1a1-d856-4979-ac4b-6c7b67c4be4b@o15g2000prn.googlegroups.com>,
 kirby <chou2008@gmail.com> wrote:

> Forgot to mention, it is C shell.

That was pretty obvious from the fact that you use the set command to 
assign a variable.

Why are you writing scripts in C shell instead of a reasonable one?

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

> 
> On Mar 26, 3:55�pm, kirby <chou2...@gmail.com> wrote:
> > The following script will output one line. the carriage return is
> > removed by the back quote operation. Any way to prevent that carriage
> > is removed?
> >
> > set a=`ls -al`
> > echo $a

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
0
Reply Barry 3/27/2011 6:48:17 AM

On 2011-03-27, Barry Margolin wrote:
> In article 
><0a4df1a1-d856-4979-ac4b-6c7b67c4be4b@o15g2000prn.googlegroups.com>,
>  kirby <chou2008@gmail.com> wrote:
>
>> Forgot to mention, it is C shell.
>
> That was pretty obvious from the fact that you use the set command to 
> assign a variable.
>
> Why are you writing scripts in C shell instead of a reasonable one?
>
> http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

    See also:

     Top Ten Reasons not to use the C shell
        <http://www.grymoire.com/Unix/CshTop10.txt>

     Csh problems
        <http://www.grymoire.com/Unix/Csh.html#uh-0>


-- 
   Chris F.A. Johnson, author           <http://shell.cfajohnson.com/>
   ===================================================================
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

0
Reply Chris 3/27/2011 6:59:32 AM

kirby <chou2008@gmail.com> writes:
> The following script will output one line. the carriage return is
> removed by the back quote operation. Any way to prevent that carriage
> is removed?
>
> set a=`ls -al`
> echo $a

It would help to know what output "ls -al" produces, but it's safe to
assume that it's at least 3 lines.  Are you referring to the carriage
return at the end of the last line, or the carriage returns at the end
of each line?

Quoting the tcsh man page:

   Command substitution
       Command substitution is indicated by a command enclosed
       in ``'.  The output from such a command is broken  into
       separate words at blanks, tabs and newlines, and null words
       are discarded.  The output is variable and command substituted
       and put in place of the original string.

       Command substitutions inside double quotes (`"') retain
       blanks and tabs; only newlines force new words.  The single
       final newline  does  not  force a new word in any case.
       It is thus possible for a command substitution to yield only
       part of a word, even if the command outputs a complete line.

       By default, the shell since version 6.12 replaces all newline
       and carriage return characters in the  command  by  spaces.
       If this is switched off by unsetting csubstnonl, newlines
       separate commands as usual.

I don't know (a) whether you're using tcsh or csh, or (b) whether
classic csh behaves the same way (it probably does, mostly).

So if "ls -al" produces, say, 10 lines of output, then

    set a = "`ls -al'"

will set a to a 10-element array, one for each line.  The newlines
aren't saved, but you can still retrieve the individual lines by
traversing the array.  For example:

    foreach elem ($a:q)
        echo "$elem"
    end

Note that replacing $a:q by "$a" does not work.

The fact that I've been using tcsh for many years, and I had
to resort to trial and error, even after reading the man page,
to write that loop, should tell you something.

Whatever it is you're doing, if I were doing it, I'd probably
use Perl.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21474) 3/29/2011 9:53:59 PM

16 Replies
462 Views

(page loaded in 0.142 seconds)

Similiar Articles:


















7/22/2012 2:49:57 PM


Reply: