These classnotes are depreciated. As of 2005, I no longer teach the classes. Notes will remain online for legacy purposes

UNIX01/Aliasing And Exporting

Classnotes | UNIX01 | RecentChanges | Preferences

Shell scripting continued

Recall last time that we learned that a shell script can contain any command which you would normally type at a command line. Today we will learn several commands which are largely used inside of shell scripts (but which you can also use from the command line).

alias

alias displays or defines mappings between a keyword and a command (or another keyword). For example, if you were having a hard time remembering the "ls" command, and you keep finding yourself typing "dir" instead, you could set up an alias for the "ls" command calling it "dir".

The alias command alone displays a list of defined aliases. Typing alias on my Debian machine shows me the following definitions

 sam@rygel:~$ alias
 alias cp='cp -i'
 alias la='ls -la --color=auto'
 alias ll='ls -l --color=auto'
 alias ls='ls --color=auto'
 alias mv='mv -i'
 alias rm='rm -i'
 alias where='type -all'
 alias which='type -path'

To display the value of a particular alias, you use the defined alias as the command line argument. For example,

 sam@rygel:~$ alias ll
 alias ll='ls -l --color=auto'

You can also use the alias command to define aliases. The syntax for this is as follows

 alias [alias-name[=value]]

So, if I wanted to alias "ls -la" to "dir" I would do

 alias dir='ls -la'

unalias

unalias removes aliases that have been defined. With the "-a" option, you can remove all defined aliases. The format for the unalias command is
 unalias [-a] [alias-name....]

export

As we have seen, we can define variables inside of a shell script or shell command environment. These variables that we define are only valid in the context of the script we are running or shell environment we are in, meaning that once we swicth (either by switching shell environments or running another shell script), our variables are lost.

If we want to make a variable accessable to other scripts and environments, we use the export command. For example, if we had the variable

 myname="sam"

We could export it thusly

 export myname

To see a list of all currently exported variables, use the -p option

 export -p


Classnotes | UNIX01 | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited July 26, 2003 12:54 pm (diff)
Search:
(C) Copyright 2003 Samuel Hart
Creative Commons License
This work is licensed under a Creative Commons License.