Posted: . At: 9:35 AM. This was 6 years ago. Post ID: 11832
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

Get a randomly generated bash shell prompt and some other tricks for the Linux shell.

A nice shell prompt that is randomly generated each time it appears. Working version.

PS1="\[\e[1;31m\]$(shuf -n 1 /usr/share/dict/words)\[\e[m\]@\[\e[1;35m\]$(shuf -n 1 /usr/share/dict/words)\[\e[m\]\$ "

Select a random word from a textfile with bash.

ubuntu ~ $ shuf -n 1 /usr/share/dict/words
plucked

Select a random word from a text file and reverse it when it is printed to the terminal.

ubuntu ~ $ shuf -n 1 /usr/share/dict/words | rev
serutnedni

Select the random word from /usr/share/dict/words and then print it in uppercase and then reverse the output.

ubuntu ~ $ shuf -n 1 /usr/share/dict/words | tr [a-z] [A-Z] | rev
S'TTOILLE

This is another way to get random words from a file, but even on an SSD, it is very slow compared to shuf.

ubuntu ~ $ time sort -R /usr/share/dict/words | head -n 3
binomial's
sunbeam
lollypop's
 
real    0m1.113s
user    0m1.112s
sys     0m0.001s

Better to use shuf instead.

ubuntu ~ $ time shuf -n 4 /usr/share/dict/words
handshaking
fleetest
deluxe
Songhai's
 
real    0m0.004s
user    0m0.000s
sys     0m0.003s

Some commands are better than others for certain tasks. So do not use slow commands if there is a much faster alternative.

Get a random word and then scramble the output.

ubuntu ~ $ shuf -n 1 /usr/share/dict/words | sed 's/./&\n/g' | shuf | tr -d "\n"; echo
oiudbsro

If the user requests 6 random words instead of 1, it will merge them all together. Good way to generate a random string.

ubuntu ~ $ shuf -n 6 /usr/share/dict/words | sed 's/./&\n/g' | shuf | tr -d "\n"; echo
snxssodugpr'eginlroowlmildoloitaAegaomsnsWeaqau

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.