Archive
Posts in Shell
Finding the shell level with bash
I occasionally need to start bash more than once, and typically use exit (my .bash_profile is set to ignore ^D (EOF)) to leave the new version of bash. Periodically I hit exit one too many times, which causes my terminal session (and debugging sessions) to dissappear. Since this caused me a good deal of grief yesterday, I decided to find a fix in the bash documentation. After reading for 30-minutes or so, I came across the bash SHLVL variable: This is a useful variable, and I plan to make a "shellami" alias…
$ read more →Generating random numbers with bash
While perusing through some documentation last week, I came across the bash RANDOM variable. This nifty little built-in can be used to generate a random signed integer value: This could definitely be valuable for solving some bash-specific problems, but nothing can replace /dev/random and a hardware random number generator.
$ read more →jot it up
While reading through UNIX powertools tonight, I came across jot. This nifty little utility will print a sequence of numbers, which can be used to iterate through a loop: Using jot in this way is typically faster than other mechanisms (e.g., i= ), and is easier to read IMHO. Jot it up yizos!
$ read more →Safely creating temporary files
I was recently scolded by my friend Clay for using $$ to create temporary files. Clay mentioned that I should be using the mktemp(1) command instead of $$, so I started converting all of my scripts to mktemp (if it's available). The following block of code provides an example of how mktemp can be used in a shell script: Accessing /var/tmp/tmp.file.Lvyde This is kinda swell!
$ read more →Stripping leading zeros with bash
I needed to strip leading zeros from a variable, and found the "#" modifier in the shell manual pages: The number of occurences of the value following the # indicates how many to strip, and can also be applied to ASCII characters: This is some nifty stuff!
$ read more →