Bash Toys
Contents
1. Introduction
For the sake of triviality and pointless uniquity, this page provides a collection of snippets that are syntactically correct but look unique, odd and often have no apparent useful purpose whatsoever.
This page exists purely for academic purposes. Do not use code like this in shell scripts.
Feel free to add your own.
2. Innocent
The snippets in this part, unlike the one under "Risky", are innocent, entertaining or just do nothing at all.
2.1. Pointless exercise in operators
The following burp of bash operators defines a function named _ which truncates a temporary pipe which contains the output of the no-op : command that yields no output; and then runs it. The result: Nothing whatsoever.
_(){ > >(:);};_
2.2. Pointless exercise in escaping
The following code changes the input string "< - >" into "<- ->".
\t\r \\\-\ \ \\\- <<< \<\ \-\ \>
2.3. Functions and Compound Commands
A function to do nothing at all, instantly killed off along with its parent sub-shell.
(:() (:))
2.4. Is it a keyword? a function? a variable?
#not useful at all...in bash function for { echo $1 ;};for for in for;do \for $for;done
2.5. Obfuscated awk
# squeeze the newlines into one (like tr -s '\n'), assumes a bourne shell printf "%s\n" foo '' '' bar | awk $$${$+ RS=} #another version, requires bash, ksh or zsh awk {newlines,RS}={12,}
2.6. Obfuscated sed
#remove all occurences of foo, with gnu sed echo "foo bar foo" | sed ':;;\;foo;s;;;;t;'
2.7. cut with join
#cut the first field a string separated by : (like cut -d: -f1) echo foo:moo | join -t : -v1 -o 1.1 - /dev/null
2.8. Abusing paste
#prefix every line printf %s\\n foo bar | paste -d prefix_ /dev/null /dev/null /dev/null /dev/null /dev/null /dev/null /dev/null -
2.9. Silently open an "empty" FD when a file does not exist
Say you want to run a command that reads from a file, but you don't want your bash statement to error out when the file doesn't exist. Instead, you just want to run the command and should it try to read from standard input, send it an EOF immediately. Here's a way to do that really concisely:
command < file # Errors when file does not exist. { command < <(<file); } 2>&- # Opens an FD either to the file or one that does an EOF on read.
2.10. Arithmetic variable recursion
The Fibonacci series up to LLONG_MAX (on this amd64 Linux box) generated as a side-effect of Bash's recursive arithmetic variable lookup. The series is evaluated when a[0] is dereferenced as the last part of the substring expansion to echo, and Bash tries to figure out the integer value of a[0], each time only to find another a[0] until n++/94 == 1.
a=('a[a[n]=a[n-1]+a[n-2],n++/94]' 2 0 1); echo "${a[@]:n=4,a}"
Bash searches to a depth of 1024. It's even portable to ksh93, which is limited to only 8 levels.
3. Risky
Add snippets here that may be intrusive, dangerous or damaging when executed.
Do not run any of these snippets in your shell without fully understanding them, solely at your own risk!
And don't act like a 12 year old by passing these snippets to ignorant or unwitting people either.
3.1. Recursive exercise in operators
This creates a function named _ which truncates the temporary pipe that bash creates to contain the output of a new execution of the same function. The result is a recursively executing bash function. Since the recursion happens through an asynchronous process (which is how >(..) works), this one is slightly annoying to stop. Not quite as paralyzing as the Fork Bomb, though.
_(){ > >(_);};_
3.2. Fork Bomb
What this does is create a function named : which calls itself recursively. Twice. In the background. Since the function keeps calling itself over and over (forking new processes), forever, this quickly consumes a lot of system resources. This can be very destructive for a system where no resource limits are in effect.
:(){ :|:&};:
This is a nicely camouflaged subshell bomb designed to be hard to parse by avoiding whitespace, obvious list delimiters (;), and characters other than parentheses, pipes, and brackets. It's slightly less deadly than the forkbomb but grows just as fast. Unfortunately requires some special options and works only in bash 4.2 or later due to the required lastpipe and unchecked illegal function names that are caught by Ksh.
shopt -s lastpipe; set -o pipefail ((())|[()(]|[)||(())|]()([|])||]) # ((())|]()(]|])||]) # alternate