|
From: SQ on 27 Jan 2006 12:02 I have $1, $2, $3 command line arguments that remain after a script is called. If the next script is called without command line arguments, these $1, $2, $3 are still in the environment and are evaluated by getops. Is there any way to clear these positional arguments? echo $1 $2 $3 -abc -def -ghi $ unset 1 ksh: 1: is not an identifier
From: Stephane Chazelas on 27 Jan 2006 12:10 On 27 Jan 2006 09:02:12 -0800, SQ wrote: > > I have $1, $2, $3 command line arguments that remain after a script is > called. > > If the next script is called without command line arguments, these $1, > $2, $3 > are still in the environment and are evaluated by getops. Is there any > way > to clear these positional arguments? > > echo $1 $2 $3 > -abc -def -ghi > > $ unset 1 > ksh: 1: is not an identifier [...] I don't understand your issue. But to clear the positionnal parameters, you can either do: shift "$#" or set -- If you want to make so that $2 now becomes $1 (and $3, $2...) it's simply: shift If you want to run getopts twice in a script with a different set of positional parameters, remember to reset OPTIND to 1 (OPTIND=1). -- Stephane
From: Chris F.A. Johnson on 27 Jan 2006 12:34 On 2006-01-27, SQ wrote: > > I have $1, $2, $3 command line arguments that remain after a script is > called. > > If the next script is called without command line arguments, these > $1, $2, $3 are still in the environment and are evaluated by getops. > Is there any way to clear these positional arguments? Unless you a) source the next script, or b) place the arguments on the command line, they will not be passed to another script. > echo $1 $2 $3 > -abc -def -ghi > > $ unset 1 > ksh: 1: is not an identifier To remove the first positional parameter ($1): shift To remove them all: shift $# Or use set: set -- -- Chris F.A. Johnson, author | <http://cfaj.freeshell.org> Shell Scripting Recipes: | My code in this post, if any, A Problem-Solution Approach | is released under the 2005, Apress | GNU General Public Licence
|
Pages: 1 Prev: string comparison problem in bash 3.00 Next: "or" in bash if statements |