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.


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

| improve this answer | |
  • @Gilles: According to the bash man page, set - with no arguments leaves the positional parameters unchanged. – camh Aug 19 '11 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 '19 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 '19 at 13:00
  • you are right. I got them confused. my screen made your - look like -- – kdubs May 14 '19 at 1:31

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.