105.1 Customize and use the shell environment edit
Candidates should be able to customize shell environments to meet users’ needs. Candidates should be able to modify global and user profiles.
Key Knowledge Areas
- Set environment variables (e.g. PATH) at login or when spawning a new shell.
- Write BASH functions for frequently used sequences of commands.
- Maintain skeleton directories for new user accounts.
- Set command search path with the proper directory.
The command line interface (CLI or terminal) may seem intimidating at first, but it's important to remember that the command line is really, truly your friend. An army of tools are at your disposal that can take what would be a tedious and lengthy job (like removing the last four characters from every line of a lengthy file) and turn it into a two minute job.
For every Linux distribution the command line prompt will look a little different. For example, on one system you might see your username, the '@' symbol, the machine name, your current directory and the prompt.
This is a very common prompt. You may also see your username, a space, the fully qualified domain name of the computer, the full path to your present working directory followed by the prompt
user linux.box.com /home/user$
The prompt varies from system to system based on a number of things. For example, it may be the default configuration set by the creators of your particular Linux distribution. It could also have been configured by the person who administers the computer or by yourself.
The way you configure the look of your command prompt depends on what shell you use and the shell is the piece that most people commonly refer to as "the command line" when, in reality, it is simply a piece of software that provides an interface to the services of a kernel. The distinction between a 'shell' and the 'command line' is simply that a shell refers to a specific piece of software (e.g BASH, tcsh, ksh, etc) that provides a command line interface. Most modern Linux systems use BASH (Bourne Again SHell) as their default shell.
A Little History
The commands used at the command line may seem a little cryptic due to their tendency to be very short. This is because the roots of the Linux command line are from systems where a single letter entry could take a significant amount of time to travel from a terminal, to a central server and back to the terminal where it was printed onto a roll of paper. In those old systems, the shorter the input was, the better as it meant less time waiting to issue your command and receive output. The best thing you can do to remember what commands stand for is to find out what word the command is an abbreviation for. This can go a long way to remembering the command later.
Summary of Common Commands edit
- ls - This command 'lists' the contents of your present working directory.
- pwd - Shows you what your present working directory is.
- cd - Lets you change directories.
- rm - removes one or more files.
- rmdir- Remove an empty directory.
- mkdir - Make a directory.
- ps - Provides a list of currently running processes.
- cp - Copy a file.
- mv - Move a file (this is also used to rename a file, "moving" it from one file name to another.)
- grep - The global regular expression print program lets you search through a file or output of another program.
- find - Find a file on the filesystem
- man - Displays the manual for most commands (including 'man').
For help about a command, use man command which will bring up the manual for it. Note that some commands are built into your shell and do not have a man page, use your interpreter internal command (should be help <command>).
Login vs. Non-login shell
Login shell: Shell started with login, bash -l or su command
Non-login shell: Shell started any other way
Reason for 2 types of shell
The login shell reads a series of configuration file as it is started.
The non-login shells inherit settings (environment variables) from the parent program which started it.
Variables declared inside a shell are inherited by child processes if the variable has been exported.
If a child process changes its own copy of an exported variable, the parent shell's copy is not changed. The changed value is exported to any sub-child processes.
All exported shell variables keep their export settings in the child process.
If a shell script is called from within a shell a new child non-login shell is started.
If a shell script is started with the '.' command within a shell, then the script is run within that current shell.
Warning: If the called script runs the command exit, the current shell will be terminated!
Interactive and non-interactive shells
Interactive shell: Provides a prompt where the user can type commands.
Non-Interactive shell: No shell prompt – started by calling a shell script or by the command:
sh -c 'command...'
Sequence of events when bash starts
bash --login or su - username or from login
/etc/profile Executed first from interactive login shell. It contains system-wide environment settings.
~/.bash_profile Individual user's shell settings.
~/.bash_login Executed if ~/.bash_profile doesn't exist.
~/.profile Executed if ~/.bash_login or ~/.bash_profile doesn't exist.
Interactive non-login bash
su username or bash -c command
~/.bashrc The only script executed when started. Inherits from parent bash environment.
Non-Interactive non-login bash (forked when scripts are run)
The above scripts are not executed but inherit environment from their parent.
BASH_ENV Reads file in the variable BASH_ENV.
ENV Reads file in the variable ENV if BASH_ENV doesn't exist.
/etc/inputrc - System bash line editing (readline) configuration file
~/.inputrc - Individual bash line editing (readline) configuration file
~/.bash_logout - Executed (if exists) when a login shell exits.
Commands for shell/environment variables
Variablename=Value - Assigns a value to a set (existing) or non-set variable.
export Variablename or
declare -x Variablename - Sets the export tag ON for an existing shell var.
export Variablename=value or
declare -x Variablename=value - Assign a value to a set (existing) or non-set variable and sets its export tag ON, all in one command.
env - Displays all the environment variables (export tag ON)
export - Same as env command except the display format is different eg. declare -x PAGER="less"
Aliases are normally used to create command shortcuts (short names).
Aliases are NOT exportable: not passed-on to sub-shells or child process.
Aliases are not recognized in scripts.
An alias can call another alias within a command.
Example alias li="ls -l"; alias al="li -a" al calls the alias 'li'
Parameters added to alias will be added at the end of the real command.
The parameter variables ($1, $2, $3 ...etc) cannot be used within aliases.
Aliases are often defined in a file run within a script like ~/.bashrc or ~/.profile with the dot '.' command.
alias - Displays all the current shell aliases.
alias AliasName="command(s)..." - Sets a new alias value.
# alias cp="cp -i"
Replaces the original command cp with cp -i for interactive copying (asks before overwriting files).
unalias AliasName - Unsets (deletes) the alias.
They are normally used like fast local mini-scripts within a shell which need to be called more than once within the interactive shell or script.
Variables can be passed-on to functions and will be recognized as $1 $2 $3 etc. In fact the following variables are local within a function:
$1 - $9 - Positional parameters
$# - Number of positional parameters
$* "$1 $2 $3 …"
$@ "$1" "$2" "$3" ...
The positional parameter $0 and all other variables stay global within the shell unless the command local Variablename is given within the function. Within a function, the variable FUNCNAME is used instead of the $0.
Global shell or exported variables can be changed within the function.
Functions do not return variables except for the return number, eg. Return 5. The return command will also terminate the function immediately. The return number can then be read as a normal exit value using $?.
In scripts functions are normally included at the top so that they are read in first.
Environment functions can be put into a file and read in with the . command.
Functions may be recursive. No limit is imposed on the number of recursive calls.
Functions can be exported, using the command: export -f FunctionName
Command Search Priority
When a command is run, bash tries to find the command in the following sequence:
searching the PATH
The first command found is the one which is run.
To force using a builtin command instead of an alias or a function (in the case the same command name exists as alias or function), use the command builtin.
# builtin cat /etc/fstab
Syntax: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
The set command is used to:
-Display all bash variables and their values as well as the functions.
set [bash operating attributes] (using options).
-Assign values to positional parameters:
# set aaa bbb ccc ($1 $2 $3)
The above assigns the value aaa to $1, bbb to $2 and ccc to $3.
Syntax: unset [-fv] [name ...]
For each name, remove the corresponding variable or function.
Each unset variable or function is removed from the environment passed to subsequent commands. If any of RANDOM, SECONDS, LINENO, HISTCMD, FUNCNAME, GROUPS, DIRSTACK are unset, they lose their special properties, even if they are subsequently reset. The exit status is true unless a name does not exist or is readonly.
If no options are supplied, or the -v option is given, the name refers to a shell variable. Read-only variables may not be unset.
# unset -v
# unset -f
The following examples delete the variable DISPLAY, and the function startx respectively.
# unset DISPLAY
# unset -f startx
The following is a partial list of the files, terms and utilities that were used.* /etc/profile