Basic Stuff
From CLFS-HINTS
Contents |
Setting the Bash Environment
Configuration Scripts
When you log in to your Bash (or other) shell, several scripts will be executed if they exist. Table I provides a summary of these scripts in the order they will be searched or executed.
| Script | Function |
|---|---|
| /etc/profile | System-wide script executed when any user logs in. Following the CBLFS instructions, this script calls all of the script in /etc/profile.d and executes their commands. You could also put all of the commands in these individual scripts into /etc/profile, but the CBLFS method is (of course) the best and easier to administer than a single file. When invoked interactively with the --login option or when invoked as sh, Bash reads the /etc/profile instructions. |
| /etc/bashrc | System-wide Bash specific configuration script. On systems offering multiple types of shells, it might be better to put Bash-specific configurations in /etc/bashrc, since /etc/profile is also read by other shells. Errors generated by shells that don't understand the Bash syntax are prevented by splitting the configuration files for the different types of shells. |
| ~/.bash_profile | This is the preferred configuration file for configuring user environments individually. In this file, users can add extra configuration options or change default settings. |
| ~/.profile | User-specific version of the /etc/profile script executed when the user logs in. In the absence of ~/.bash_profile and ~/.bash_login, this script is read. It can hold the same configurations, which are then also accessible by other shells. |
| ~/.bash_login | This file contains user-specific settings that are normally only executed when the user logs in to the system. |
| ~/.bashrc | It is more common to use a non-login shell, for instance when logged in graphically using X terminal windows. Upon opening such a window, the user does not have to provide a user name or password; no authentication is done. Bash searches for ~/.bashrc when this happens, so it is referred to in the files read upon login as well, which means you don't have to enter the same settings in multiple files. |
| ~/.bash_logout | User-specific executed when the user logs out. |
Environment Variables
One of the tasks performed by the configuration files in Table I is to set environment variables. Table II provides a listing of common variables that you are likely to encounter on most *nix systems. However, you define other variables environment-wide. For example, we set BUILD32, BUILDN32, and BUILD64 for our compiler flags environment-wide. Execute env or printenv to see a listing of the environment currently set on your machine.
| Variable | Function |
|---|---|
| DISPLAY | The X display to be used; for example, localhost:0. |
| HOME | The absolute path of the user's home directory. |
| HOSTNAME | The Internet name of the host. |
| LOGNAME | The user's login name. |
| The absolute path of the user's mail file. | |
| PATH | The search path. |
| SHELL | The absolute path of the current shell. |
| TERM | The terminal type. |
| USER | The user's current username; may differ from the login name if the user executes the su command. |
Bash Operations
Redirection
Oftentimes we wish to redirect the output from a Bash command to someplace other than it's standard output. Typically, this is redirecting the output from the terminal to a file or from a terminal to another command. Table III summarizes some of the redirectors commonly used.
| Redirector | Function |
|---|---|
| > file | Redirects standard output stream to specified file. |
| 2> file | Redirects standard error stream to specified file. |
| >> file | Redirects standard output stream to specified file, appending output to the file if the file already exists. |
| 2>> file | Redirects standard error stream to specified file, appending output to the file if the file already exists. |
| &> file | Redirects standard output and error streams to the specified file. |
| < file | Redirects standard input stream to the specified file. |
| << text | Reads standard input until a line matching text is found, at which point end of file is posted. |
| cmd1 | cmd2 | Takes the standard input of cmd2 from the standard output of cmd1 (also known as the pipe redirector). |
Making Characters Mean Something Else
A lot of keys have special meanings in some context or other. Quoting is used to remove the special meaning of characters or words. Table IV is a summary of the quoting characters you can use in Bash.
| Character | Function |
|---|---|
| ' | Characters within a pair of single quotes are interpreted literally; that is, their metacharacter meanings (if any) are ignored. Similarly, the shell does not replace references to shell or environment variables with the value of the referenced variable. |
| " | Characters within a pair of double quotes are interpreted literally; that is, their metacharacter meanings (if any) are ignored. However, the shell does replace references to shell or environment variables with the value of the referenced variable. |
| ` | Text within a pair of back quotes is interpreted as a command, which the shell executes before executing the rest of the command line. The output of the command replaces the original back-quoted text. |
| \ | The following character is interpreted literally; that is, its metacharacter (see below) meaning is ignored if it has one. The backslash character has a special use as a line continuation character. When a line ends with a backslash, the line and the following line are considered part of a single line. |
Working With Regular Expressions
A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to arithmetic expressions by using various operators to combine smaller expressions. A regular expression may be followed by one of several repetition operators otherwise known as metacharacters. Table V summarizes the metacharacters and their uses.
| Metacharacter | Meaning |
|---|---|
| * | Matches a string of zero or more characters |
| ? | Matches exactly one character |
| [ abc ...] | Matches any of the characters specified |
| [ a - z ] | Matches any character in the specified range |
| [! abc ...] | Matches any character other than those specified |
| [! a - z ] | Matches any character not in the specified range |
| ~ | The home directory of the current user |
| ~ userid | The home directory of the specified user |
| ~+ | The current working directory |
| ~- | The previous working directory |
