Overview
Variables
- A variable is a label that equates some value.
- The value can change over the time, across the accounts or across the systems but the label remains constant.
For Example:
- A shell script may place a file in
$HOME
, a reference to the value of the variableHOME
. - This value may differ, depending on who is running the shell script.
NOTE!: Some variables, like $HOME
, Are intended to be referenced but not set by the user.
- However, users can also create their own variables:
For Examples:
Types of Variables
Local(Shell) Variable
- Bash variables are local to a single shell By Default.
- Set with
VARIABLE=VALUE
Environment Variable
- Accessed by some programs for configuration
-
Set with
export VARIABLE=VALUE
- The
set
,env
, andecho
commands can be used to display all variables, environment variables, and a single variable value, respectively.
For Examples:
Common Variables
/-------------------------------------------------------------------------------------------------------\ | | | PS1 | Appearance of the bash prompt | | PATH | Directories to look for executables in | | EDITOR | Default text editor | | LS_COLORS | Display different file types in different colors | | HISTFILESIZE | Maximum number of commands saved in a user's .bash_history file | | | \-------------------------------------------------------------------------------------------------------/
Examples:
Common Escape Sequences
/----------------------------------------------------------------------\ | \u | User Name | | \h | Short Hostname (Not FQDN) | | \w | The Current Working Directory | | \! | The History Number Of This Command | | \$ | If The Effective UID Is 0, a #, Otherwise a $ | \-----------------------------------------------------------------------/
NOTE!: For complete list of escape sequence, see the PROMPTING section of the bash man page.
Information Variables
/-----------------------------------------------\ | | | HOME | User's Home Directory | | EUID | User's Effective UID | | | \-----------------------------------------------/
Aliases
- Aliases are shortcut names for longer commands.
- Use alias by itself to see all set aliases
- Use alias followed by alias name to see alias value
- Aliases can be used for security purposes to force you to use certain flags
Example:
Example:
NOTE!: In this case, If you ever want to use the rm command itself, instead of your alias. You can precede the command with a blackslash ().
NOTE!: The alias value must be a single word and so you will almost always want to quote the value as shown.
Aliases are local to a single shell By Default.
How Bash Expand a Command Line
- Split The Line Into Words
- Expand Aliases
- Expand Curly-Brace Statements ({})
- Expand Tilde Statements (~)
- Expand Variables ($) And Command-Substitution ($() and ``)
- Split The Line Into Words Again
- Expand File Globs (*, ?, [abc], etc)
- Prepare I/O Redirections (<, >)
- Run The Command!
Example:
Preventing Expansion
Backslash (\) Makes The Next Characters Literal
Quoting Prevents Expansion
'
(Single Quotes) - Inhibit All Expansion"
(Double Quotes) - Inhibit All Expansion Except- \ (Backslash) - Single Character Inhibition
- $ (Doller Sign) - Variable Expansion
- ` (Back Quotes) - Command Substitution
- ! (Exclamation Point) - History Substitution
Examples:
Login vs Non-login Shells
- Startup Is Configured Differently For Login and Non-login Shells
/---------------------------------------------------------------\ | Login Shell | Non-login Shell | |---------------------------------------------------------------- | | | /etc/profile | ~/.bashrc | | /etc/profile.d/ | /etc/bashrc | | | /etc/profile.d/ | | ~/.bash_profile | | | ~/.bashrc | | | /etc/bashrc | | | | \---------------------------------------------------------------/
Login Shell
su -
Any Shell Created At Login (Includes X Login)
Non-login Shell
- su
- Executed Script
- Graphical Terminals
- Any Other Bash Instance
Bash Startup Tasks
- By Default, Most of the configuration items, That we have seen in this unit are only valid for the given shell.
- But Typically, We want settings to be established every time we start a shell, Rather than typing all of our variables, aliases, and other commands on a per shell basis.
- To accomplish this, we use startup scripts, scripts that runs when a shell is created.
profile
- Stored in
/etc/profile
(Global) and~/.bash_profile
(User) - Runs For Login Shells Only
- Used For
- Running Commands (Example: Mail Checker Script)
- Setting Environment Variables (Such As PATH, USER, LOGNAME, MAIL, HOSTNAME, HISTSIZE, HISTCONTROL)
bashrc
- Stored in
/etc/bashrc
(Global) and~/.bashrc
(User) - Runs For All The Shells
- Used For
- Setting umask
- Defining/Undefining Aliases
- Setting Local Variables (Such As PS1)
Bash Exit Tasks
logout
- Stored in
~/.bash_logout
(User) - Runs when Login Shell Exits
- Used For
- Creating Automatic Backup
- Cleaning Out Temporary Files
Sourcing Files
- Changes made to profile and bashrc files need to be sourced.
-
This will execute the file and read it into the running shell.
- For Instance, If you make a changes to
/etc/bashrc
. Opening a new terminal will source this file and activate your changes. However, The original terminal where you made those changes still has old settings. From the original terminal, Run one of the following command