tech-userlevel archive

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

Re: interactive shell detection in shrc



On Fri, 27 Sep 2024, Steffen Nurpmeso wrote:

Yes i am afraid of shell field splitting and expansion, and i do
not understand why eg

 a() {
         echo $#,1="$1"/$1,2="$2"/$2,3="$3"/$3,4="$4"/$4,5="$5"/$5,6="$6"/$6,'*'="$*"/$*,'@'="$@",$@
         echo $#,1= "$1" , $1 , 2= "$2" , $2 , 3= "$3" , $3 , 4= "$4" , $4 , 5= "$5" , $5 , 6= "$6" , $6 , '*=' "$*" , $* , '@=' "$@" , $@
 }
 set -- '' '' ''
 a "$*"$* $*


Don't rightly see what the difficulty here is: both NetBSD's /bin/sh and bash
produce identical outputs (ie. what you would expect here):

```
$ sh ./x.sh
1,1=  / ,2=/,3=/,4=/,5=/,6=/,*=  / ,@=  ,
1,1=    , , 2=  , , 3=  , , 4=  , , 5=  , , 6=  , , *=    , , @=    ,
$ cmp <(bash ./x.sh) <(dash ./x.sh)
```

whereas dash-0.5.12 substitutes a naked `$@' with a space (which seems wrong
to me--not even sure if a bare `$@' makes sense--as opposed to `"$@"' which is
well defined).

The thing to realise in your script is that only 1 string is passed to the
a() function. The other 2 `$*' are elided since they're not in quotes and
empty. And, `"$*"' ends up (ie. as $1) to a() as `<space><space' since the original 3 args ($1, $2, $3) are empty with only the 2 separators in between
'em.

-RVP


Home | Main Index | Thread Index | Old Index