- [Narrator] Playing with variables
and environment variables.
In the previous video, we learned about
printing in the terminal.
In this video, we're going to see how
to play with variables and environment variables.
Variables are essential components
of every programming language.
And they're used to hold varying data.
Scripting languages usually do not require
variable type declaration before it's used
as they can be assigned directly.
In BASH, the value for every variable is strict.
Regardless of whether we assign variables
with quotes or without quotes.
Furthermore, there are variables used by
the shelling environment and the operating environment
to store special values
which are called environment variables.
Variables are named with the usual naming constructs.
When an application is executing,
it will be past a set of variables
called environment variables.
To view all the environment variables related
to a terminal, issue the ENV command.
For every process, environment variables
and it's runtime can be viewed by.
Set pid with the process ID of the process.
pid always takes an integer value.
For example, assume that an application
called G-edit is running.
We can obtain the process ID of G-edit
with -d pgrep command as shown here.
You can obtain the environment variables
associated with the process by
executing the command which is showing here.
Note that many environment variables
are stripped off for convenience.
The actual output may contain numerous variables.
The aforementioned command returns a list
of environment variables and their values.
Each variable is represented as a name equals value pair
and are separated by a null character, \0.
If you can substitute the \0 character
with \n, you can reformat the output
to show each variable equals value pair in each line.
Substitution can be made using the TR command as shown here.
Now let us see how to assign and manipulate variables
and environment variables.
Let's see how to do it.
Var is the name of a variable
and value is the value to be assigned.
If value does not contain any space character,
such as space, it need not be enclosed in quotes.
And otherwise, it is to be enclosed
in single or double quotes.
Note that var, space, equals, space, value
and var equals value are different.
It is a usual mistake to write var=value
instead of var = value.
The latter one is the assignment operation
whereas the earlier one is an equality operation.
Printing contents of a variable is done by prefixing $
with the variable name as...
We will receive an output as...
We can use variable values inside print F
or echo in double quotes.
Now, the output will be...
Environment variables are variables
that are not defined in the current process,
but are received from the pairer process.
For example, HTTP underscore proxy
is an environment variable.
The variable defines which proxy sev
should be used for an internet connection.
Usually, it is set as...
The export command is used to set the ENV variable.
Now in the application, executed from
the current shell script, will receive this variable.
We can export custom variables for our own purposes
in an application or shell script that is executed.
There are many standard environment variables
that are available for the shell by default.
For example, path.
A typical path variable will contain...
When given a command of execution,
the shell automatically searches for the executor
in the list of directories in the path environment variable.
Directory paths are delimited by the colon character.
Usually $path is defined in /etc/environment
or /etc/profile.
or ~/.bashrc.
When we need to add a new path to the path environment
we use these commands...
Or alternatively, we can use...
Here we have added /home/user/bin to path.
Some of the well known environment variables
are home, pwd, user, uid, shell, and so on.
When using single quotes, variables will not be expanded
and will be displayed as...
This means whereas $echo, $var will print the value
of the $var variable if defined
or nothing at all if it is not defined.
Let's see more of tips associated with standard
and environment variables.
Finding the length of a string and get the length
of a variable value using the command...
For example,...
The length parameter will bare
the number of characters in the string,
identifying the current shell.
To identify the shell which is currently being used,
you can use the shell variable, like so.
For example,...
Checking for superuser.
UID is an important environment variable
that can be used to check whether the current script
is run as a root user or a regular user.
For example, the UID value for the root user is zero.
Modifying the bash prompt string
{username@hostname:~$}.
When we open a terminal or run a shell,
we see a prompt string such as, user@hostname:/home/$.
Different GNU/linux distributions
have slightly different prompts and different colors.
We can customize the prompt text
using the PS1 environment variable.
The default prompt text for the shell is set
using a line in the ~/.bashrc file.
We can list the line used to set
the PS1 variable as follows.
To set a custom prompt string into the following command.
We can use colored text using special escape sequences
such as E hard bracket one code on three one.
Referring to the printing in
the terminal recipe of this chapter.
There are also certain special characters
that expand to system parameters.
For example, /u expands to user name,
/h expands to host name,
and /w expands to the current working directory.
Awesome.
In this video, we've successfully learned
how to play with variables and environment variables.
In the next video, we'll make function
to prepend to environment variables.