f



[why are trailing '\n's missing in "$x"]: x="$(printf "hi\n\n")"; printf "$x"

Ex
  
  ~ $ printf "hi\n\n"|cat -A
  hi$
  $
  ~ $ x="$(printf "hi\n\n")"
  ~ $ printf "$x"|cat -A
  hi~ $

Why are the trailing '\n's not in "$x"?  Is there any way to stop this,
w/o using a tempfile?

--
thanks!

0
anonb6e9
12/29/2013 10:34:45 PM
comp.unix.shell 15484 articles. 3 followers. Post Follow

14 Replies
1464 Views

Similar Articles

[PageSpeed] 9

On 12/29/2013 11:34 PM, Name withheld by request wrote:
> x="$(printf "hi\n\n")"

x="$(printf "%s" "hi\n\n")"

Best regards,
Cyrus
0
Cyrus
12/29/2013 10:56:05 PM
In article <bibnk6Ftcm6U1@mid.individual.net>,
Cyrus Kriticos  <cyrus.kriticos@googlemail.com> wrote:
>On 12/29/2013 11:34 PM, Name withheld by request wrote:
>> x="$(printf "hi\n\n")"
>
>x="$(printf "%s" "hi\n\n")"
>
>Best regards,
>Cyrus

Which is, at best, a pedantic nitpick, having nothing to do with OP's
problem.

In fact, ITYM:

x="$(printf "%s\n\n" "hi")"

(which still doesn't solve OP's problem, but at least demonstrates better
pedantry)

-- 
Watching ConservaLoons playing with statistics and facts is like watching a
newborn play with a computer.  Endlessly amusing, but totally unproductive.
0
gazelle
12/29/2013 11:00:46 PM
On 12/30/2013 12:00 AM, Kenny McCormack wrote:
> In article <bibnk6Ftcm6U1@mid.individual.net>,
> Cyrus Kriticos  <cyrus.kriticos@googlemail.com> wrote:
>> On 12/29/2013 11:34 PM, Name withheld by request wrote:
>>> x="$(printf "hi\n\n")"
>>
>> x="$(printf "%s" "hi\n\n")"
>>
>> Best regards,
>> Cyrus
>
> Which is, at best, a pedantic nitpick, having nothing to do with OP's
> problem.
>
> In fact, ITYM:
>
> x="$(printf "%s\n\n" "hi")"
>
> (which still doesn't solve OP's problem, but at least demonstrates better
> pedantry)
>

You are right with the syntax. Both solve the problem with a bash.

Best regards,
Cyrus
0
Cyrus
12/29/2013 11:14:53 PM
On Mon, 30 Dec 2013 00:14:53 +0100, Cyrus Kriticos
<cyrus.kriticos@googlemail.com> wrote:

> On 12/30/2013 12:00 AM, Kenny McCormack wrote:
> > In article <bibnk6Ftcm6U1@mid.individual.net>,
> > Cyrus Kriticos  <cyrus.kriticos@googlemail.com> wrote:
> >> On 12/29/2013 11:34 PM, Name withheld by request wrote:
> >>> x="$(printf "hi\n\n")"
> >>
> >> x="$(printf "%s" "hi\n\n")"
> >>
> >> Best regards,
> >> Cyrus
> >
> > Which is, at best, a pedantic nitpick, having nothing to do with OP's
> > problem.
> >
> > In fact, ITYM:
> >
> > x="$(printf "%s\n\n" "hi")"
> >
> > (which still doesn't solve OP's problem, but at least demonstrates
> > better pedantry)
> >
> 
> You are right with the syntax. Both solve the problem with a bash.
> 
> Best regards,
> Cyrus

None solve the problem, this does:

x=$(printf "%s\n\nx" "hi")
x=${x%x}
0
pk
12/29/2013 11:24:01 PM
On Mon, 30 Dec 2013 00:24:01 +0100, pk <pk@pk.invalid> wrote:

> x=$(printf "%s\n\nx" "hi")
> x=${x%x}

In a form easier to reuse for any generic command substitution:

x=$(printf "%s\n\n" "hi"; echo x)
x=${x%x}
0
pk
12/29/2013 11:33:55 PM
On 29.12.2013 23:34, Name withheld by request wrote:
> Ex
>   
>   ~ $ printf "hi\n\n"|cat -A
>   hi$
>   $
>   ~ $ x="$(printf "hi\n\n")"
>   ~ $ printf "$x"|cat -A
>   hi~ $
> 
> Why are the trailing '\n's not in "$x"?  Is there any way to stop this,
> w/o using a tempfile?

