BASHSHELL
BASHSHELL
Linux Fundamentals
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
2 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
This section defines a Linux shell and introduces the Bash shell.
The Linux shell
What is a shell?
• A shell accepts and interprets commands.
• A shell is an environment in which
commands, programs, and shell scripts
are run.
• There are many types of Linux shells
available. Bash is one of them, and this
section discusses it further.
4 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
What is a shell?
The primary purpose of a shell is to allow the user to interact with the computer
operating system. It has two different functions. One is a program and the other is a
command interpreter. As a program shell, it provides the interface for utilities and
programs. As a command interpreter, a shell accepts and interprets the commands
you enter into the command line interface (CLI) or terminal.
The Bourne Again Shell: Bash
What is the Bourne Again Shell (Bash)?
• Bash is the default shell in Linux.
• It offers an efficient environment for
interacting with the operating system and
scripting.
5 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
What is Bash?
Bash is a programming language for running commands. Bash is the default shell in
Linux operating systems. It is widely used, so some familiarity with Bash is expected
in many systems or development roles.
Shell variables
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
This section explains what Bash shell variables are, how to name them, what the
rules are for writing them, and how to assign a value to them.
Shell variables
]$ name=value
In a shell, a variable is used to store values. A variable value can be a string, a number, or special characters; by default, variables
are strings.
7 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Scripts or other commands can call shell variables. The values that these shell
variables represent are then substituted into the script or command.
The next slide provides a few shell syntax rules for creating variables; you will look at
these rules.
Syntax rules: Variable syntax structure
]$ restart_student=
By convention and as a good practice, the name of a variable that a user has created is in lowercase. Environment (system)
variable names are capitalized. Also, there is no space before or after the equal sign.
8 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
When defining a variable, the variable name must be prefixed with the dollar ($)
symbol.
The variable must contain no spaces or special characters within the variable name. A
variable name can contain only letters (a to z or A to Z), numbers (0 to 9), or the
underscore character ( _), and they are usually capitalized (e.g. VARIABLE).
Syntax rules: Naming variables
]$ restart_student= ]$ student=
]$ restart_us_cohort_1= ]$ us_cohort=
]$ restart_spring_student ]$ spring!=
Variable names are crucial; they assist in making sense of what Unhelpful, confusing, or vague variable names can contribute to
the variable is used for and the readability of the variable. the disconnect between a variable and its value.
9 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Naming variables in Bash can be difficult, but if you name variables properly, it is
useful. Good variable names are crucial; they assist in making sense of what the
variable is used for and the readability of the variable. It is good practice to be
consistent in your naming pattern. Unhelpful, confusing, or vague variable names can
contribute to the disconnect between a variable and its value.
The variable names on this slide are examples of a few good and a few bad variable
names.
Assigning a value to a variable
]$ name=value
Variables are assigned by using the = operator. The value of the variable is located to the right of the = operator.
]$ restart_student=Li Juan
Variables can be a name, a number, or special characters; by default, all variables are treated as strings, even if a variable is
assigned to a number. There is no space between the variable name and the value.
10 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
A value can be assigned as a number, text, file name, device, or other data type.
Again, as discussed earlier, a variable is used to store values. Variables can be a name,
a number, or special characters; by default, all variables are treated as strings, even if
a variable is assigned to a number. Variables are assigned by using the = operator.
There is no space between the variable name and the value.
Displaying shell variables
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
In this section, you learn how to display variables using the echo command.
Displaying shell variables
]$ echo $VARIABLE_NAME
or
]$ echo $(VARIABLE_NAME)
You can also use the echo command to view the output from environment variables.
12 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
To display the value of a variable, use the echo $VARIABLE_NAME. Also use the echo
command to view the output from environment variables or system-wide variables.
Environment variables
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
]$ KEY=VALUE
In a shell, environment variables are the same as shell variables. Structurally, these variables are no different from each other.
Both use the key-value pair, and they are separated by the equal (=) sign.
14 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Environment variables are structurally the same as shell variables; they are no
different from each other. Both use the key-value pair, and they are separated by the
equal (=) sign.
Environment variables are system wide, and all child processes and shells inherit
them. With environment variables, you can pass information about the current
operating environment to a program running. Finally, applications and daemons
reference environment variables as needed.
Common environment variables
Use the echo command to view environment variables.
]$ echo $VARIABLE_NAME
15 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
The table on this slide displays a list of a few common environment variables you may
encounter when using Linux.
Common environment variables: $HOME
The echo command is used to view environment variables.
]$ echo $HOME
/home/username
In Bash, the output for the $HOME environment variable will Environment Variables Description
display the user's home directory because it is set as an
environment variable when you log in. Defines the user's home
$HOME
directory
Indicates the location of the
$PATH
commands
16 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
In Bash, for example, the output for the $HOME environment variable will display the
user's home directory.
Common environment variables: $PATH
The echo command is used to view environment variables.
]$ echo $PATH
/usr/local/sbin:/bin/bin…. : /root/bin
17 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Directions
1. Open the Bash shell in a Linux environment.
2. At the command prompt, enter the echo command and an
environment variable from the following list:
– echo $HOME
– echo $SHELL
– echo $USER
– echo $PATH
18 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
2. At the command prompt, enter the echo command and an environment variable
from the following list:
• echo $HOME
• echo $SHELL
• echo $USER
• echo $PATH
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
This section discusses the Bash environment and the env command.
The env command
The env command is used to view environment variables.
]$ env [OPTIONS]
20 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
The env command is a shell command for Linux. You use this command to print a list
of environment variables or run another utility in an altered environment.
The initialization process for Bash environment files
User logs in
21 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
During the initialization process of Bash environment files, two different initialization
shell files are invoked.
When you log in, Bash reads the /etc/profile instructions. The /etc/profile file usually
sets the shell variables PATH, USER, HOSTNAME, etc.
The /etc/bashrc file contains system-wide functions and aliases, including other
configurations that apply to all system users.
Instructor demonstration Displaying the Bash environment files
You will view the files that are used to set a user's configuration
during system startup.
Directions
Steps to view the .bashrc
1. Open the Bash shell in a Linux environment.
2. At the command prompt, enter the ls -a command to view hidden
files.
3. Enter the cat .bashrc command to display the content of the file.
22 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
]$ alias alias_name=‘command’
How it works: Enter the command alias, desired alias, and then the command to run. Ensure the value of the command in
single quotation marks.
24 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
By using aliases, you can define new commands by substituting a long command with
a short one. Aliases can be set temporarily in the current shell, but it is more
common to set them in the user's .bashrc file so that they are permanent. In the
example, ll is often substituted or aliased to ls –l.
]$ unalias [alias_name]
Example: Now if you run the command, you will find that
the command is no longer valid.
25 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
The unalias command removes the configured alias if it is not configured in the
.bashrc file.
In the example, the unalias command is used to remove the new alias that was
created earlier.
Add an alias to the .bashrc file
Aliases can be added to the .bashrc file.
]$ nano ~/.bashrc
How it works: Use vim or nano to edit the file. Add your aliases.
# Aliases
# alias [name[=value]…]
26 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
The .bashrc file is stored in the home directory of each user. As mentioned earlier,
the .bashrc file is used to store configurations specific to the user. When creating an
alias, remember that after you create it, the alias is applied to the .bashrc file.
Checkpoint questions
What commands do you think you will create aliases for in Bash?
27 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
1. Some commands used for aliases (other than the commands that were listed
previously) include those that return the following:
• The name of the user who is running the application
• A user’s preferences when they play a game (for example, sound and music
levels)
• A value that is commonly used when running calculations (pi, the fixed cost
of product)
2. The env command displays the environment variable. You use this command to
display your current environment.
Key takeaways • Variables are ways of storing values and reusing the
value in commands scripts.
28 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021 Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from Amazon Web Services, Inc. Commercial copying, lending, or selling is
prohibited. Corrections, feedback, or other questions? Contact us at https://support.aws.amazon.com/#/contacts/aws-training. All trademarks are the property of their owners.
Thank you.