Work the Shell - Looking More Closely at Letter and Word Usage

More examples of using the shell to find the frequency of letters used in the English language.

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

missing the n in the newline substitution

Anonymous's picture

$ cat dracula.txt | tr ' ' '\
' | grep -v '[^[:alpha:]]' | grep -v "^$"

shoule be:

$ cat dracula.txt | tr ' ' '\n
' | grep -v '[^[:alpha:]]' | grep -v "^$"

unless I am missing something ;)

Bash is not C or Python or ...

Mitch Frazier's picture

Ending a line with a backslash is the correct way to escape a newline in bash. In many languages, such as C, the backslash gives the next character special significance (eg \n causes the n to become a newline in the string). In bash, on the other hand, the backslash removes any special significance that the next character has. So a backslash as the last character on the line causes the next character (a real newline) to lose its special significance (which is to end a command line) and become a regular character.

The newline case is a bit strange for sure and causes some funny looking code sometimes, I tend to do this instead:



cat dracula.txt | tr ' ' "$newline" | grep ...

That makes it a bit more explicit as to what's happening and you only have to see the "weirdness" once at the top of your script.

Also note, that in Dave's example the newline is not actually required since it appears inside a quoted string. Bash won't recognize a newline as the end of a command if there is an open quoted string, so it just puts the newline into the string. So, the following will work also:

$ cat dracula.txt | tr ' ' '
' | grep -v '[^[:alpha:]]' | grep -v "^$"

Mitch Frazier is an Associate Editor for Linux Journal.