17 November 2009

Make commands behave nicely by default, using shell aliases

You have surely been in situations where a command needed an unwieldy parameter list to do things in a specific way, and editing a config or parameter file, or creating a shell script for the task, was not an option. I already wrote an article on how to search your command history, but in many cases you will not be executing the command interactively; it will be called from a script or such. Well, you are not out of luck. Let me show you one nice trait of bash aliases.

Your .bashrc might contain something like this:

alias ls='ls --color=auto'

This line defines an alias for the ls command, named ls. Weird? Not at all. Aliases override commands and builtins of the same name. The above example results in ls always displaying colorful directory listings, because Bash checks whether a word is an alias before looking for a builtin or command.

Another example: If you frequently ssh into boxes on your local network and start GUI programs on them, the following enables X forwarding by default.

alias ssh='ssh -X'

Aliases also work for programs that are started from the menu in a desktop environment. If you are over 25 and happen to like classical console games, meaning you probably have ZSNES installed, you can fix issues with missing audio using

alias zsnes='zsnes -ad sdl'

Add that to .bashrc, then log out and in and clicky-clicky on the icon.

1 comment:

  1. Thanks for that little history lesson. It's amazing how troublesome even the most simple things were back in those days, like making your text terminal display colors or missing audio in applications. I sure am glad that's been fixed since Windows 95.