Aliases

Aliases are command name macros used as shorthand for other commands, especially frequently used ones. This saves a lot of typing time. Aliases are defined with the alias name=value command. For example, we could create an alias for the print command like this:

					$ alias p=print
				

Whenever p is invoked, print is executed:

					$ p Hello
					Hello
				

Make sure to enclose the value in quotes if it contains whitespace. Here we create an alias l that is set to the ls –Fac command:

					$ alias l="ls —Fac"
				

Now when you type in l, ls –Fac is executed:

					$ l
					./
					../
					compress.Z*
					func.Z*
					test/
					uncompress.Z*
					. . .
				

Alias values can contain any text, including special characters, like wildcards, pipes, or I/O redirection operators. Let's change alias l so that the output is piped to the more command:

					$ alias l="ls —Fac | more"
				

But what if you wanted to make this a global alias by setting it in the /etc/profile file, and some people wanted to use more, while others wanted to use pg? We could add the PAGER variable to the l alias, and let each user set PAGER to whatever they wanted.

					$ alias l="ls —Fac | ${PAGER:—/bin/pg}"
				

Notice that if PAGER is not set, the default /bin/pg will be used. One last point. Using double quotes causes alias values to be expanded only when the alias is set. This means that if we reset PAGER after l is defined, it would have no effect on the alias. To have the alias value expanded each time it is invoked, use single quotes like this:

					$ alias l='ls —Fac | ${PAGER:—/bin/pg}'
				

Now whenever PAGER is redefined, the next time alias l is invoked, it uses the new value.

If an alias value ends with a blank, then the next word following the alias is also checked if it is an alias. Here we set two aliases: p and h. When invoked, we get h instead of Hello.

					$ alias p='print' h=Hello
					$ p h
					h
				

After the p alias is reset with a trailing blank, h gets substituted in the next command correctly:

					$ alias p='print ' h=Hello
					$ p h
					Hello
				

Table 7.3. Preset Aliases
Alias Value Definition
autoload typeset –fu define an autoloading function
echo print – display arguments
functions typeset –f display list of functions
hash alias –t – display list of tracked aliases
history fc –l list commands from history file
integer typeset –i declare integer variable
r fc –e – re-execute previous command
stop kill –STOP suspend job
type whence –v display information about commands

Displaying Current Aliases

A list of the current aliases is displayed using the alias command without arguments:

						$ alias
						autoload=typeset —fu
						cd=_cd
						echo=print —
						functions=typeset —f
						h=Hello
						hash=alias —t —
						history=fc —l
						integer=typeset —i
						l=ls —Fac | more
						ls=/usr/bin/ls
						mv=/usr/bin/mv
						nohup=nohup
						p=print
						r=fc —e —
						rm=/usr/bin/rm
						stop=kill —STOP
						suspend=kill —STOP $$
						type=whence —v
						vi=SHELL=/bin/sh vi
					

Exported aliases are displayed using the alias –x command.

Tracked Aliases

Tracked aliases are used to associate an alias with the full pathname of a program. When a tracked alias is invoked, instead of searching each directory in PATH, the full pathname of the corresponding command is returned from the alias table. This speeds execution by eliminating the path search.

Most implementations of the Korn shell come with a few default tracked aliases. These are usually set to frequently used commands. Tracked aliases and their values can be displayed with the alias –t command. Let's see what we've got:

						$ alias —t
						ls=/usr/bin/ls
						mv=/usr/bin/mv
						rm=/usr/bin/rm
						vi=/usr/ucb/vi
					

On this version of the Korn shell, the ls, mv, rm, and vi commands are standard tracked aliases. On other implementations, they may be different.

Tracked aliases are basically the same as normal aliases, except that they are defined using the following format:

						alias —t
						name
					

Notice that a value is not given, as in normal alias –name =value syntax. This is because the Korn shell assigns a value automatically by doing a search on PATH. In the case of the tracked alias ls, the value is set to /usr/bin/ls, since /usr/bin is the first directory in PATH that contains ls.

We could set up a tracked alias for the cp command like this:

						$ alias —t cp
					

If the trackall option is set (set –h, or set –o trackall), then the Korn shell attempts to generate tracked aliases for all commands that it encounters for the first time. By default, this option is usually disabled.

Tracked aliases become undefined if the PATH variable is unset. However, they continue to be tracked aliases. The next reference to the tracked alias causes the value to be reassigned.

Removing Aliases

Aliases are removed with the unalias command. Let's try it with the l alias:

						$ unalias l
					

Now when invoked, it returns an error.

						$ l
						/bin/ksh: l:  not found
					

If you want to prevent an alias from being interpreted as one without having to delete it, just enclose it in single quotes. This is useful for when aliases are named after commands or functions. For example, on systems that alias cd to the _cd function, the real built-in cd command could be invoked like this:

						$ 'cd'
					

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.98.148