Current-Users archive

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

Shell confusion over positional argumentsÉ



Hello.  A /bin/sh script I wrote about 10 years ago under NetBSD-5 broke under NetbSD-10.99.12.
The issue seems to be a change in the way command line arguments are assigned to 
the $number variables, i.e. $1, $2, $3, etc.  
I wrote two scripts, below, to illustrate the issue.  Since the new (bad) behavior is exhibited
by new versions of bash and by /bin/sh on FreeBSD-13.1, I assume it is correct.  However, I
don't understand the reason why these modern shells are mangling the command line arguments
which are assigned to the numeric variables if previous command line arguments include a hypen
"-".  Can someone explain the logic of what is going on here?  The older shell assigns the
arguments to their numeric values without changing the values of subsequent arguments.  

Any light which can be shed on this strangeness would be greatly appreciated.

-thanks
-Brian


Good (old) script
Script started on Mon Mar 10 08:43:49 2025
%uname -a
NetBSD www3.nfbcal.org 5.2_STABLE NetBSD 5.2_STABLE (NFBCAL_DOMU) #0: Fri Nov 19 09:03:13 PST 2021  buhrow%lothlorien.nfbcal.org@localhost:/usr/src/sys/arch/amd64/compile/NFBCAL_DOMU amd64
%./runtestarg
arg1 = -cmd1
arg2 = -cmd2
arg3 = cmd3
arg4 = cmd4
arg5 = cmd5
arg6 = cmd6
arg7 = cmd7
arg8 = cmd8
arg9 = cmd9
arg10 = cmd10
arg11 = cmd11
%exit
%exit

Script done on Mon Mar 10 08:44:09 2025

Bad (new) script
Script started on Mon Mar 10 08:45:09 2025
%uname -a
NetBSD lothlorien.nfbcal.org 10.99.12 NetBSD 10.99.12 (MIRKWOOD_PVH) #0: Thu Dec  5 22:47:09 PST 2024  buhrow%loth-9.nfbcal.org@localhost:/usr/src/sys/arch/amd64/compile/MIRKWOOD_PVH amd64
%./runtestarg
arg1 = -cmd1
arg2 = -cmd2
arg3 = cmd3
arg4 = cmd4
arg5 = cmd5
arg6 = cmd6
arg7 = cmd7
arg8 = cmd8
arg9 = cmd9
arg10 = -cmd10
arg11 = -cmd11
%exit
%exit

Script done on Mon Mar 10 08:45:20 2025

Here are the scripts themselves.  The runtestarg script runs the testarg script and the testarg
script generates the output you saw above.


#!/bin/sh
# Test retrieving command arguments by using numeric values.

arg1=$1
arg2=$2
arg3=$3
arg4=$4
arg5=$5
arg6=$6
arg6=$6
arg7=$7
arg8=$8
arg9=$9
arg10=$10
arg10=$10
arg11=$11

# Now, print the arguments we  received.
echo "arg1 = $1"
echo "arg2 = $2"
echo "arg3 = $3"
echo "arg4 = $4"
echo "arg5 = $5"
echo "arg6 = $6"
echo "arg7 = $7"
echo "arg8 = $8"
echo "arg9 = $9"
echo "arg10 = $10"
echo "arg11 = $11"

exit 0


#!/bin/sh

./testarg -cmd1 -cmd2 cmd3 cmd4 cmd5 cmd6 cmd7 cmd8 cmd9 cmd10 cmd11
exit 0


Home | Main Index | Thread Index | Old Index