LinuxModule2 BashShell
LinuxModule2 BashShell
LinuxModule2 BashShell
2
Different Shells
• There are many different shells you could run into in the wild:
– sh - Bourne shell
– many others …
3
Starting the Login Shell
• When a new shell is started scripts run to setup the environment
4
Starting a Non-login Shell
• For a interactive non-login shell startup
– /etc/bashrc -- system wide functions and aliases
5
The source Command
Description:
Execs the commands within a file in the current process memory space.
Usage:
souce FILENAME
. FILENAME
Examples:
source /etc/bashrc
. test.sh
Additional Info:
The .(dot) is the same thing. This will NOT fork a new process to execute the file, so
if the file is a script that has an exit in it the shell will close.
6
Shell Variables
• Set by the startup scripts for the shell
7
Shell Variables continued
• Assigning variables
– Does not need the $ in the front
– No spaces in the command
– Need quotes around the text if spaces are included
– Backslash (\) is used to escape shell metacharacters
– Examples:
• PATH=$PATH:/sbin
• PS1=“[\u@\h \w]\$ “
• Declaring your own variables (current session)
8
The export Command
Description:
Adds a variable into the current session.
Usage:
export [OPTIONS] VARIABLE
Examples:
export OS export -p
export OS=“CentOS 6.3”
Additional Info:
If you have variables set that need to be accessed by a script that is running, you’ll
need to export those variables for the script to see them.
9
Important Shell Variables
• A partial list of the more important environment vars
Variable Description
$PATH The path used to search for any executable
$SHELL The current shell being used
$HOSTNAME Hostname for the system
$USER Current user
$HOME Current user’s home directory
$PS1 Prompt for the shell being used
$HISTFILE File all commands executed are saved to when
session is closed
$HISTFILESIZE Size of the history file
$HISTSIZE Size of the history buffer for the current session
10
Special Shell Variables
• There are a few special variables available with a bash shell:
– $$ - Current PID
11
The unset Command
Description:
Removes a variable from the current session.
Usage:
unset [OPTIONS] [VARIABLE]
Examples:
unset HISTFILE HISTSIZE HISTFILESIZE
unset OS
Additional Info:
The -f option is used to unset a function
12
History
• By default, bash will save all of the commands executed to a
session variable. Once the session is closed the contents are
copied to a $HISTFILE.
– Important variables: HISTFILE, HISTFILESIZE, HISTSIZE
– Use the <UP> and <DOWN> arrows to navigate through the session
history
13
The history Command
Description:
Displays a list of al lthe commands that have previously been executed by the
current user, not necessarily all from the current session.
Usage:
history [OPTION]
Examples:
history
history -c
Additional Info:
Using the UP and DOWN arrows will also navigate through previous commands.
PAGE UP and PAGE DOWN jumps multiple commands at a time.
14
Bang completion
• ! completion
– !## - Command ## from the history list will be executed again
15
Wildcards
• Wildcards can be used on the CLI to help search for files or
dirctories
– * - Represents any file or group of characters
ls -l /etc/cron.*
ls -l /var/log/messages?.tar.gz
ls -l /var/log/messages[123].tar.gz
16
Shell Substitution & Expansion
• The ~ (tilde)
– Represents your home directory
[root@localhost ~]# cd ~
[root@localhost ~]# cd ~?
17
Brace Expansion
• When characters or digits are placed inside of braces .. (dot dot)
between them, the shell will expand it and write all the characters
18
Communication
• In the shell environment there are 3 different file descriptors used
for communication with the terminal (tty)
– STDIN (0) - The standard input channel, default is the terminal
• Looking in /proc/$$/fd
lrwx------ 1 root root 64 Aug 1 15:41 0 -> /dev/pts/4
21
Pipelines
• Using the | (vertical bar) character allows for STDOUT to be
connected to STDIN of the next command
• Using |& allows for STDERR to be connected to the STDIN of the
next command
– This is just shorthand for 2>&1 |
• Use of multiple pipes in one command is allowed
• Some examples of using | and |&
22
Redirection
• < is used to redirect input for the command
mysql -u root < /home/mysqldump
23
Redirection continued
• Can also redirect by using #>&#, which will copy one of the pipes
to the same place as another
– 2>&1 - redirect STDERR to the same place as STDOUT
– Order is very important here
ls > dirlist 2>&1 (redirects both STDERR and STDOUT to dirlist)
ls 2>&1 > dirlist (redirects STDERR to terminal and STDOUT to dirlist)
• << is used for Here documents
24
The tee Command
Description:
Sends output to file and STDOUT.
Usage:
tee [OPTIONS] [FILE]
Examples:
cat sshd.service | tee sshd.bkup
Additional Info:
-a is used to append to a file instead of creating a new file.
If - is used instead of a fileneame it will just print to STDOUT twice.
25
The alias Command
Description:
Allows for a custom command to be created.
Usage:
alias [-p] NAME=VALUE
Examples:
alias ll=‘ls -l’
alias mydocs=‘cd $HOME/Documents’
Additional Info:
An alias created this way will only persist in the current session of the shell. Need to
append to .bashrc for persistence
The unalias command can be used to remove an alias from the session.
26
The diff Command
Description:
Displays the differences between 2 files.
Usage:
diff [OPTIONS] FILE1 FILE2
Examples:
diff myfile myfile.bkup
diff /var/log/messages /tmp/.o
Additional Info:
If both files are directories diff will compare all the filenames within the directories,
it does not compare the contents of the files.
27
The clear Command
Description:
Clears the terminal screen.
Usage:
clear
Examples:
clear
Additional Info:
The command history or buffered data is not removed, it simply slides the prompt
to the top of the screen.
28
The wc Command
Description:
Counts the characters, words and/or lines from STDIN or a file.
Usage:
wc [OPTIONS] [FILENAME]
Examples:
cat /etc/passwd | wc -l
wc -l < /var/log/messages
Additional Info:
Without a switch wc will list all 3 results in a single line.
If no filename is included it will wait for input from STDIN until CTRL+D is entered.
29