Aliases
Use aliasing mechanism to assign a sequence of commands to a name.
Properties
Something like a local variable only executable.
Aliases run in current shell.
Should be quoted when assigned to name. Like variable.
Not exportable.
But can be defined in .bashrc
Alias definition must fit on one line (can be long).
Or use \ at end of line.
Can use &&, ||, ;, & to put multiple commands in alias.
Can use some scripting code if fit on a single line.
alias t='for fn in *;do echo $fn; wc $fn;done'
#Note single quotes to prevent $fn from being evaluated too soon.
Technically, bash alias cannot take command line arguments.
But arguments following an alias stay in place on the command line.
Thus becoming part of the last command in alias.
An alias can call another alias.
But not itself.
Redefinable by simply assigning a new sequence of commands.
* C-shell version of alias can use history to take arguments,
but also did not have a function mechanism.
Bash does have functions. Use these for more complex definitions.
Creation, listing, removal.
Creating
alias alias_name="command sequence"
Listing existing aliases.
alias
alias target_alias
Deleting
unalias target_alias
Exporting
Because an alias is NOT exportable,
1. Place your alias definition in .bashrc in your $HOME directory.
2. Invoke .bashrc from .bash_profile in your $HOME directory.
Getting inteactive input from user
Although the bash alias cannot use the history mechanism to get
arguments from the command line.
It can use the shell built-in read function to read input directly
from the user.
The following alias prompts the user for two filenames and then swaps their
contents.
alias swapper='read -p "1st file : " fn1; e read -p "2nd file : " fn2; mv $fn1 ${fn1}.tmp; mv $fn2 $fn1; mv ${fn1}.tmp $fn2'
Note the following :
This is a rather long alias, so it is best to edit it in a file and
then source the file. Be careful, there are no line breaks in this.
Because there are variable references in the alias, the whole alias is
single quoted to prevent evaluation of variables during alias assignment.