Can I unset the $1 variable?

You can't unset it, but you may shift $2 into $1:

$ set bon jour
$ echo "$1$2"
bonjour

$ shift
$ echo "$1$2"  # $2 is now empty
jour

shift will shift all positional parameters one step lower. It is common for e.g. command line parsing loops (that does not use getopt/getopts) to shift the positional parameters over in each iteration while repeatedly examining the value of $1. It is uncommon to want to unset a positional parameter.

By the way, unset takes a variable name, not its value, so unset $1 will in effect unset the variable bon (had it been previously set).


You could empty it, by re-setting the parameters to: "the empty string" followed by "the parameters, starting from the 2nd one":

$ set -- 1 2 3 4
$ printf -- "->%s<-\n" "$@"
->1<-
->2<-
->3<-
->4<-
$ set -- "" "${@:2}"
$ printf -- "->%s<-\n" "$@"
-><-
->2<-
->3<-
->4<-

Besides all the correct technical answers you should notice that there is a logical flaw in your question. $1, $2,... are not the values of named variables like $A, $B, ..., but the 1st, 2nd,... value of a list of values. Maybe the first value of a list is an empty string but it does not make sense to say my list has no first value. If a list has no first value then it has no value at all and so it is an empty list.

So you cannot "unset" the first value of a list. You can only remove the first value from a list. Then the former 2nd value of the list is now the 1st value of the list, the former 3rd is now the 2nd and so on. This is exactly what the shift operator does.

Tags:

Bash

Set

Variable