Friday, February 17, 2017

How to create command alias on Ubuntu

There is another cool trick that you can do on the command line of ubuntu, you can make an alias for calling other command, it's little bit hard to explain but i think it's easier to understand when you try out your self.

For example, by default if you run 'pwd' on the command line it will display your current working directory, but you can map 'pwd' to run other command instead, like for example you map 'pwd' command to 'ls' command, so when you run 'pwd' it will display list of directory and files, get the idea?

This technique of mapping command line is called command alias, to create command alias, we are going to need the bashrc file located at your home folder (~/.bashrc). So go ahead open up the bashrc file on your computer.

How to create command alias on ubuntu
  • open ~/.bashrc file with your favorite text editor
  • gedit ~/.bashrc
  • go to the end of the file and add this code
  • alias pwd='echo ubuntu is cool'
  • save and exit
  • run this to make sure the alias is working
  • . ~/.bashrc
  • try run 'pwd' command and see what happen
  • pwd
  • if you want the 'pwd' command to work normally, simply remove the alias on bashrc file and run the . ~/.bashrc again.

This alias technique is very useful for making shorter command line, like for example you have a command line with very long parameter input, you can make it shorter by using alias, very cool isn't it?

The 'alias' itself is actually a command line program, by running 'alias' on the command line, you will get list of aliases on your ubuntu, all of these aliases comes from the bashrc file.

No comments:

Post a Comment