L2v2 Chapter 01 PowerPoint
L2v2 Chapter 01 PowerPoint
L2v2 Chapter 01 PowerPoint
● Many of the topics that will be covered in this unit were also
covered in the previous course for LPIC-101 exam preparation
because these topics are testable on both LPIC exams.
Shell Variables
Shell Variables
● A variable is a name or identifier that can be assigned a value.
● The shell and other programs can read the values of the variables
A=1 1=a
_1=a A-1=3
LONG_VARIABLE=’O K’ LONG-VARIABLE=‘WRONG’
● Local variables can only be used by the shell and use lowercase
names by convention.
● If the shell option nounset is enabled with the set -o nounset command,
then referring to an unset variable will result in an unbound variable error:
sysadmin@localhost:~$ set -o nounset
sysadmin@localhost:~$ echo $variable_1 -bash: variable_1:
unbound variable
sysadmin@localhost:~$ set +o nounset
sysadmin@localhost:~$ echo $variable_1
The PATH Variable
The PATH Variable
● Provides a colon : separated list of directories to search to execute
commands given without an explicit path.
● Directories are processed from left to right, and first directory found
which contains the command to execute will be used and any others
remaining to the right will be ignored.
o an alias
o a function
/usr/bin Contains the majority of the commands that are available for regular
users to execute.
/usr/local/bin Normally empty, but may have commands that have been compiled
from local sources.
/usr/local/sbin Normally empty, but may have administrative commands that have
been compiled from local sources.
/home/jamie/bin A directory for the current user (jamie) to place programs. Typically
used by users who create their own scripts. *Note that is usually not
recommended to use a first name for a username. Convention is to use
last name and first initial (i.e., ramirezj for the user Jamie Ramirez).
Executing Outside the PATH
o The PATH variable can be set to include the directory where the command is
located. (Administrative privilege required)
o The command can be copied to a directory that is listed in the PATH variable.
(Administrative privilege required)
Path Review
smithj@localhost:~$ /home/smithj/my.sh
Today is 04/22/25
Hello josietech
smithj@localhost:~$ ./my.sh
Today is 04/22/25
Hello josietech
smithj@localhost:~$
PATH Scenario
● The directory /home/smithj could be added to the PATH variable to
allow the script to execute:
smithj@localhost:~$ echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/bin:/bin/:/usr/bin:/usr/local/sbin:/usr/sbin:
/sbin:/home/smithj/bin
smithj@localhost:~$ PATH=$PATH:/home/smithj
smithj@localhost:~$ echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/bin:/bin/:/usr/bin:/usr/local/sbin:/usr/sbin:
/sbin:/home/smithj/bin:/home/smithj
smithj@localhost:~$ ./my.sh
Today is 04/22/25
Hello josietech
smithj@localhost:~$
● The best way to avoid naming your script the same as an existing
command is to use the type command:
smithj@localhost:~$ type echo
echo is a shell builtin
smithj@localhost:~$ type hello
hello is a function
hello ()
{
echo hello
}
smithj@localhost:~$ type ls
ls is aliased to `ls –color=auto’
smithj@localhost:~$ type cal
cal is /usr/bin/cal
smithj@localhost:~$ type junk
-bash: type: junk: not found
The PS1 variable
● The PS1 variable determines the bash shell prompt:
sysadmin@localhost:~$ echo $PS1
[\u@\h \W]\$
sysadmin@localhost:~$ PS1=’\$ ‘
$ _
● Several variables affect the way history is stored for the bash shell:
Variable Effect
HISTFILESIZE Number of history entries to store in history file.
HISTFILE Name of history file if different from ~/.bash_history.
● For example:
o HISTIGNORE='ls*:cd*:history*:exit’
o The above means do not store any command that starts with ls, cd, or
history, as well as the exit command.
Setting Variables
sysadmin@localhost:~$ alias
alias l.=‘ls –d .* --color=auto’
alias ll=‘ls –l –color=auto’
alias ls=‘ls –color=auto’
alias vi=‘vim’
alias which=‘alias | /usr/bin/which --tty-only –read-alias –show-dot –show-tilde’
sysadmin@localhost:~$
The alias Command
● To define a new alias use the syntax:
alias new_alias=‘command –options… arguments…’
● Aliases defined from the command line will not persist after the user
exits.
sysadmin@localhost:/usr/share/doc$ report() {
> /usr/share/doc
> echo “Document directory usage report” > /tmp/report
> date >> /tmp/report
> pwd >> /tmp/report
> du -sh . >> /tmp/report
> }
sysadmin@localhost:/usr/share/doc$ report
Function Scenario
● To make a persistent function, add it to a shell initialization file like
~/.bashrc:
sysadmin@localhost:~$ cat .bashrc
# .bashrc
HISTCONTROL=‘ignorespace:erasedups’
# Source global definitions
if [ -f /etc/bashrc ]; then
./etc/bashrc
fi
# User specific aliases and functions
alias grep=‘grep --color’
report() {
cd /usr/share/doc
echo “Document directory usage report” > /tmp/report
date >> /tmp/report
pwd >> /tmp/report
du –sh . >> /tmp/report
}
Function Arguments
& Each command within the list is executed asynchronously within a subshell, or in the
background. The shell does not wait for the commands to terminate and returns an exit stats
of zero.
&& This is an “AND” list, so if the command on the left side of && executes successfully, then the
command on the right side of && will execute. The exit status of the list is based upon the exit
status of the last command that is executed.
|| This is an “OR” list, so if the command on the left side of || does not execute successfully,
then the command on the right side of || is executed. The exit status of the list is based upon
the exit status of the last command that is executed.
Sequential Lists
sysadmin@localhost:/usr/share/doc$ date;who;uptime
Tue Apr 22 07:39:50 PDT 2025
student tty1 2025-04-22 02:50 (:0)
student pts/0 2025-04-22 02:50 (:0)
student pts/1 2025-04-22 02:50 (:0.0)
07:39:50 up 4:51, 3 users, load average: 0.00, 0.00, 0.00
Background Lists
root@localhost:~$ updatedb&makewhatis&
[1] 4326
[2] 4327
Background Lists
● Commands that may run for a long time are especially appropriate
for background lists.
● The job number and PID can be used to send signals to control the
command while it runs by commands such as kill.
AND Lists
sysadmin@localhost:~$ grep –q alice /etc/passwd && echo alice has account || echo alice
has no account
alice has no account
sysadmin@localhost:~$ grep –q joe /etc/passwd && echo joe has account || echo joe has no
account
joe has account
Initialization Files
Initialization Files
● Initialization files are used to set the initial state of the shell's
variables, aliases, and functions.
~/.bash_profile Each user has their own .bash_profile file in their home
directory. The purpose for this file is the same as the
/etc/profile file, but having this file allows a user to
customize the shell to their own tastes. Normally used to
create customized environment variables.
~./bashrc Each user has their own .bashrc file in their home directory.
The purpose for this file is to generate things that need to be
created for each shell, such as local variables and aliases.
/etc/bashrc This file may affect every user on the system. Only
administrators can modify this file. Like the .bashrc file, the
purpose for this file is to generate things that need to be
created for each shell, such as local variables and aliases.
Modifying Initialization Files
● Global initialization files are stored in the /etc/ directory and require
administrator privileges to modify.
● Local initialization files are stored in the user's home directory ~, so a user
may modify their own initialization files.
● Sourcing an initialization file executes the file in the context of the user's
shell, and is a good way of testing.
● If the .bash_logout file is located in the user's home directory, then it will
be executed upon logout