basename

basename returns the actual filename name of a file, stripped of any directory paths. It is by far most commonly used with the $0 variable for a script to find out how it was called. This can be useful for debugging and general output messages. The capability to determine the name that a program was called by is also used by some system utilities to modify their actual behavior. The mount and umount commands share a lot of common code, but the effect of running umount /home is very different — the opposite — from that of running mount /home. A single program deals with this by checking the name that it was called as and acting appropriately. This removes redundancy and keeps all of the filesystem mounting and unmounting code in the same program. A straightforward example of this is the dos2unix conversion utility mentioned later. The DOS (and Microsoft Windows) text file format uses CR+LF, whereas Unix (and Linux) use LF alone. This difference goes back a long way and is not likely to be reconciled ever. Two conversion utilities, unix2dos and dos2unix, exist to perform the simple conversions between the two text file formats. These are not always available on every platform, and there are various quirks that affect the apparently simple translation between these two very similar formats, but sed is a great way to perform the conversion. The page http://sed.sourceforge.net/sed1line.txt has a load of useful sed one-liners, including some that are used by this script.

Get Shell Scripting: Expert Recipes for Linux, Bash, and More now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.