Skip to main content

Is it a good practice to delete all variables at the end of a script? [Resolved]

I usually delete all the variables that I use at the end of a script.

rm -rf $abc
rm -rf $def


unset $abc
unset $def

and so on.

Is this actually a good practice?

Asked April 21, 2017
Posted Under: Unix Linux
3 Answers

This is a very bad practice.

rm deletes files. It is nothing to do with variables. In any case, the variables themselves will be disposed of when the script ends and the operating system reclaims the shell's memory.

In the simple case, a file with the same name as the value of one of the variables will be deleted by this construction:

rm -f $abc  # Deletes "filename" in the current directory

It gets worse. If abc and def contain the names of files as individual words separated by a space (or any other character of IFS), you will delete those files, and wildcards like * will be expanded too if they appear in any of the words.

abc='hello world'
rm -f $abc  # Deletes file "hello" and "world" (leaves "hello world" alone)
abc='5 * 3'
rm -f $abc  # Deletes all files, because * is expanded (!)
def='-r /'
rm -f $def  # Really deletes *all* files this user can access

Shell parameter expansion with $var is subject to word splitting, where every character of the IFS variable divides the variable into different arguments. Each word is then subject to filename expansion, which uses *, ?, and [abc...] patterns to create filenames. This could get very bad, depending on what your variables have in them. Do not do this.

There is no need to blank or unset variables at the end of a shell script in any way.

Answered April 21, 2017
Note that there is a protection against the latter (-r / ) on several current rm implementations (see ) – jlliagre 3 hours ago
 CanDoerz  1 year ago
As an addition: the command to delete variables is unset. But it is not neccessary to unset them as they are not transferred to the calling shell, if you simply execute the script (you'd need to call . if you would like to have the variables of the script in your currecnt shell environment) – Philippos 5 hours ago
 CanDoerz  1 year ago

I wonder if you actually mean shell variables, or temporary files.

If what you're doing is this (a temporary file):

something > "$tmp"
something else < "$tmp"
rm "$tmp"

Then sure, go ahead and remove the temp file after you're done. Though if the script crashes in the middle, the file will be left laying there, but that's not exactly uncommon either. You could use trap 'rm "$tmp"' EXIT to remove the file when the shell exits, if you care.

But I don't see a reason to use -f, let alone -r with rm here.

(Also, remember to quote the variable, even though mktemp by default makes "nice" filenames.)

However, if you're doing this (a variable):

read var
do something with "$var"
rm "$var"

Then you don't want to do that: you have no file to remove, just something entered by the user. Shell variables cease to exist when the shell exits, so no need to unset them.

Though if your script is meant to be sourced from another shell (or it's in .bashrc or similar), the shell doesn't exit when your script ends. In that case it might be useful to unset any temporary variables you used at the end, to not leave them set for the rest of the shell's lifetime.

# in .bashrc
__hour=$(date +%H)
if [ "$__hour" -lt 12 ] ; then echo "Good Morning!" ; 
elif [ "$__hour" -gt 18 ] ; then echo "Good Evening!" ;
unset __hour

The variable name might still collide with some variable used outside that script so some care must be taken with naming.

Answered April 21, 2017

No.rm doesn't deal with variables. If you wrote rm $abc it will delete the file in current directory with the name stored in variable abc.

Example: A variable abc stores value 52. and you performed rm $abc then it will delete file 52 in your current directory (use pwd command to print current directory and ls to list files in current directory).

Variables will be disposed automatically (at least in shell script) don't worry about that.

Answered April 21, 2017
Your Answer