14

How do I do this:

set foo bar baz
unset # Something else here
echo $# # Should ouput 0

By doing set "", $# will still be 1 and not 0.

2 Answers 2

19

Ok this was just stupid, and I found out using set -- will work.

4
  • @Gilles: According to the bash man page, set - with no arguments leaves the positional parameters unchanged.
    – camh
    Aug 19, 2011 at 3:07
  • @camh I must disagree. I see this in the manual: set -- If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters are set to the args, even if some of them begin with a -.
    – kdubs
    May 8, 2019 at 12:33
  • @kdubs you are quoting the man page for set --. I have used set - (single hyphen), which says: "If there are no args, the positional parameters remain unchanged."
    – camh
    May 8, 2019 at 13:00
  • you are right. I got them confused. my screen made your - look like --
    – kdubs
    May 14, 2019 at 1:31
6

Easily unset all positional parameters with:

shift $#

To remove the first 3 positional parameters:

shift 3

Shift "pops" off the first N positional parameters. By using $# we get the total number of positional parameters. The command thus pops off all the positional parameters.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .