Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: triviality regarding $# counts



As I said about echo way back in my first response: the print command is what is turning the backslash-n sequences into newlines. If you use -r to turn off that behavior, you can see the strings unmangled:

print -r $ggg                             

 abc\n \n def\n \n ghi\n


The -r is telling print not to do something it normally does. The result is the raw string (which I think is what -r stands for).

To restate: if your goal is to print out a newline, you have options. If you do this:

print $'\n' 

Then you are creating in the shell's memory a string containing a literal newline character (one byte with value 10), which the shell then passes to print, which just echoes it verbatim, doing no translation on it whatsoever.  Job done. The translation from \n to newline was done by the shell before print ever ran.

If you instead do this:

print '\n'

Then the string you create and pass to print has no newline in it. It is not one byte of value 10, but two bytes, one with value 92 (backslash) and the second with value 110 (lowercase n).  The code implementing the print command recognizes this sequence as code for "print a newline", so it does. But that's the command doing that. The string itself has no newline in it. 



Mark J. Reed <markjreed@xxxxxxxxx>


On Sun, Apr 14, 2024 at 10:53 Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:


On 2024-04-14 07:15, Roman Perepelitsa wrote:
> On Sun, Apr 14, 2024 at 4:06 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>> % ggg=( 'abc\n' '\n' 'def\n' '\n' 'ghi\n' )
> There are no newlines in there.
% ggg=( ' abc\n' '\n' 'def\n' '\n' 'ghi\n' )

% print $ggg
  abc

  def

  ghi


% typeset -p ggg
typeset -a ggg=( ' abc\n' '\n' 'def\n' '\n' 'ghi\n' )





Messages sorted by: Reverse Date, Date, Thread, Author