EXPLAIN: How to Use the Alias Command?
Reader Geoff recenty had the opportunity to play with the alias command. It all started with a desire to get aterm running with a bunch of command line switches. Obviously, typing a long string of switches into the command line gets pretty tedious after a while, so it’s natural to want to shorten that up.
The first thing that came to mind was to whip up a quick bash script and put the ugly stuff in it. Wrap it up by giving the bash script a nice name like ‘term’ and presto…a single word command now fires up aterm with a bunch of command line switches after it.
That was my solution, but then Geoff came across the alias command which seems like a much better way to do stuff like this. In short, you can create named aliases. An alias basically allows me to call something like ls -la –color=auto by assigning it to a simple la command.
You will likely find that you already have some aliases installed on your system out of the box. To find out, open a terminal window and type:
alias
If your system is like mine, then you’ll see something like this:
alias ..=’cd ..’
alias cp=’cp -i’
alias l=’ls -a –color=auto’
alias la=’ls -la –color=auto’
alias ll=’ls -l –color=auto’
alias ls=’ls –color=auto’
alias mv=’mv -i’
alias rm=’rm -i’
alias where=’type -all’
alias which=’type -path’
You can make your own alias. Let’s say I edit my syslog frequently. I might want to make an alias to open up my syslog in my favourite text editor nano.I think I want to assign the command nn to run it. The command to make my alias is:
alias nn=”nano /etc/syslog”
Now if I type nn, nano will fire up with my syslog in it ready to go. Why I would want to edit my syslog is beyond me, but it makes for a good example.
The lifetime of an alias
Just typing that alias command into my terminal session will only allow me to use it while that terminal session is active. Once I close the terminal, that alias is gone forever.
If I want to make that alias available to me every time I start a terminal session, then I need to put it into my .bashrc file. The command is exactly the same, just plunk it into the .bashrc file in your home directory.
If I wanted to make this command available to every user on my system, there’s a few ways to do this. The most likely way to enable this is to put my alias command into my system’s /etc/profile file. Most systems are rigged to import the /etc/profile file into each user’s personal .bashrc file every time a terminal session is started.
Thanks, Geoff!
Related Stories
POSTED IN: Explanation
2 opinions for EXPLAIN: How to Use the Alias Command?
geoff
Nov 5, 2005 at 12:50 am
Hey, no problem! My work didn’t go beyond 5 characters; you did all the typing.
Jon
Nov 5, 2005 at 11:07 am
But you *discovered* it!
Have an opinion? Leave a comment: