Subject: Re: bug#76768: BUGGY! "cut" AND "printf" in func arg >> $@

From: Bernhard Voelker <[email-masked]>
Date: Fri, 7 Mar 2025 08:51:14 +0100

[re-adding the list]

On 3/6/25 15:32, G. K. wrote:
> hi bernhard voelker/berny,
>
> an explanation in connection with the "cut" program for displaying the files in the current directory, I had to
> specifically exclude that in your answer.
>
> you said that the problem could be the quotation marks that are missing for the echo, I had commented out this
> instruction completely.
> the listing of files in the workdir remained.
>
> for testing with the c14 program.
> the "cut" command is solely for the variable as an array via the "for" loop and is re-instructed each time with the counter.
>
>   TEST PROGRAM
>
> declare -a local svn='' echo -n '------*' > 1234567 for i in `seq 7` do svn[$i]=`cut -b$i 1234567` # # echo ${svn[$i]} done

This one is messed up.  I don't know what email program you are using, but that
effectively kills proper syntax.  Please fix this.

This is probably what it should look like:

   $ cat x
   #!/bin/bash
   declare -a local svn=''
   echo -n '------*' > 1234567
   for i in `seq 7`
   do
       svn[$i]=`cut -b$i 1234567`
       # # echo ${svn[$i]}
   done

Now, let's run it with the debug flag -x:

   $ bash -x ./x
   + declare -a local svn=
   + echo -n '------*'
   ++ seq 7
   + for i in `seq 7`
   ++ cut -b1 1234567
   + svn[$i]=-
   + for i in `seq 7`
   ++ cut -b2 1234567
   + svn[$i]=-
   + for i in `seq 7`
   ++ cut -b3 1234567
   + svn[$i]=-
   + for i in `seq 7`
   ++ cut -b4 1234567
   + svn[$i]=-
   + for i in `seq 7`
   ++ cut -b5 1234567
   + svn[$i]=-
   + for i in `seq 7`
   ++ cut -b6 1234567
   + svn[$i]=-
   + for i in `seq 7`
   ++ cut -b7 1234567
   + svn[$i]='*'

In the last line, we see the proof that the output of 'cut -b7' is a literal '*',
which then gets assigned to the svn array element.

If you un-comment the echo, then you'll see that the shell will expand the unquoted * to all
files in the current directory and pass it to echo which in turn outputs their names.

   ...
   ++ cut -b7 1234567
   + svn[$i]='*'
   + echo 1234567 fileA fileB fileC x
   1234567 fileA fileB fileC x

With proper quoting ...

   echo "${svn[$i]}"

... it would instead output a literal '*' again.

   ++ cut -b7 1234567
   + svn[$i]='*'
   + echo '*'
   *

> in general, no error can be seen in all the help and examples relating to variables and their arrays.
>
> the contents of a new file are inserted using the "echo" program.
>
> an asterisk is the last character of seven.
>
> in the sequenced "for" loop, "cut" is added to the index with the sequence statement as a counter for the byte in the
> array of the variable.
>
> from this perspective, there is no asterisk or list of files in index seven of the array of the variable. the "cut"
> program generates the display itself and takes over. this confirms that the variable is empty with an output via the
> array index.

no, see above.

> the program command as an instruction in the input prompt
>
> with "cut -b7 1234567" displays the asterisk from the file '1234567' without any problems.
>
> the error that I believe can be identified can only be seen between the two, that of executing "cut" within the indexed
> array variable and in the entry for this.
>
> what is wrong with the fact that an entire work directory and all the files in it are listed with an asterisk when
> reading the file?
>
> in comparison, shell instructions and programs like "ls" are necessary. that is more like foreign and suspicious to me.
>
> then have a nice day

Again, this is no issue with any of the GNU coreutils, and I'd say not even an issue
with the shell being used, but simply a misunderstanding on how quoting has to be
used in shell syntax.

Have a nice day,
Berny