Alias is Your Friend

I regularly use ls -la to list directory contents on my Raspberry Pi. I often use ls -lah to also display hidden files. This week I wondered if there was a way to use either of these ls options by default. Well, there is.

Say hello to the Bash shell’s alias command, which lets you assign a string that will be displayed and actioned when you enter another (typically much shorter) string:

alias ls="ls -lah"

Now whenever I enter ls at the prompt, it’s as if I actually typed ls -lah.

You can use alias at the prompt, but Bash only remembers the alias for the current session. Sign out or shut down and the alias is lost. A more permanent solution is to add your alias commands to the end of your .bashrc file, which configures your command line shell at start-up.

There’s a quirk with alias: it doesn’t follow your command line colouring rules. To fix that, add --color to every aliased string:

alias ls="ls -lah --color"

I also tire of entering sudo shutdown -h now to power down my Pi. Again, alias comes to the rescue, so now my own .bashrc includes the following lines:

alias ls="ls -la --color"
alias la="ls -lah --color"
alias sd="sudo shutdown -h now"
alias rb="sudo shutdown -r now"

This gives me four short commands in place of commonly issued long ones. If you add any to your own system, just make sure you don’t use the name of an existing command. For example,

alias python="more"

will stop you accessing the Python shell.