Upon completion of this unit, you should be able to:
- Know how to use local and environment variables
- Know how to inhibit variable expansion
- Know how to create aliases
- Understand how shell parses a command line
- Know how to configure startup files
- Know how to handle input with the read command and positional parameters
Bash Variables
1) Variables are named values
- useful for storing data or command output
2) Set with VARIABLE=VALUE
3) Referenced with $VARIABLE
$ File=$(ls /etc/)
Environment Variables
1) Variables are local to a single shell by default
2) Environment variables are inherited by child shells
- Set with export VARIABLE=VALUE
- Accessed by some programs for configuration
The set, env and echo commands can be used to display all variables, environment variables and a single variable
- set | less
- env | less
- echo $PATH
Some Common Variables
1) Configuration Variables
- PS1: Appearance of the bash prompt
- PATH: Directories to look for executables in
- EDITOR: Default text editor
- HISTFILESIZE: Number of commands in bash history
2) Information variables
- HOME: User’s home directory
- EUID: User’s effective UID
Aliases
1) Aliases let you create shortcut to commands
2) Use alias by itself to see all set aliases
3) Use alias followed by an alias name to see alias value
$ alias dir
alias dir=’ls �ClaF’
How bash Expands a Command Line
1) Split the line into words
2) Expand aliases
3) Expand curly-brace statements ({})
4) Expand tilde statement (~)
5) Expand variables ($)
6) Command-substituation ($() and ``)
7) Split the line into word again
8) Expand file globs (*, ?, [abc], etc)
9) Prepare I/O redirections (<, >)
10) Run the command!
Preventing Expansion
1) Backslash ( \ ) makes the next character literal
2) Quoting prevents expansion
- Single quotes (‘) inhibit all expansion
- Double quotes (“) inhibit all expansion, except
- $ (dollar sign) �C variable expansion
- ` (backquotes) �C comand substitution
- \ (backslash) �C single character inhibition
- ! (exclamation point) �C history substitution
Login vs non-login shells
1) Startup is configured differently for login and non-login shells
2) Login shells are:
- Any shell created at login (include X login)
- su -
3) Non-login shells are:
- su
- graphical terminals
- executed scripts
- any other bash instances
Bash startup tasks: profile
1) Stored in /etc/profile (global) and ~/.bash_profile (user)
2) Run for login shells only
3) Used for
- Setting environment variables
- Running commands (eg mail-checker script)
Bash startup tasks: bashrc
1) Stored in /etc/bashrc (global) and ~/.bashrc (user)
2) Run for al shells
3) Used for
- Setting local variables
- Defining aliases
Bash exit tasks
1) Stored in ~/.bash_logout (user)
2) Run when a login shell exits
3) Used for
- Creating automatic backups
- Cleaning out temporary files
Scripting: Taking input with positional parameters
1) Positional parameters are special variables that hold the command-line arguments to the script
2) The positional parameters available are $1, $2, $3, etch.. These are normally assigned to more meaningful variable names to improve clarity
3) $* holds all command-line arguments
4) $# holds the number of command-line arguments
Scritping: Taking input with read command
1) Use read to assign input values to one or more shell variables:
- -p designates prompt to display
- read reads input from standard input and assigns one word to each variable
- Any leftover words are assigned to the last variable
- read �Cp “Eneter a filename:” FILE
End of Unit12
1) Questions and Answers
2) Summary
- local and environment variables
- command line parsing
- configuring the shell environment
- positional parameters and the read command