May 08, 2003

Bang History

The !! event designator is substituted with the last command entered. !-2 is substituted with the one before that, and so on. For example:


which script.pl
less `!!`

Posted by Rob at 05:21 PM | Comments (0)

Repeating A Command Sequence - Ctrl-O

If you find yourself running a sequence of commands repeatedly, Ctrl-O is your friend. Press the up arrow to find the first command in the sequence, then repeatedly press Ctrl-O to run each command in the history.

Posted by Rob at 04:22 PM | Comments (1)

May 07, 2003

Extracting a Filename From a Path Name

If you have a variable $FILE which contains a full path and filename, for example, "/home/fred/myfile.txt", then the following code:

${FILE##/*/}

will evaluate to the filename without the path, e.g. "myfile.txt". The ## string operator deletes the longest match of the specified regular expression.

Posted by Rob at 07:04 PM | Comments (0)

For Loops in Bash

For loops are a useful feature in Bash for performing the same operation on a number of files. For example:

for FILE in *.dv; do dv2avi $FILE; rm $FILE; done

might convert all the .dv video files to .avi and delete the original file. Such commands can also be split across multiple lines whether in a shell script or interactively on the command line.

Posted by Rob at 06:08 PM | Comments (0)