• Aucun résultat trouvé

The .bash_profile, .bash_logout, and

Dans le document Learning the bash Shell, 3rd Edition (Page 187-200)

.bashrc Files

Three files in your home directory have a special meaning to bash, providing a way for you to set up your account environment automatically when you log in and when you invoke anotherbash shell, and allowing you to perform commands when you log out. These files may already exist in your home directory, depending on how your system administrator has set up your account. If they don't exist, your account is using only the default system file/etc/profile. You can easily create your ownbashfiles using your favorite text editor. If you are unfamiliar with text editors available under UNIX, we suggest that you familiarize yourself with one of the better-known ones such as vi or emacs before proceeding further with the

The most important bash file, .bash_profile, is read and the commands in it executed bybash every time you log in to the system. If you examine your .bash_profile you will probably see lines similar to:

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin

These lines define the basic environment for your login account. For the moment, it is probably best to leave these lines alone until you understand what they do.

When editing your .bash_profile, just add your new lines after the existing ones.

Note that whatever you add to your .bash_profile won't take effect until the file is re-read by logging out and then logging in again. Alternatively, you can also use the sourcecommand.[1]For example:

source .bash_profile

source executes the commands in the specified file, in this case.bash_profile, including any commands that you have added.

bashallows two synonyms for.bash_profile:.bash_login, derived from the C shell's file named .login, and .profile, derived from the Bourne shell and Korn shell files named

.profile. Only one of these three is read when you log in.

If.bash_profile doesn't exist in your home directory, then bashwill look for .bash_login. If that doesn't exist it will look for.profile.

One advantage of bash's ability to look for either synonym is that you can retain your .profile if you have been using the Bourne shell. If you need to add bash-specific commands, you can put them in .bash_profile followed by the command source .profile.

When you log in, all thebash-specific commands will be executed, and bash will source .profile, executing the remaining commands. If you decide to switch to using the Bourne shell you don't have to modify your existing files.

A similar approach was intended for .bash_login and the C shell .login, but due to differences in the basic syntax of the shells, this is not a good idea.

.bash_profileis read and executed only by the login shell.

If you start up a new shell (asubshell) by typing bashon the command line, it will attempt to read commands from the file .bashrc. This scheme allows you the flexibility to separate startup commands needed at login time from those you might need when you run a subshell. If you need to have the same commands run regardless of whether it is a login shell or a subshell, you can just use thesourcecommand from within.bash_profile to execute .bashrc. If .bashrc doesn't exist then no commands are executed when you start up a subshell.

The file .bash_logout is read and executed every time a login shell exits. It is provided to round out the capabilities for customizing your environment. If you wanted to execute some commands that remove temporary files from your account or record how much time you have spent logged in to the system then you would place the commands in .bash_logout. This file doesn't have to exist in your account—if it isn't there when you log out, then no extra commands are executed.

[1]You can also use the synonymous command dot (.).

Aliases

If you have used UNIX for any length of time you will have noticed that there are many commands available and that some of them have cryptic names. Sometimes the commands you use the most have a string of options and arguments that need to be specified. Wouldn't it be nice if there was a feature that let you rename the commands or allowed you to type in something simple instead of half a dozen options? Fortunately,bashprovides such a feature:

the alias.[2]

Aliases can be defined on the command line, in your .bash_profile, or in your.bashrc, using this form:

alias name=command

This syntax specifies that name is an alias for command.

Whenever you type name as a command, bash will substitutecommandin its place when it executes the line.

Notice that there are no spaces on either side of the equal sign (=); this is the required syntax.

There are a few basic ways to use an alias. The first, and simplest, is as a more mnemonic name for an existing command. Many commonly used UNIX commands have names that are poor mnemonics and are therefore excellent candidates for aliasing, the classic example being:

alias search=grep

grep, the UNIX file-searching utility, was named as an acronym for something like "Generalized Regular Expression Parser."[3] This acronym may mean something to a computer scientist, but not to the office administrator who has to find Fred in a list of phone numbers. If you have to findFredand you have the word searchdefined as an alias forgrep, you can type:

$ search Fred phonelist

Some people who aren't particularly good typists like to use aliases for typographical errors they make often. For example:

alias emcas=emacs alias mali=mail alias gerp=grep

This can be handy, but we feel you're probably better off suffering with the error message and getting the correct spelling under your fingers. Another common way to use an alias is as a shorthand for a longer command string.

For example, you may have a directory to which you need to go often. It's buried deep in your directory hierarchy, so you want to set up an alias that will allow you tocdthere without typing (or even remembering) the entire pathname:

alias cdvoy='cd sipp/demo/animation/voyager'

Notice the quotes around the full cd command; these are necessary if the string being aliased consists of more than one word.[4]

As another example, a useful option to thelscommand is -F: it puts a slash (/) after directory files and an asterisk (*) after executable files. Since typing a dash followed by a capital letter is inconvenient, many people define an alias like this:

alias lf='ls -F'

A few things about aliases are important to remember.

First, bash makes a textual substitution of the alias for that which it is aliasing; it may help to imagine bash passing your command through a text editor or word processor and issuing a "change" or "substitute"

command before interpreting and executing it. Any special characters (such as wildcards like * and ?) that result when the alias is expanded are interpreted properly by the shell.[5] For example, to make it easier to print all of the files in your directory, you could define the alias:

alias printall='pr * | lpr'

