Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Blogs > loadedmind
User Name


Rate this Entry

Using aliases

Posted 05-31-2017 at 07:52 AM by loadedmind

This topic assumes you know better than to test in production or on a system you depend on for your livelihood. When in doubt, spin up a VM and test. Then, when you're 1000% sure there will be no impact on your system, only then can you use what I've outlined here. I take no responsibility for your goof-ups.

In this post, I'll cover the use of aliases, why you should (or shouldn't) use them, how it can save you time and potential pitfalls.

First, what is an alias? Well, since I'm all about applying subject matter to the real world, let's take such a scenario. Suppose you use git for version control of your documentation for your machines. You ARE documenting, right? 8^) So, as you may or may not already know, the usual progression of git is, after init'ing the directory, git add, then git commit -m "comment here", git status and git clone. Since you are most likely typing this over and over again, perhaps an abbreviated version of these commands is in order? This is only one such usage for aliases, but let's look at how to do this for common commands first. Here's an example ~/.bashrc:

alias gcm='git commit -m '
alias ga='git add'
alias gs='git status'
alias gcl='git clone '
alias gl='git log'
To use this immediately, we'll need to source our ~/.bashrc. You can do this one of two ways:
. ~/.bashrc
source ~/.bashrc
(they both get the job done)
It's worth mentioning that, should you attempt to include these lines in your own ~/.bashrc, you should first ensure that you a)know how to use git and b) don't already have gcm, ga, gs, gcl and gl mapped as another command on your machine already. To check this, you can use the which command.

Example of which:
[testuser@machinezero]$ which gl
/usr/bin/which: no gl in (/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/users/testuser/bin)
Your mileage may vary.

Ok, that's great, but maybe you don't care about git (hint, you should) or maybe you want to use aliases for something else. Well, aliases can also be used in place of commands, but I should caution you here - doing so can have unintended consequences if you don't know what you're doing.

In this example, I'm going to substitute the normal, boring, outdated vi command with vi enhanced (vim). A large majority (perhaps all) of machines running Linux will come with an editor. Don't worry, this is not a debate on why one editor is better than the other (even though everyone knows vim is better8). We're going to first check whether vim is installed on the machine and, if so, tell vi to point to vim as an alias.

[testuser@machinezero]$ which vim
Now, within our ~/.bashrc:

alias vi='/usr/bin/vim'
You'll need to source bashrc each time you make a change before you'll see the intended results.

Depending on your Linux distribution, you may have noticed a few alias lines already in your bashrc. Perhaps something like the following:

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
This is basically the same thing as what I've done with the vi command - altering the default behavior of that command. These are one of those ID10T things that you might want to include in your default ~.bashrc to prevent very bad things from happening, such as wiping out entire directories or extremely important files. Ask me how I know! 8^) If you keep backups like you should, this shouldn't be an issue. You ARE backing up, right?!

As you might have gathered by now, aliases can be quite useful, but do be cautious when using them. Exercise due diligence and test things before implementing. Ensure your actions result in expected behavior and you should be fine. One way to help with this is to watch what happens when you source your bashrc. If you get strange results, it's probably something in your bashrc. Make a change, test. Make a change, test. Rinse repeat.
Posted in Uncategorized
Views 277 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 07:14 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration