Wenton's Linux Bash Page

by
Wenton L. Davis

The Bourne-Again SHell (bash) is arguably the most commonly-used shell in Linux.  It is the default shell for all distributions of Linux that I am aware of.  There are many other options out there, but bash has far-and-away outlasted them all.

It would probably be possible to create an entire website dedicated just to using bash; so I'll just try to touch on some of the most important ones.

First off, read the man page for bash.  Granted, that goes for almost all of the commands, but really, wow, start there.

Second, Look up the many various you-tube videos by David Eddy called "You Suck At Programming."  The guy comes across as a big dick in his early videos, but he seems to have tamed down a LOT, and the guy is amazing with dealing with bash, particularly bash programming and bash intrinsics.

Stuff I Always Forget

I often fined myself working with groups of files, and frequently processing one set of files, generating a new set, but I don't want to over-write the originals, so I need to write the processed files to new file names.  The first one is easiest, so we'll start here:

$ for i in *.mp3
> do
>   j=`echo $i | sed s/mp3/fixed.mp3/g`
>   sox $i  $j
> done

This works well in most cases.  However, it's also possible to put this into a bash script...

#!/bin/env bash

for file in Part.WAV; do
  #Check if the file exists (in case there are no matching files)
  if [[ -f "$file" ]]; then
    output_file="${file%.WAV}_converted.wav"
    
    sox "$file" -f 44100 -b -e signed-integer "$output_file"
    
    echo "Converted $file to $output_file"
  fi
done

The script does take advantage with some of BASH's substitution features, accomplishing (roughly) the same thing as my sed command, and the script also includes some error-checking work, so it has some features my single command doesn't have.  Both get the job done.

Wenton's email (wenton@ieee.org)

home