Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Making Your Own Linux Bash Commands


  • Please log in to reply
No replies to this topic

#1 Andrew

Andrew

    Bleepin' Night Watchman


  • Moderator
  • 8,260 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:Right behind you
  • Local time:11:42 PM

Posted 03 May 2008 - 02:49 AM

Here's a little tip for all you Linuxers out there.

BASH, the default shell program for most Linuxes is a powerful and versatile tool. Often we find outselves pounding out the same long commands over and over again. I for one use SSH a lot to log into various remote computers. The solution is to define command aliases for these commands.

An alias is just another name for a specific command or series of commands. So, rather than entering "ssh -C -l superman -p 5519 192.168.0.1" I can make an alias with that command and name it "superman." Now whenever I want to execute the command, I just type "superman" and BASH executes the aliased command. This saves me 30 keystrokes!

Just about any command can be aliased. You can even alias shell scripts, for example, by making the aliased command execute the script just like you would do manually. You can issue more than one command by using BASH's built in operators like && and ||. For example, if you compile a lot of programs from source you could make an alias called "makeit" and have it execute "./configure && make && make install".

You can create temporary aliases that last only one session using the alias command like this:

alias myalias='mycommand'

for example:

alias fun='apt-get install moo'

These aliases only last until you exit the Terminal. To make permanent aliases, you'll need to edit your .bashrc file and create (or edit) your .bash_aliases file.

Both files can be found in your Home folder. Open .bashrc in your favorite text editor (make a backup copy just in case) and find the following section:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

#if [ -f ~/.bash_aliases ]; then
# . ~/.bash_aliases
#fi


Remove the #'s from the last three lines so that it looks like this:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi


Then save and close it. Now open (or create) .bash_aliases in your home folder. Here you can create all your permanent aliases. The syntax of the .bash_aliases file is the same as the command, with one alias per line. Here's an excerpt from my .bash_aliases file:

alias repos='sudo gedit /etc/apt/sources.list'
alias updateapt='sudo apt-get update'
alias upgradeapt='sudo apt-get upgrade'
alias makeit='./configure && make && sudo make install'
alias bored='echo Andrew is bored && echo Very, very bored...'



BC AdBot (Login to Remove)

 





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users