Variables
One of the first things that we need to cover is how to deal with variables. They were briefly talked about in the other chapters, but they will be cover in slightly more depth here.
The variable functionality provides a way for the script to get input, store data temporarily, and display output to the user or sending data to a calling script.
There are two categories that variables can be divided into:
Environment Variables: are maintained by the system, they are inherited by the current and any child shells or processes that are spawned.
Shell Variables: are contained exclusively within the shell in which they were set or defined. They are mostly used to keep track of temporal data, like the current working directory in a session.
Shell Variables
There are generally two types of shell variables. Those that are maintained by the system, and the others that are user defined for scripts or passing information into other commands, scripts or programs.
As stated earlier, the values in the shell variables can be user defined. Meaning that the user is responsible for updating these values.
Note:
Variable names are case sensitive, so test, Test, TEST are three different variables.
Examples:
To create a custom variable, type:
test=HelloWorld
To display the contents of a custom variable, type:
echo $test
To clear out (or undefine it) a shell variable, type:
unset shell_variable_name
Notes:
To see all the shell variables, type:
set | less.
To demote an environment variable to a shell variable, type:
export -n env_variable_name
Tip:
You can assign the STDOUT of a command and its arguments to a variable then perform another operation with the infromation, for example:
x=$(cmd a1 a2 a3)
To see the contents of variable x, type:
echo $x
For more information on command substitution (i.e. $()), see the following section "Advanced: Using command Substitution"
The next table is a list of the commonly used shell variables in Linux. Depending on your distro and version some of these variable may or may not exist.
Shell Variable
Variable
Variable Description
Viewing Variable
_ (underscore)
The most recent used previously executed command.
echo $_
HOME
Stores the home directory of the current user.
echo $HOME
LANG
The current language and localization settings, including character encoding.
echo $LANG
LS_COLORS
Defines the color codes that are used to add colored output to the ls command.
echo $LS_COLORS
OLDPWD
Displays the previous working directory path.
echo $OLDPWD
PPID
The process ID of the parent process of the shell.
echo $PPID
PWD
Displays the current working directory path.
echo $PWD
SHELL
Displays which login shell is being utilized.
echo $SHELL
TERM
Displays the terminal emulation type.
echo $TERM
USER
Displays the logged in user name.
echo $USER
Environmental Variables
There are also two types of environment variables. Those that are maintained by the system, and the others that are user defined for scripts or passing information into other commands, scripts or programs.
As stated earlier, the values in the environment variables are maintained by the system and are configured at startup by a configuration file. Meaning that the system will automatically create and update the values in these variables.
The environment variables have to be changed using the export command otherwise they will not be inherited by the commands that are calling them from other shell instances.
Examples:
To create or modify an environment variable, type:
export TEST=HelloWorld
To display the contents of an environment variable, type:
echo $TEST
Note:
To see all the system maintained environmental variable, type:
env | less.
These are only system variables that are not in this list.
Environmental variables can be used to pass information into processes that was spawned from a shell.
Tip:
The printenv command is an equivalent of the env command to display values of all or specified environment variables.
The next table is a list of the commonly used environmental variables in Linux. Depending on your distro and version some of these variable may or may not exist.
Environment Variables
Variable
Variable Description
Viewing Variable
BASH_VERSINFO
Stores the version of BASH for this instance (machine readable)
echo $BASH_VERSION
BASH_VERSION
Stores the version of BASH for this instance
echo $BASH_VERSION
BASHOPTS
The list of options that were used when BASH was started.
echo $BASHOPTS
CDPATH
The search path used with the cd built-in command.
echo $ CDPATH
COLUMNS
The number of characters wide that output can be written on the screen.
echo $COLUMNS
DIRSTACK
The stack of directories that are available with the pushd and popd commands.
echo $DIRSTACK
HISTFILE
Stores the name of the command history file.
echo $HISTFILE
HISTFILESIZE
Stores the maximum number of lines that can be held by the history file.
echo $HISTFILESIZE
HISTSIZE
Stores the maximum number of commands that can be held by in the history in memory.
echo $HISTSIZE
HOSTNAME
Stores the system name of the computer.
echo $HOSTNAME
IFS
Input Field Separators. This is normally set to ⟨space⟩, ⟨tab⟩, and ⟨newline⟩.
echo $IFS
MAIL
The name of a mail file that's checked for new mail.
MAILCHECK
How frequently in seconds that the shell checks for new mail in the files specified by the MAILPATH or MAIL file.
MAILPATH
A colon ":" separated list of file names, for the shell to check for incoming mail.
Note: This environment setting overrides the MAIL setting. There is a maximum of 10 mailboxes that can be monitored at once.
PATH
The search path for commands.
Note: This is a colon-separated list of directories in which the shell searches for commands.
echo $PATH
PS1
The shell prompt settings.
Note: The PS2 variable is used to declare secondary prompt when a command spans multiple lines.
echo $PS1
PS2
The secondary prompt string, which defaults to “> ”.
echo $PS2
PS4
Output before each line when execution trace (set -x) is enabled, defaults to “+ ”.
echo $PS4
SECONDS
Displays a count of the number of seconds the shell has been running.
echo $SECONDS
SHELLOPTS
Shell options that can be configured with the set option.
echo $SHELLOPTS
UID
The user ID of the current logged in user.
echo $UID
Persistent Variables
Since variables (i.e. Environment and Shell) are stored in RAM they are temporal, and will disappear when the shell's process is killed or the computer is shutdown. Persistent variables are stored in a configuration file and loaded when the system is loaded or when the shell starts.
When creating predefined variables at login depends upon how Bash was started and which configuration file gets loaded. There are basically four different user session types (aka login shell), Login, Non-Login, Interactive, and Non-Interactive.
Login: A shell session begins after a user authenticates (i.e. local or ssh terminal session). The login shell session, reads the configuration details from the /etc/profile file. It will then search for the first user login shell configuration file in the user’s home directory to load any user-specific configuration (i.e. ~/.bash_profile -OR- ~/.bash_login -OR- ~/.profile).
Non-Login: When a new shell is started from within an authenticated login session. For example, when you run a Bash command from the terminal, a non-login shell session (under the logged in user's credentials) is started. A non-login shell session reads the /etc/bash.bashrc file and then the user-specific ~/.bashrc file to configure the environment.
Interactive: A shell session that is attached to a terminal and in use. For example, an ssh session is defined as an interactive login shell.
Non-Interactive: A shell session is one that is not attached to a terminal session, and not currently in use. For example, when you a run script from the command line run in a non-interactive, non-login shell. Non-interactive shell sessions first read the environment variable named BASH_ENV, and then reads the file specified in this variable to setup the new shell environment.
Tip:
To force your current shell session to re-read the config file, type:
source ~/.bashrc
Notes:
Individual user defined variable (not system wide) that can be made available to both login and non-login shell sessions can be define in the ~/.bashrc file.
To create system wide environmental variables, you need to add the variable definition(s) to one of the following files:
/etc/profile
/etc/bash.bashrc
/etc/environment
Shell vs. Environment vs. Persistent Variables
If you look at a hierarchical chart showing the different layers of the operating system, with the user layer where you run your commands at the top, and hardware at bottom. This is where you can see the difference between shells vs. environment variables.
Environment variables are run at a lower level, they are available to all shell instances. Also since persistent variables are loaded from a configuration file at the computer or shell startup, they can withstand the process being killed or computer being turned off.
System Layers: Shell/Environment/Persistent Variables
User Layer
Commands/Programs (Console)
Shell Variables
These variables are only available in the current instance of the shell.
Shell (Instance)
Multiple instances of the shell can be running, each with own unique set of variables.
Operating System Layer
Environmental variables
These variables are available to all shell instances)
Persistent Variables
These are loaded from a configuration file at startup of the computer or shell.
Kernel
This is where the modules, drivers, etc. are loaded
Hardware Layer (i.e. the CPU, RAM, Storage, NIC)
String Types
Bash only supports a few different types of variables, such as: strings, integers, arrays and read-only. By default Bash will treat all variables as strings, unless otherwise declared. You're even allowed to perform mathematical operation on a string even though it's not an integer.
For example, if you typed:
a=1234
let "a += 1"
echo "a = $a"
The output of $a would be 1235
Now if you declare $a a as being an integer:
a=1234
declare -i a
let "a += 1"
echo "a = $a"
The output of $a would be 1235
Using the declare command, you can also make a variable readonly, which means it can't be modified, it can only be deleted.
For example, if you typed:
declare -r a=1234
let "a += 1"
echo "a = $a"
The output of $a would be 1234
Bash also supports array variables, which allow one variable to hold related information (or values). It basically is one variable that acts like many, each set of values has an index value of 0 to ….
For example, to create an array of Linux distros, type:
Distro[0]='Red Hat'
Distro[1]='Debian'
Distro[2]='Ubuntu'
To see the value in the array under the Index of 2, type:
echo ${Distro[2]}
Tips:
Another way to declare a variable is use the declare command to get the similar effect in fewer lines of code, type:
declare -a Distro=('Red hat' 'Debian' 'Ubuntu')
To see all the values in an array, type:
echo ${Distro[@]}
To see number of values in an array, type:
echo ${#Distro[@]}
To see length of value in an array, type:
echo ${#Distro[1]}
String Variable Manipulation
Now that you understand the different types of variables, you now have to learn how to modify the data in a string. This is done with special functions that grant you that ability.
Bash supports, two types of string manipulation functions. These functions come in handy when you have to remove text from a string.
The first one, ${string#remove_text} removes the specified characters from the beginning of the string. The second one, ${string%remove_text} removes the specified characters from the end of the string.
Ex: Next are two examples of these functions:
B="12345"
C=${B#123}
echo $C
Result: 45
-OR-
B="12345"
C=${B%45}
echo $C
Result: 123
Comments
Post a Comment