Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How dangerous is this bash script?

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!

like image 572
derek8 Avatar asked May 28 '12 14:05

derek8


People also ask

What does $() mean Bash?

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).

Are .sh files safe?

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.

What is $1 bash script?

$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.


2 Answers

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.

like image 171
choroba Avatar answered Sep 20 '22 12:09

choroba


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.

like image 36
Marc B Avatar answered Sep 17 '22 12:09

Marc B