Displaying a table of shell aliases I'm just getting into bash and sh scripting.  I mostly just stub out little convenience or exercise scripts for myself, but I recognize I may be flaunting best practices at times.

My question is specifically related to best practices (or just common sense) when using rm inside a shell script.  My googles have only turned up suggestions to alias rm to rm -i or somesuch.

Anyway, enough talk, here's the script (note: the rm appears at the very end of the script):

#!/bin/sh -e
# Format output of alias print dump into more readable format

if [ -f "TEMP.AWK" ]; then
    echo "Somehow 'TEMP.AWK' already exists"
    exit 1
fi


cat ${HOME}/.zshrc | grep -e '^alias' | awk -F'=' 'BEGIN {
        print "ALIAS                         | COMMAND";
        print "---------------------------------------";
    }
    {

        # replace all multi-spaces with a single space
        gsub(/\s+/, " ", $0);

        col1Len  = 20;
        col2Len  = 60;
        aliasLen = length($1);
        cmdLen   = length($2);

        tab1len  = col1Len - aliasLen;
        tab2len  = col2Len - cmdLen;

        printf " " $1 " "

        for (i = 0; i &lt; tab1len; i++)
            printf "-"

        print " " $2

    } END {
        print "---------------------------------------";
    }' &gt; TEMP.AWK

cat TEMP.AWK | grep --color -e '[#].*'

rm TEMP.AWK


Any other feedback would not go unappreciated.
The best practice for dealing with temporary files is simply not to use them in the first place.  Many things can go wrong with temporary files:


Lack of permissions to write to a directory
Lack of disk space
Read-only filesystem
File or symlink already exists
File or symlink didn't exist when you checked, but got created immediately after, in a race condition
Script crashed, such that it created a temporary file but didn't get a chance to clean it up


Though there are ways to mitigate these risks, I see no reason to use a temporary file here at all.  For that matter, cat and the first grep are also superfluous.

#!/bin/sh

awk -F= '
    BEGIN {
        TABLE_FMT = "%-18s | %-58s"
        row = sprintf(TABLE_FMT, "ALIAS", "COMMAND");
        print row;
        gsub(".", "-", row);
        print row;
    }
    END {
        print row;
    }

    /^alias/ {
        gsub(/\s+/, " ", $0);  # Condense consecutive whitespace
        printf TABLE_FMT "\n", $1, $2;
    }
' &lt; "$HOME/.zshrc" | grep --color '#.*\|$'


I can't tell for sure, but it looks like the second grep is meant to highlight comments.  Unfortunately, it actually ends up stripping out all uncommented lines, including the header and footer.



Instead of parsing the $HOME/.zshrc configuration file, you might want to query the live alias definitions in the current shell instead.  In that case, you should write a shell function that processes the output of the bare alias command.