3

Say I connected to a Linux system as root.

What exactly happens when I type unset *?

0
13

I will assume that your shell is Bash, although the following should apply to most Bourne/POSIX-like shells as well.

In short, the command doesn't make sense.

The shell will first replace the asterisk * by the names of all files and directories in the current directory, except those that start with a dot .. Then it will call the unset command on that list.

The purpose of unset is to remove shell variables, not to process files. If a file or directory name happens to correspond to a currently defined shell variable, that variable will be removed. Names that don't correspond to a shell variable will either have no effect at all, or if they contain characters that are illegal for shell variables, they will generate an error message like not a valid identifier.

In reality, the unset command is a bit more complex; for example, variables can be marked as readonly, in which case they can't be unset. It also works with functions, not just variables. However, this doesn't change anything to the summary that the command doesn't make sense in normal situations.

Also, as Stéphane Chazelas' commented, like all builtins that take variable names, unset has potentially dangerous side effects, mostly with data that looks like array indexing. E.g. if a file HOME[$(some command)] existed, unset * would run the command substitution when trying to evaluate the array index. See: Security Implications of using unsanitized data in Shell Arithmetic evaluation

6

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.