I use Bash 4.3.48(1) and I ran the following command pattern on a testing VPS machine:
rm -rf ${drt}/${pma}*
This command deleted the entire operating system (Ubuntu). That was evident by executing cd /
which returned nothing but the following error:
bash: cd /: No such file or directory
In a deeper look, this happened because both variables in the original command above, were not declared:
I created a file named
~/repoName/assignments_variables.sh
that contains a list of exported variables (includingdrt
andpma
).Instead running
source /etc/bash.bashrc
, I ran/etc/bash.bashrc
, which was wrong, because that file is only relevant to the current session (though it can be used to subsequently run files that by themselves execute data in subsessions).
Now that it's clear to me that the bad path is the general cause for the problem, I'd like to dive a bit deeper and ask this:
Why did the rm -rf
ignored the wrong variable-expansions and kept going to the /*
? I know that rm -rf
should delete a dir only if it finds it, hence shouldn't rely on parts of the path of a non-existent dir (like /*
that caused deletion of the operating system). How could I improve the rm -rf
command to cover possible similar cases of a bad path (due to wrong variable-expansions) in the future?
Is there some Bash directive to make sure that Bash will never expand empty variables (until I undo that directive)?
Let me emphasize: I usually work not only with backups but with double backups. That was really a testing environment with x3 backups in background.
/*
is/bin /boot /dev /etc /home /lib /lib64 /lost+found /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var
, not a non-existent directory.