View Single Post
  #3  
Old July 9th, 2003, 02:26 AM
twistedcranium's Avatar
twistedcranium twistedcranium is offline
CTH Subscriber
 
Join Date: May 2003
Posts: 1,133
add aliases to your bash rc to automate commands

One of the first thing that I do when I get an account on a unix/linux machine at work or setup a linux install at home is to modify my bash shell profile to preset aliases for me.

Firstly, I should mention that the 'bash' shell is the default shell loaded with the recent RedHat versions and is likely the most commonly used shell in the linux/unix world. When you fire up a terminal window, that command line is your shell. To determine what shell you're currently using type 'echo $SHELL' at the prompt. Pick your shell from the following

ECHO RESULTS
/bin/bash.............bash shell (bourne again shell)
/bin/sh...............bourne shell (predecessor to the bash shell)
/bin/csh..............C shell
/bin/tcsh.............enhanced version of C shell

Secondly, I'll mention what an alias is. An alias is somewhat like a variable that can be used to reprlace a string of characters. For example, lets say that you want to do a long listing of the files in a directory, with all the attributes shown as well as hidden files and pause the listing should it scroll off the screen. This command would be 'ls -la |more'. That's hard to type, especially if you do this often enough...so we create an alias to handle this command for us.

At your shell prompt, type

alias llm 'ls -la |more'

from that point on during that shell terminal session all you need to do is type llm and it will execute the full ls command with the -la switches and pipe that to the more command to pause the scrolling.

Now, the alias feature is great, but wouldn't it be even better if you could put a collection of aliases somewhere and have them available in a moment's notice in any shell terminal session. Well, you can do that by editing your bash shell RC script.

In your user directory (/home/username) there should be a file named .bashrc which you can edit in a plain text editor. Here is a sample....

# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

PATH=$PATH:$HOME/bin:$HOME/utils:/sbin:/bin:/$HOME/programming:/$HOME/programming/Projects
export PATH

# aliases
alias trash="rm -rf"
alias ll="ls -la"
alias lm="ls -la |more"
alias pss="ps -U username"
alias shred="shred -u"

PS1="[\t][\u \w]\$ "


**notice the alias lines in the RC file?

These lines set the aliases for you each time you open a terminal shell session as the system processes your .bashrc when starting the session. The trash alias I use delete a file or folder and tell it to ignore the "are you sure?" prompt; the ll and lm are my listing aliases; the pss alias tells me what processes I have running as username; and shred just allows me to use the shred command without having to use the -u switch.

Soooooo, anytime you catch yourself typing the same text more than three times, alias-away!!
Reply With Quote