Say I connected to a Linux system as root.
What exactly happens when I type unset *
?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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
HOME[$(reboot)]
in the current directory, unset *
could also attempt to reboot the system. unset
in bash
and other Korn-like shells is one of those builtins that can ran arbitrary commands, so should only be passed known and sanitised data.
Feb 18, 2021 at 7:25
unset
. Any pointers? Yes I know this could derail the discussion of the original question.
Feb 18, 2021 at 7:59
[ -v var ]
, getopts
, read
, printf -v
...) has a problem. See Security Implications of using unsanitized data in Shell Arithmetic evaluation
Feb 18, 2021 at 8:35