0% found this document useful (0 votes)
40 views22 pages

3-Configurig Bash Shell

This document summarizes key aspects of configuring the Bash shell, including: - Shell variables can be assigned values and alter program behavior depending on the variable value. Local variables are only available in the current shell, while environment variables are passed to other programs. - The PATH variable contains a list of directories searched to find and execute commands. It is one of the most critical environment variables. - Initialization files like .bashrc and .bash_logout contain commands executed on shell start/exit and can be used to set aliases, environment variables, and other configurations. - Command history allows re-executing or editing previous commands using up/down arrows or Ctrl-R search. The history command lists

Uploaded by

roderickvicente
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views22 pages

3-Configurig Bash Shell

This document summarizes key aspects of configuring the Bash shell, including: - Shell variables can be assigned values and alter program behavior depending on the variable value. Local variables are only available in the current shell, while environment variables are passed to other programs. - The PATH variable contains a list of directories searched to find and execute commands. It is one of the most critical environment variables. - Initialization files like .bashrc and .bash_logout contain commands executed on shell start/exit and can be used to set aliases, environment variables, and other configurations. - Command history allows re-executing or editing previous commands using up/down arrows or Ctrl-R search. The history command lists

Uploaded by

roderickvicente
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

3-Configuring Bash Shell

Shell Variables
A variable is a name or identifier that can be assigned a value. The shell and other commands
read the values of these variables, which can result in altered behavior depending on the
contents (value) of the variable.

You assign a value to a variable by typing the name of the variable immediately followed by the
equal sign = character and then the value. For example:
Local and Environment Variables
A local variable is only available to the shell in which it was created. An environment variable is
available to the shell in which it was created, and it is passed into all other commands/programs
started by the shell.

Local variable declaration


Local and Environment Variables
An environment variable is available to the shell in which it was created, and it is passed into all
other commands/programs started by the shell.

Environment variable declaration


Displaying Variables
There are several ways to display the values of variables. To display only environment variables,
you can use several commands that provide nearly the same output:
Unsetting Variables
If you create a variable and then no longer want that variable to be defined, use the unset
command to delete it:
PATH Variable
The PATH variable is one of the most critical environment variables for the shell, so it is
important to understand the effect it has on how commands will be executed.
The PATH variable contains a list of directories that are used to search for commands entered by
the user. When the user types a command and then presses the Enter key, the PATH directories
are searched for an executable file that matches the command name. Processing works through
the list of directories from left to right; the first executable file that matches what is typed is the
command the shell will try to execute.
PATH Variable
Absolute Path
An absolute path specifies the location of a file or directory from the
top-level directory through all of the subdirectories to the file or
directory. Absolute paths always start with the / character
representing the root directory. For example, /usr/bin/ls is an
absolute path. It means the ls file, which is in the bin directory, which
is in the usr directory, which is in the / (root) directory. A file can be
executed using an absolute path like so:
Relative Path
A relative path specifies the location of a file or directory relative to the current
directory. For example, in the /home/sysadmin directory, a relative path of
test/newfile would actually refer to the /home/sysadmin/test/newfile file.
Relative paths never start with the / character.
Hands-On: Absolute and Relative paths
1. Open terminal and type echo “echo Welcome to Linux” > welcome.sh
2. Change the file to execution chmod u+x welcome.sh
3. Run the script using absolute path

4. Run the script using relative path


Adding Home Directory to PATH
Initialization Files
Modifying Initialization Files
Modifying global configuration requires administrative access to the system as
the files under the /etc directory can only be modified by an administrator. A
user can only modify the initialization files in their home directory.
Hands-On
Initialize cls command
per Bash shell using
.bashrc file
1. Open terminal
2. Type nano ~/.bashrc
3. Add the code
alias cls=“clear”

4. Open a new terminal


and test the cls
command
BASH Exit Scripts
Just as Bash executes one or more files upon starting up, it also may execute one or more files
upon exiting. As Bash exits, it will execute the ~/.bash_logout and /etc/bash_logout files, if they
exist. Typically, these files are used for "cleaning up" tactics as the user exits the shell. For
example, the default ~/.bash_logout executes the clear command to remove any text present in
the terminal screen.
Command History
There are several ways that this command history is advantageous to the user:
You can use the Up ↑ and Down ↓ Arrow Keys to review your history and select a previous
command to execute again.
You can select a previous command and modify it before executing it.
You can do a reverse search through history to find a previous command to select, edit, and
execute it. To start the search, press Ctrl+R and then begin typing a portion of a previous
command.
You execute a command again, based upon a number that is associated with the command.
Using the history Command
The history command can be used to re-execute previously executed commands. When used
with no arguments, the history command provides a list of previously executed commands:
Executing Previous Commands
The ! exclamation mark is a special character to the Bash shell to indicate the execution of a
command within the history list. There are many ways to use the ! exclamation character to re-
execute commands; for example, executing two exclamation characters will repeat the previous
command:
Clear History
To clear the entire contents of the history file,
execute history -c .
END OF PRESENTATION

You might also like