
First of all, note that this question cannot be asked in Windows. The standard Windows command line shell has neither aliases nor functions, so it’s a script or nothing. They are implemented in the UNIX/Linux/OS/X shells, though. Aliases, variables, functions, and scripts are different ways of executing code that you don’t want to type out time time, but each has a slightly different purpose:
>alias foo="blah blah blah"
then
>foo John
will be executed as if you had typed blah blah blah John.
Note that for this to happen, the alias must be the first
thing on the command line. If you type
>John foo Dierdorf
then what you see is what you get — no substitution will take
place. Among other things, this means that any arguments on the
command line will always be at the end after the alias is expanded,
which severely limits what can be accomplished. For example, suppose
you want to write a shortcut that will copy a file to a backup
directory. It is not possible to create an alias to do this;
you need to have mybackup myfile expand to cp
myfile backupdir, but your argument (myfile)
cannot go in the middle of an alias expansion.
acroread from the command line, you want it to start in
the background. That is, you want the line ac myfile.pdf
to expand into acroread myfile.pdf &. This cannot be
done, because there’s no way (using an alias) to slip the
argument in before the ampersand.
>alias agud="sudo apt-get update; sudo apt-get dist-upgrade"
will work just fine. This can be fairly complicated; for example I
have an alias for synchronizing my Calibre library between my desktop
and laptop:
>alias unical='if [[ -d /dell/CalibreLibrary ]] then unison-gtk -times
/dell/CalibreLibrary ~/CalibreLibrary; else echo "Dell not mounted";fi'
>alias ee=’echo ’
>alias jj=’John ’
>alias dd=’Dierdorf ’
>alias is=’are ’
>ee jj dd is the speaker.
John Dierdorf are the speaker.
>ee jj dd "is the speaker."
John Dierdorf is the speaker.
>
Note that the non-alias argument(s) still have to go at the end. (In
the last example, is is not expanded because the next
argument is the complete string is the speaker. and
that’s not an alias.)
.bashrc or
.zshrc so they are always available. Note that if an
alias’s expansion contains blanks or other special characters,
it must be enclosed in quotes when defined.
Zsh can also associate file types with a
program to handle that file, so for example if I have associated
.pdf with /user/bin/acroread, then I can run
the appropriate program by simply typing the file name at the command
line:
>myebook.pdf
To pull this off, use a special alias syntax as follows, putting the
lines with the rest of your aliases:
alias -s jpg=gv
alias -s gif=gv
alias -s png=gv
alias -s pdf=acroread
alias -s html=kq
alias -s htm=kq
alias -s txt=vi
alias -s odt=oowriter
alias -s doc=oowriter
This is the same sort of thing which happens when you click the icon
of a file in a graphical file manager.
me=’John R. Dierdorf’
mymail=’dierdorf@prismnet.com’
then the line
>tell $me everything at $mymail
expands into tell John R. Dierdorf everything at
dierdorf@prismnet.com. Again, these definitions should be in
.bashrc or wherever.
Zsh has a cute feature that if you define a variable to
hold a path name, then you don't have to use the $. This
will work:
>mydir=’/home/dierdorf/words/extra’
>cd mydir
In fact, in zsh you don’t even need to type the
cd:
>mydir
will work just as well. It doesn’t have to be the full path, as
long as the part to be substituted is at the beginning:
>mydir/js
will expand to cd /home/dierdorf/words/extra/js. This
trick will not work as a part of a file name, though. If you
want to edit a file in a directory named by a variable, you do have to
use the dollar sign:
>vi $mydir/myfile.html
bash or
zsh’s language. The syntax is:
myfunction() {
code goes here
}
$1, $2,
... are the arguments to the function, while $@
means all arguments.
### start acroread in background; ignore error messages
ac() {
acroread $1 2>/dev/null &
}
### back up arguments to backup directory
mybackup() {
cp $@ /home/dierdorf/backupdir
}
### copy files to my web server at prismnet.com/publicweb
pf() {
if [[ -e $1 ]]
then
scp $@ dierdorf@prismnet.com:public-web/
else
echo "usage is pf localfilename [...]"
fi
}
These would be executed as:
>ac mydoc.pdf
>mybackup file1 [file2 …]
>pf file1 [file2 …]
Note that gv, kq, etc. mentioned in the
alias -s definitions above, are functions in the same
form as ac, invoking gwenview and
konqueror, respectively.
.bashrc or .zshrc.
#! “shebang” notation, scripts
can be written in perl, python,
ruby, or whatever, whereas functions are limited to the
shell’s native language. (Likewise, a script can be written
using zsh or csh syntax even though your
running shell is bash.) The external languages provide
more capability than the shell (otherwise there would be need for them
at all!) and (particularly for larger programs) are easier to write,
read, and debug. Examples of stuff you can easily use in these
languages include regular expressions, floating point and complex
arithmetic, mathematical functions (sin, log, …), data base
manipulation, complicated I/O, multi-dimensional arrays,
object-oriented programming, etc.
edit-->save-->execute-->edit… sequence. If you
edit a function or alias, you have to reload the shell before it
becomes “visible” for testing. This can be alleviated,
however, by putting your aliases and functions in a separate file (say
.mycode) and then adding only one line to the startup
file to include it.
source ./.mycode
If you change .mycode, you can simply execute that line
or its even shorter version,
>. ./.mycode
from the command line to make the shell re-load the definitions.
& notation, but a function must run to
completion before the shell regains control.