Tuesday, April 3, 2012

LINUX/UNIX C Shell Alias Command Syntax Example Code

LINUX/UNIX Aliases:
The C shell allows you to create and customize your own commands by using the built-in command alias. Commonly used for a long strings that are frequently used. Alias allows you to have a small more familiar command or name to execute a long string. An alias will last for the life of the shell session.
Regularly used aliases can be set from the shell's configuration file (~/.cshrc or the systemwide /etc/csh.cshrc for csh, or ~/.bashrc or the systemwide /etc/bashrc or /etc/bash.bashrc for bash) so that they will be available upon the start of the corresponding shell session. The alias commands may either be written in the config file directly or sourced from a separate file, typically named .alias (or .alias-bash, .alias-csh, etc., if multiple shells may be used).

Syntax and description of the alias shell command:

Shell Command: alias [word [string] ]
alias supports a simple form of command-line customization. If you alias word to be equal to string and then later enter a command beginning with word, the first occurrence of word is replaced by string and then the command is reprocessed.
If you don't supply word or string, a list of all the current shell aliases is displayed. If you only supply word, then the string currently associated with the alias word is displayed. If you supply word and string, the shell adds the specified alias to its collection of aliases. If an alias already exists for word, it is replaced.
If the replacement string begins with word, it is not reprocessed for aliases to prevent infinite loops. If the replacement string contains word elsewhere, an error message is displayed when the alias is executed.

To give you some exposure of how alias command works on running linux system, below is an example of alias in action:
$ alias dir 'ls -aF' ...register an alias.
$ dir ...same as typing "ls -aF".
./ main2.c p.reverse.c reverse.h
../ main2.o palindrome.c reverse.old
$ dir *.c ...same as typing "ls -aF *.c".
main2.c p.reverse.c palindrome.c
$ alias dir ...look at the value associated with "dir".
ls -aF
$ _

In the following example, I aliased a word in terms of itself:
% alias ls 'ls -aF' ...define "ls" in terms of itself.
% ls *.c ...same as typing "ls -aF *.c".
main2.c p.reverse.c palindrome.c
% alias dir 'ls' ...define "dir" in terms of "ls".
% dir ...same as typing "ls -aF".
./ main2.c p.reverse.c reverse.h
../ main2.o palindrome.c reverse.old
% alias who 'date; who' ...infinite loop problem.
% who
Alias loop.
% alias who 'date; /usr/bin/who' ...full path avoids error
% who ...works fine now.
Fri May 13 23:33:37 CST 2005
smith ttyp0 Feb 13 23:30 (xyplex2)
% _

Removing an Alias:
To remove an alias, use the built-in command unalias.

Syntax and description of the unalias shell command:
Shell Command: unalias pattern unalias removes all of the aliases that match pattern. If pattern is *, then all aliases are removed.

0 comments:

Post a Comment