help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Printing with and without newlines


From: irenezerafa
Subject: Printing with and without newlines
Date: Sun, 14 Nov 2021 00:28:51 +0000

It works with single elements but not with array ranges

This works

      for (( j=0; j<argc; j++ )); do
        (( j+1 <= nl )) && printf '%s%s%s%s' $ctp "${aggr[j]}" $rst "$fs"
        (( j+1 > nl )) && printf '%s%s' "${aggr[j]}" "$fs"
      done

This fails

        aggr=("$@") ; nl=3 ; nk=$((nl-1))
        printf '%s%s' "${ctp}${aggr[*]:0:nk}${rst}" "$fs"
        (( nl + 1 <= $# )) && printf '%s%s' "${aggr[*]:nl}" "$fs"




‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On Saturday, November 13th, 2021 at 10:27 PM, irenezerafa 
<irenezerafa@protonmail.com> wrote:

> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
>
> On Saturday, November 13th, 2021 at 10:09 PM, irenezerafa 
> irenezerafa@protonmail.com wrote:
>
> > > > nl=$'\n'
> >
> > > > notice the $
> > >
> > > Thanks. Have done as follows
> > >
> > > nl=3
> > >
> > > printf '%s\n' "nwline: $nwline"
> > >
> > > (( nwline == 1 )) && fs=$'\n' || fs=$' '
> > >
> > > printf '%s%s' "${ctp}${@:1:nl}${rst}" "$fs"
> >
> > > But still the first three arguments get displayed on a single line
> >
> > > pfm "Mary" "had" "a" "little" "lamb"
> > >
> > > nwline: 1
> > >
> > > Maryhada
> >
> > To continue, I tried declaring an array and changing IFS in a subshell
> >
> > nwline=1 ; aggr=("$@")
> >
> > (( nwline == 1 )) && fsb='\n' || fsb=' '
> >
> > ( IFS=$fsb ; echo "${aggr[*]}" )
> >
> > But the above still wrote on the same line
>
> Then I remembered what you said and changed to
>
> nwline=1 ; aggr=("$@")
>
> (( nwline == 1 )) && fsb=$'\n' || fsb=' '
>
> ( IFS=$fsb ; echo "${aggr[]}" )
>
> This worked great and things are printed with newlines.
>
> Still, with printf '%s%s' "$@" "$fs", I do not get the same result
>
> nwline=1 ; aggr=("$@")
>
> (( nwline == 1 )) && fs=$'\n' || fs=' '
>
> ( IFS=$fs ; echo "${aggr[]}" )
>
> printf '%s%s' "$*" "$fs"
>
> Result:
>
> pfm "Mary" "had" "a" "little" "lamb"
>
> nwline: 1
>
> Mary
>
> had
>
> a
>
> little
>
> lamb
>
> Maryhadalittlelamb
>
> Using ( IFS=$fs ; echo "${aggr[@]}" ) does not produce newlines though.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]