unset positional parameter in shell (218 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 3 (218 Views)
Accepted Solution

unset positional parameter in shell

hello,

 

how can i unset positional parameter ?

 

unset a defined variable using the unset command:
unset variable_name

 

unset positional parameter ?

my proposal ? :

 

while [ $# -gt 0 ]
do
  shift
done

test:

 

set a b c
$ echo "$*"
abc

#positional parameters $1, $2,$3


while [ $# -gt 0 ]
do
  shift
done

 

echo "$*"

$

Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 3 (214 Views)

Re: unset positional parameter in shell

Your while-loop proposal can be simplified to:

shift $#

 

If you use the "set" command to set positional parameters, you should use "set -- <positional parameters>" to make sure you don't accidentally change any shell options.

 

An alternative way to unset all positional parameters would be:

set --

 

MK
Acclaimed Contributor
Dennis Handly
Posts: 25,188
Registered: ‎03-06-2006
Message 3 of 3 (203 Views)

Re: unset positional parameter in shell

Typically you just use shift to march through your parms.  You can use "set -A" to copy the parms to an array and then you can fiddle with that array.  (Provided there are only 1023 parms.)

And then another "set -A" to restore the changed parms.

set -A SAVEARGS -- "$@"

The proper way to reset them is:
set -- "${SAVEARGS[@]}"

 

http://h30499.www3.hp.com/t5/Languages-and-Scripting/Shift-command/m-p/4133193

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.