It is currently Fri Sep 22, 2017 9:19 am

alias tutorial

All times are UTC - 6 hours

Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Mar 27, 2008 11:37 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
an alias is sort of like a one-line script. it can be used to group together like commands or shorten common ones. you can add some into your ~/.bashrc file or add them into the profile file or even the /etc/skel/.bashrc file (for new users) or you can even just use one during a shell session.
i suggest doing it in a shell session first, just to test it. then commiting it to a static location for constant adding.
i have tons of aliases in my bashrc file just because i want to use them all the time (not to mention i'm lazy). since i use sudo, i get tired of typing "sudo shutdown -h now" everytime i want to shutdown my computer, so i made an alias
alias shutdown='sudo shutdown'

that one only eliminates the need to type the word sudo
alias sd='sudo shutdown -h now'

that makes it so i just have to type "sd" to shutdown like i want

ofcourse you can get tricky with them, say you changed to a particular directory all the time (like /home/jbsnake/scripts :))? i like to use
alias scripts='cd /home/jbsnake/scripts'

Important: DO NOT name an alias the same as a command that doesn't do the same thing(i.e. alias df='do something that df doesn't do')

ofcourse you can pass an alias more than one command, just use a semi-colon ( ; ) as a seperator.
Syntax: alias <nameofalias>='command1;command2;etc.'

suppose instead of just changing to the best directory ever :) i wanted to list the contents once i was there. i would use this alias
alias scripts='cd /home/jbsnake/scripts; ls'

if you don't like adding symbolic links within your path using neat scripts :)
you can reference different files as aliases, like
alias link2path='/home/jbsnake/scripts/link2path'

as you can tell from the first couple posts i made here, you can use an alias for many monotonous things (mounting removable media). if your like me (hopefully you aren't) i never label my floppy disks, or if i do, the name is not acurate anymore :), so this alias is great when i have to search through every one
alias floppy='LAST_DIR=`pwd`; mount /dev/fd0; cd /mnt/floppy; ls'

that alias takes my current directory and stores it into a variable called "LAST_DIR", then it mounts the floppy drive, then changes to the floppy drives directory, then lists the contents. if you havn't noticed yet, aliases are very handy things to have around.

 PostPosted: Sun Apr 25, 2010 2:34 pm   

Joined: Sun Apr 04, 2010 11:44 am
Posts: 9
I'm trying to make an alias that uses the watch command, namely
watch 'df -h'
However because this command already uses a ' i can't make an alias using
alias x='watch 'df -h''
because the ' in the watch command is seen as an end to the alias. How can I fix this?

 PostPosted: Sun Apr 25, 2010 4:55 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 638
simply use different quotes:
alias x='watch "df -h"'
or don't:
alias x='watch -- df -h'
works too.

alias x='watch df -h'

 PostPosted: Mon Apr 26, 2010 11:14 pm   

Joined: Wed Apr 14, 2010 10:23 am
Posts: 17
Additional comments about aliases:

Manpage clearly states that aliases shouldn't be overrated:
bash(1) wrote:
For almost every purpose, aliases are superseded by shell functions.

Gurus just say the same :)
greybot (IRC bot freenode's Bash channel) wrote:
If you have to ask, use a function instead: myfunc() { foo "[email protected]" | bar; }

An additional warning:

Do not try to define a function with the same name as an alias. It will, depending on the alias, result in a parsing error.

 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  

BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group