In this lesson,
we're going to discuss the Bash shell environment
and how you can customize it to enhance your productivity
when working in the command line.
Most of us know how to use a mouse and click icons
to access our files, programs, and folders on our devices,
but this is just one way for us
to communicate with our computers.
The command line and the shell
make up a quick, powerful text-based interface
that developers often use more effectively and efficiently
to communicate with their computers
to accomplish a wide set of task
that every system administrator is going to have to handle
at some point in their Linux career.
Now, scripts can greatly improve efficiency
by minimizing the amount of repetitive typing
that you're going to need to do at the command line interface,
and this can enable you to get more work done
in a shorter amount of time.
Surprisingly, Bash is so much easier
than any other programming language
you might have to work with.
You're going to learn that we can harness the power of scripts
to really help you become more productive at doing your job
and be able to get things done much more efficiently.
First off, you may want to change the way things look and act
inside of your system.
You can make all sorts of changes
to make things easier for you to use.
Many people are going to change their prompts,
the color that things are displayed in,
the paths to make launching things easier,
alias is to make commands easier to run,
changing out their default editors, and much more.
The shell environment is really the mechanism
by which Bash or any other shell
will maintain settings and other behavioral details
about their own shell.
This shell creates an environment
when starting up a session,
and it uses the environment settings
to determine how to interact with the user.
The process of creating a new session
is called shell spawning.
This new session is really a copy
and it's called a child process.
For example, the shell spawns a child process
when the user enters a command.
This child process becomes the new process
and can also create other sub-processes
or child processes from itself.
This can result in multiple generations of processes
in a nested tree architecture.
Now, each process is going to call upon the shell environment
and pass its details onto the next generation going upward
until it reaches the kernel and the processor.
Now, when I talk about a script,
I'm really talking about any computer program
that automates the execution of tasks
for particular runtime or shell environment.
Scripts are written in scripting languages,
which are really just a subset of programming languages.
Scripts typically are not full-fledged programs,
but instead they have a much more limited feature set.
And this is used to integrate with other programs
and operating system components
to achieve the automation and efficiency you're looking for.
Think about a script in programming
the way we use scripts in plays or movies or TV shows.
The script is telling the actors what they should say
and what they should do.
Well, in our world,
a script is going to tell the shell exactly what it should do
to accomplish a given task.
Now, as I said,
you can change the details of your environment
within the shell,
and you do this by changing its variables and their values.
Variables refer to things where the values can change
from time to time.
Think about it as a big placeholder.
Most variables are going to be set
either by the operating system when you log in
or by the shell when you invoke it initially.
These variables are the key components
that comprise the shell environment
and tell the shell how it should operate
and how it should act and present itself to you.
For example, you can set shell variables
by typing VAR equals and the value.
So if I wanted to create a variable called MYVARIABLE,
I can say MYVARIABLE=123,
and this will be able to have that new word, MYVARIABLE,
reference the number 123.
Anytime I want to pull out the information from that variable,
I'll simply type it out in the format
of dollar sign, curly bracket, the variable name,
and curly bracket.
And this can be used inside of your scripts
to retrieve the value from the variable
and then perform logical functions or commands upon it.
For example, if I wanted to display the contents
of my variable to the screen,
I would simply enter echo ${MYVARIABLE}
at the command line environment
and hit Enter.
Then I'm going to see what is echoed,
which is the contents of that variable, which was 123.
Now, there are some built in environmental variables
that you're going to come across as well inside of Bash.
This includes things like path and shell and HISTSIZE.
Let's talk a little bit about
these different environmental variables.
Now, an environmental variable
is really a variable that's inherited
from the parent shell process,
and it's subsequently going to be passed on
to any child processes that respond.
These environmental variables can consist of a name,
usually written in upper case letters, and then a value.
For example, if I'm using the path variable,
it's going to contain the name of the path.
Some of the default environmental variables
include certain functions,
such as HOSTNAME,
which is going to be set to the host name of the system.
SHELL, which is going to be set to the shell path
for the system such as /bin/bash.
MAIL, which is going to be set to where the mail
is going to be stored on a given system,
HOME.
This is a variable that defines the home directory
for the user.
PATH.
This is a variable where it's going to specify
the search path that you should look for
whenever you invoke a command
because it knows those are places
that commands are usually kept, such as the /bin directory.
HISTSIZE.
This is a variable that specifies the number of entries
that are stored in the command history
inside that given shell.
And USER.
This is a variable that specifies the name
of the user that's currently logged in.
Now, environmental variables can also be used
to configure localization options
by editing things inside the /etc/locale.conf file
and then assign the appropriate locale to the variable.
The LC_*variable is going to specify
a collection of localization environmental variables.
The LANG variable is going to specify the locale to use
for all the LC_* variables that aren't explicitly defined.
The LC_ALL is going to be a variable
that specifies the locality use for all options.
And this will override whatever is stored
in LANG and LC_*.
Another environmental variable you can use for localization
is for time zone, which is written as TZ.
TZ stands for time zone,
and it specifies the time zone of your given system
and stores that in that variable.
Now, you can effectively change a shell variable
into an environmental variable
by simply using the export command.
For example, if you have a shell variable called SHELL_VAR,
you can enter export SHELL_VAR
to make it an environmental variable.
You can also change the value of the variable
while you're exporting it
by including the existing environmental variables
and the equal sign.
To do this you can enter something like,
export SHELL_VAR="New value" at your command line.
To run the export command,
simply type in export, the options, and the variable name,
and then the value if you're going to use that option.
The next command we have
is known as the environment command, spelled env.
The env Command is used to run a command
with modified environmental variables.
To do this, you're going to simply supply
the name of a variable
and a value in key value pair format
as well as the command that you want to run.
And this will be able to change
the value of the specified variable
for that particular command session only.
To run this, simply type in env, the options,
the name of the variable equals the value,
and the command you want to run.
If you want to see all of the shell variables,
environmental variables, and shell functions,
you can do this by using this set command.
Simply type in set and hit Enter without any arguments
to print out all of your shell variables,
environmental variables, and shell functions.
You can also use the set command
to enable the use of options in a shell script
to change its behavior too.
Now, to summarize some of the differences
between these three commands,
let's do a quick review.
The export command is going to change the value of the variable
for all of your child processes,
the environmental command
is going to view the environmental variables
or change the value of a variable
for a specified command being run,
and the set command is going to view all the shell variables
or environmental variables
and it can be used to change the value of shell attributes.
All right, the next thing we're going to talk about is search,
and I mentioned earlier
that we had an environmental variable called path.
Well, a search path is really a sequence
of various directory paths
that's going to be used by the shell to locate files.
These paths can be assigned
to the path environmental variable,
and the path variable has a list of all the directory names
separated by colons.
You can also set a new path
to the existing group of path names, modify that path,
or delete a path by overwriting the contents
of this variable.
Now, earlier I talked about
the HISTSIZE environmental variable.
There's also a companion one to this
known as the HISTFILESIZE variable.
This is a shell variable
that enables you to set the maximum number of lines
contained in the command history file.
This is slightly different than HISTSIZE,
because HISTSIZE is telling you how many commands
you can store in memory,
but HISTFILESIZE tells you how many you can store
in the command's history file.
By overriding the value of the HISTFILESIZE,
you can set exactly how many lines or previous commands
you want to be able to have stored in that file.
For example, if you assign a value of 20 to this variable,
the history file will cut off after it gets 20 lines.
By default, the value of this variable is 1000,
meaning your system is memorizing
the last 1000 things you've done in the command line
and storing that in the history file.
Another command we have is known as alias.
The alias command is used to customize the shell environment
by generating command line aliases.
Now, aliases are essentially short hands
for longer expressions.
And by using aliases, you can create short sequences
that you can type in to represent longer commands
and all the various options and arguments it has.
For example, I often find myself using
the ls -la command.
This tells me to list out things in a directory
and showing me everything there
in a list that I can understand
and see all of their different characteristics,
such as the owners and the file permissions.
But if I didn't want to type that out each time,
I can create an alias called myls
that then includes the ls -la command inside of it.
To do this, we can go ahead and set it by typing an alias,
the alias name, equals, and the command with options.
So in my case, that would be alias, myls='la -ls'
Now, after running this alias command,
anytime I type in myls in the command line,
it's going to execute the substitute command
that I made the alias for,
which was ls -la.
This is really helpful when you have very long commands
you want to run
and you want to use this as a shorthand.
In this case, I didn't save that much effort
because myls is four characters
and ls -la is six characters.
So it didn't save me too much time here.
Now, another thing you want to think about
is how you can find out how long it takes
for a command to run,
and this will be able to give you a better idea
how you can optimize your frequently used tasks.
To do this, you can use the time command.
The time command is used to gather information
about how long it takes to execute a given command
as well some additional statistics about the input output
and memory used by that command's execution.
By default, the time command is going to give you
three different pieces of statistical information.
The first is the real time that elapsed
between when you started running the command
and when it terminated,
the second is the amount of time spent
running the code inside the user space,
which is known as the user CPU time,
and the third is the system CPU time,
which is the time spent running the code
inside the kernel space.
So by having these three pieces of information
you have the total amount of time,
the time in user space, and the time in kernel space.
This can tell you a lot about a given program
and what permissions it's using
at certain times during it's execution.
To run the time command,
simply type in time, the options,
and the command that you want to evaluate.
All right, we covered a lot in this lesson,
but let's do a quick recap
of what you can do when things go wrong.
Sometimes you're going to encounter some issues
when you're using
or customizing these Bash shell environmental variables.
When you add an alias, if you get an error,
always go back and check your syntax
to make sure you typed it in correctly.
Whenever you're executing scripts or other programs,
if they're not stored in a normal location
for executable files,
they may have trouble finding them.
And so you may need to add their location
to the path variable,
or you're going to have to execute those
by using a dot slash proceeding the command or script's name
to say use the one right here in this current directory.
Remember, if you need to set a variable
for all your shell child processes,
you're going to use the export command.
Also, remember all your environmental variables
do get stored by default in your Bash profile.
So you can configure the environmental variables
inside that file by going to -/.bash_profile
and making those variables available
to all of your shells by default.
For example, if you have a service account
that requires certain environmental variables,
you can set those up inside your Bash profile
for that account.
Just edit the Bash profile file
to change those default variables,
and it will then inherit those
every time you launch a new shell.
Finally, sometimes it's important
that you set environmental variables
for certain software packages
based on the dependencies they have.
For example, let's say that we have a Java application
that relies on the Java runtime environment.
It may only be able to find and access
that runtime environment
if it's referenced inside the path variable,
which is an environmental variable after all.
So you always need to make sure
these things are working properly and working together
to allow you to use your system more effectively.