$ x=$'hi\n\n'
$ printf "$x" | cat -A
hi$
$


Janis

> 
> --
> thanks!
> 

0
Janis
12/29/2013 11:37:21 PM
On 12/30/2013 12:24 AM, pk wrote:
> On Mon, 30 Dec 2013 00:14:53 +0100, Cyrus Kriticos
> <cyrus.kriticos@googlemail.com> wrote:
>
>> On 12/30/2013 12:00 AM, Kenny McCormack wrote:
>>> In article <bibnk6Ftcm6U1@mid.individual.net>,
>>> Cyrus Kriticos  <cyrus.kriticos@googlemail.com> wrote:
>>>> On 12/29/2013 11:34 PM, Name withheld by request wrote:
>>>>> x="$(printf "hi\n\n")"
>>>>
>>>> x="$(printf "%s" "hi\n\n")"
>>>>
>>>> Best regards,
>>>> Cyrus
>>>
>>> Which is, at best, a pedantic nitpick, having nothing to do with OP's
>>> problem.
>>>
>>> In fact, ITYM:
>>>
>>> x="$(printf "%s\n\n" "hi")"
>>>
>>> (which still doesn't solve OP's problem, but at least demonstrates
>>> better pedantry)
>>>
>>
>> You are right with the syntax. Both solve the problem with a bash.
>>
>> Best regards,
>> Cyrus
>
> None solve the problem [...]

No.

$ x="$(printf "%s" "hi\n\n")"
$ printf "$x"|cat -A
hi$
$

It works fine.

Best regards,
Cyrus
0
Cyrus
12/30/2013 12:02:32 AM
anonb6e9@nyx.net (Name withheld by request) writes:

> Ex
>   
>   ~ $ printf "hi\n\n"|cat -A
>   hi$
>   $
>   ~ $ x="$(printf "hi\n\n")"
>   ~ $ printf "$x"|cat -A
>   hi~ $
>
> Why are the trailing '\n's not in "$x"?  Is there any way to stop this,
> w/o using a tempfile?

Within the $() construct above, pls consider
  printf "hi\n\n"
fixed, ie a given.
It's ok to filter it's output as in x="$(printf "hi\n\n" |FILTERHERE)".

In my real problem the output comes from a perl script instead of the
printf, and the number of trailing '\n's is *unknown*.

I'm curious why the shell is stripping the \012 chars, and only at the
end.  I tried removing '\n' from IFS and OFS.

I thought you could:

  x="$(cat ./foo)"

and be assured that

  echo "$x"|md5sum
  md5sum ./foo

would be identical. Try for example:

  md5sum  <(tail -2 /etc/group) <(printf "$(tail -2 /etc/group)")

Are there any other cases other than the trailing '\n's where
  x="$(cat ./foo)"
will not agree w/contents of file ./foo?

--
thanks
0
Am
12/30/2013 1:33:36 AM
On 30.12.2013 02:33, Am Nym wrote:
> anonb6e9@nyx.net (Name withheld by request) writes:

Are you and the OP the same poster?

> 
>> Ex
>>   
>>   ~ $ printf "hi\n\n"|cat -A
>>   hi$
>>   $
>>   ~ $ x="$(printf "hi\n\n")"
>>   ~ $ printf "$x"|cat -A
>>   hi~ $
>>
>> Why are the trailing '\n's not in "$x"?  Is there any way to stop this,
>> w/o using a tempfile?
> 
> Within the $() construct above, pls consider
>   printf "hi\n\n"
> fixed, ie a given.
> It's ok to filter it's output as in x="$(printf "hi\n\n" |FILTERHERE)".
> 
> In my real problem the output comes from a perl script instead of the
> printf, and the number of trailing '\n's is *unknown*.

If you use ksh, for example, you can also use another approach, say,

printf "hi\n\n" | read -d $'\a' x
printf "$x" | cat -A

so that you can avoid command substitution.

Janis

> 
> I'm curious why the shell is stripping the \012 chars, and only at the
> end.  I tried removing '\n' from IFS and OFS.
> 
> I thought you could:
> 
>   x="$(cat ./foo)"
> 
> and be assured that
> 
>   echo "$x"|md5sum
>   md5sum ./foo
> 
> would be identical. Try for example:
> 
>   md5sum  <(tail -2 /etc/group) <(printf "$(tail -2 /etc/group)")
> 
> Are there any other cases other than the trailing '\n's where
>   x="$(cat ./foo)"
> will not agree w/contents of file ./foo?
> 
> --
> thanks
> 