Second, keep in mind that aliases are recursive, which means that it is possible to alias an alias. A legitimate objection to the previous example is that the alias, while mnemonic, is too long and doesn't save enough typing. If we want to keep this alias but add a shorter abbreviation, we could define:

With recursive aliasing available it would seem possible to create an infinite loop:

alias ls='ls -l'

bash ensures that this loop cannot happen, because only the first word of the replacement text is checked for further aliasing; if that word is identical to the alias being expanded, it is not expanded a second time. The above command will work as expected (typing ls produces a long list with permissions, sizes, owners, etc.), while in more meaningless situations such as:

alias listfile=ls alias ls=listfile

the aliaslistfileis ignored.

Aliases can be used only for the beginning of a command string—albeit with certain exceptions. In the cd example above, you might want to define an alias for the directory name alone, not for the entire command. But if you define:

alias anim=sipp/demo/animation/voyager

and then type cd anim, bash will probably print a message likeanim: No such file or directory.

An obscure feature of bash's alias facility—one not present in the analogous C shell feature—provides a way around this problem. If the value of an alias (the right side of the equal sign) ends in a blank, then bash tries to do alias substitution on the next word on the command line.

To make the value of an alias end in a blank, you need to surround it with quotes.

Here is how you would use this capability to allow aliases for directory names, at least for use with the cd command. Just define:

alias cd='cd '

This causes bash to search for an alias for the directory name argument to cd, which in the previous example would enable it to expand the aliasanimcorrectly.

Another way to define a directory variable for use with the cd command is to use the environment variable cdable_vars, discussed later in this chapter.

Finally, there are a few useful adjuncts to the basic alias command. If you type alias name without an equal sign (=) and value, the shell will print the alias's value oralias name not found if it is undefined. If you type alias without any arguments, you get a list of all the aliases you have defined. The command unalias name removes any alias definition for its argument.

Aliases are very handy for creating a comfortable environment, but they have essentially been superseded by shell scripts and functions, which we will look at in the next chapter. These give you everything aliases do plus much more, so if you become proficient at them, you may find that you don't need aliases anymore. However,

forbidding place, full of terseness and devoid of good mnemonics. Chapter 4 shows the order of precedence when, for example, an alias and a function have the same name.

[2] C shell users should note that the bash alias feature does not support arguments in alias expansions, as C shell aliases do. This functionality is provided by functions, which we'll look at inChapter 4.

[3] Another theory has it that grep stands for the command "g/re/p", in the old ed text editor, which does essentially the same thing asgrep.

[4]This contrasts with C shell aliases, in which the quotes aren't required.

[5] An important corollary: wildcards and other special characters cannot be used in the names of aliases, i.e., on the left side of the equal sign.

Options

While aliases let you create convenient names for commands, they don't really let you change the shell's behavior. Options are one way of doing this. A shell option is a setting that is either "on" or "off." While several options relate to arcane shell features that are of interest only to programmers, those that we will cover here are of interest to all users.

The basic commands that relate to options are set -o optionname and set +o optionname. You can change more than one option with the one set command by preceding each optionname with a -o or +o. The use of plus (+) and minus (-) signs is counterintuitive: the-turns the named option on, while the+ turns it off. The reason for this incongruity is that the dash (-) is the conventional UNIX way of specifying options to a command, while the use of+is an afterthought.

Most options also have one-letter abbreviations that can be used in lieu of theset -ocommand; for example,set -o noglobcan be abbreviated set -f. These abbreviations are carryovers from the Bourne shell. Like several other

"extra" bash features, they exist to ensure upward compatibility; otherwise, their use is not encouraged.

Table 3-1lists the options that are useful to general UNIX

Table 3-1. Basic shell options Option Description

emacs Entersemacsediting mode (on by default)

ignoreeof

Doesn't allow use of a single CTRL-D to log off; use the exit command to log off immediately (this has the same effect as setting the shell variable IGNOREEOF=10)

noclobber Doesn't allow output redirection (>) to overwrite an existing file

noglob

Doesn't expand filename wildcards like * and ? (wildcard expansion is sometimes calledglobbing)

nounset Indicates an error when trying to use a variable that is undefined

vi Entersviediting mode

There are several other options (21 in all; Appendix B lists them). To check the status of an option, just typeset -o. bash will print a list of all options along with their settings.

shopt

bash 2.0 introduced a new built-in for configuring shell behaviour, shopt. This built-in is meant as a replacement for option configuration originally done through environment variables and thesetcommand.[6]

Theshopt -o functionality is a duplication of parts of the set command and is provided for completeness on the part of shopt, while retaining backward compatibility by its continued inclusion inset.

The format for this command is shopt options option-names.Table 3-2listsshopt's options.

Table 3-2. Options to shopt Option Meaning

-p Displays a list of the settable options and their current values

Option Meaning

-s Sets each option name

-u Unsets each option name

-q Suppresses normal output; the return status indicates if a variable is set or unset

-o

Allows the values of the option names to be those defined for the -o option of the set command

The default action is to unset (turn off) the named options. If no options and arguments are given, or the -p option is used,shoptdisplays a list of the settable options and the values that they currently have. If -s or-u is also given, the list is confined to only those options that are set or unset, respectively.

A list of the most useful option names is given in Table 3-3. A complete list is given inAppendix B.

Table 3-3. shopt option names

Dans le document Learning the bash Shell, 3rd Edition (Page 187-200)

Documents relatifs