WARNING: Dangerous script. Do not run from your command line!
Saw this in a company joke email. Can someone explain to me why this bash script is more dangerous than a normal 'rm -rf' command?:
nohup cd /; rm -rf * > /dev/null 2>&1 &
Particularly, why is nohup used and what are the elements at the end for?
WARNING: Dangerous script. Do not run from your command line!
Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).
They are analogous to . bat files ( cmd scripts) under Windows. All of these (shell scripts, cmd scripts, .exe Windows executables, Linux executables (which usually have no extension)) are executable programs; if you run one, it can do anything you can do. So yes, shell scripts can be harmful.
$1 - The first argument sent to the script. $2 - The second argument sent to the script. $3 - The third argument... and so forth. $# - The number of arguments provided. $@ - A list of all arguments provided.
You can try something less "dangerous":
nohup cd /; find * >/dev/null 2>&1 &
I'm getting this:
nohup: ignoring input and appending output to `nohup.out'
nohup: cannot run command `cd': No such file or directory
[2] 16668
So, nohup
part does nothing, it only triggers an error. The second part (of the original script) tries to remove everything in your current directory, and cannot be stopped by Ctrl-C, because it runs in the background. All its output is redirected to void, so you do not see any 'access denied' progress messages.
2>&1
takes stderr (file handle 2) and redirects to stdout (file handle 1). &
by itself places the rm command in the background. nohup allows a job to keep running even after the person who started it logs out.
In other words, this command does its best to wipe out the entire file system, even if the user ragequits their terminal/shell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With