0
Janis
12/30/2013 7:51:19 AM
On Mon, 30 Dec 2013 01:02:32 +0100, Cyrus Kriticos
<cyrus.kriticos@googlemail.com> wrote:

> > None solve the problem [...]
> 
> No.
> 
> $ x="$(printf "%s" "hi\n\n")"
> $ printf "$x"|cat -A
> hi$
> $
> 
> It works fine.

I think you misunderstood the original problem.



0
pk
12/30/2013 9:05:30 AM
In article <1388356484.623199@nfs20.nyx.net>,
 anonb6e9@nyx.net (Name withheld by request) wrote:

> Ex
>   
>   ~ $ printf "hi\n\n"|cat -A
>   hi$
>   $
>   ~ $ x="$(printf "hi\n\n")"
>   ~ $ printf "$x"|cat -A
>   hi~ $
> 
> Why are the trailing '\n's not in "$x"?  Is there any way to stop this,
> w/o using a tempfile?

From the bash man page description of Command Substitution:

Bash performs the expansion by executing command and replacing the 
command substitution with the standard output of the command, with any 
trailing newlines deleted.

I don't see any way to stop it.  Quoting prevents word splitting, but 
not the trailing newline deletions.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
0
Barry
12/30/2013 3:22:09 PM
On 30.12.2013 16:22, Barry Margolin wrote:
> In article <1388356484.623199@nfs20.nyx.net>,
>  anonb6e9@nyx.net (Name withheld by request) wrote:
> 
>> Ex
>>   
>>   ~ $ printf "hi\n\n"|cat -A
>>   hi$
>>   $
>>   ~ $ x="$(printf "hi\n\n")"
>>   ~ $ printf "$x"|cat -A
>>   hi~ $
>>
>> Why are the trailing '\n's not in "$x"?  Is there any way to stop this,
>> w/o using a tempfile?
> 
> From the bash man page description of Command Substitution:
> 
> Bash performs the expansion by executing command and replacing the 
> command substitution with the standard output of the command, with any 
> trailing newlines deleted.
> 
> I don't see any way to stop it.  Quoting prevents word splitting, but 
> not the trailing newline deletions.

Indeed. What you can do, though, is to avoid command substitution and
use other methods. The previously for ksh posted alternative would be
for bash, e.g.,

printf "hi\n\n" | { IFS= read -d $'\a' x
printf "$x" | cat -A ;}

with some appropriately chosen delimiter.

Janis

0
Janis
12/30/2013 3:32:55 PM
Am Nym wrote:

> I'm curious why the shell is stripping the \012 chars, and only at the end.
> I tried removing '\n' from IFS and OFS.

I believe it's just the originally (7th ed shell, '79) intended utilization:

Make the output from a command quickly re-usable, which is usually a
one-liner.  Here it's just handy.

On the one hand, controlling this with a field separator would be a
cute idea, on the other hand it might complicate things further.

Stripping trailing \n if you have several lines usually makes much
less sense.  But well, it's sort of a hack anyway, although an
enormous useful one. perl avoided this problem, doesn't strip and
you're on the safe side, but almost always have to use chomp() to
strip it anyway.  I can see why Bourne was tempted to strip \n away
by default.
0
Sven
12/31/2013 12:33:19 AM
Barry Margolin <barmar@alum.mit.edu> writes:

> In article <1388356484.623199@nfs20.nyx.net>,
>  anonb6e9@nyx.net (Name withheld by request) wrote:
>
>> Ex
>>   
>>   ~ $ printf "hi\n\n"|cat -A
>>   hi$
>>   $
>>   ~ $ x="$(printf "hi\n\n")"
>>   ~ $ printf "$x"|cat -A
>>   hi~ $
>> 
>> Why are the trailing '\n's not in "$x"?  Is there any way to stop this,
>> w/o using a tempfile?
>
> From the bash man page description of Command Substitution:
>
> Bash performs the expansion by executing command and replacing the 
> command substitution with the standard output of the command, with any 
> trailing newlines deleted.
>
> I don't see any way to stop it.  Quoting prevents word splitting, but 
> not the trailing newline deletions.

OK, thanks much - the man page snip above 'trailing newlines deleted'
explains it!  

BTW - I am the original poster.

--
Thanks again to all that helped.
0
Am
12/31/2013 12:36:11 AM
Reply: