Using Aliases

One common shell customization is setting aliases, which enable you to type shorthand versions of commands or configure the shell to automatically add certain parameters to commands. For instance, the default behavior of the cp command is to overwrite files that match the target filename without prompting. If you prefer to have cp prompt you before overwriting an existing file, you could enter the following line in a bash startup script:

This line tells bash to silently substitute cp -i whenever you type cp. You can also create a shorthand for a command; for instance, if you prefer to type lo instead of logout, you could create an alias like this:

alias lo='logout'

You normally place aliases in non-login file locations—/etc/bashrc, -/.bashrc, or the like. This practice ensures that the aliases will be available whenever you launch a shell, whether or not it's a login shell. If you don't want to use an alias on a one-time basis, you can do so in a couple of ways. First, you can surround the command in double quotes, as in "cp" filel file2 to use cp without -i to copy the file. Second, you can type the complete path to the original executable, as in /bin/cp filel file2.

Setting Environment Variables

Environment variables are pieces of data that Linux makes available to programs you run. For instance, a Usenet news reader needs to know the address of the news server from which it should retrieve messages. Some programs use program-specific configuration files for this purpose, but others rely on environment variables. The NNTPSERVER environment variable, for instance, holds the name of a Usenet news server. Using environment variables for this purpose makes sense when there are several programs that might use the information—you only need to set the environment variable once, and if all the programs know to access that variable for the relevant information, you're done. Some programs rely on environment variables even for program-specific settings, so you might need to set environment variables even for individual programs.

To set an environment variable, use the export command in a bash startup script, as export

You can reference existing environment variables when setting a new one by preceding the existing environment variable with a dollar sign ($). For instance, one important environment variable is PATH, which specifies the directories in which Linux searches for programs when you type their names. Each directory is separated from its neighbors by a colon (:). You can add a directory to PATH with a command like export PATH=$PATH:/opt/

By convention, environment variables are all-uppercase. Variables used by a single bash script, as described in the upcoming section, "Using Variables," are lowercase or mixed case. If you need to set an environment variable for a program, you should describe that variable in its documentation. You can then add the variable to a global or local configuration file, as appropriate.

0 0

Post a comment