LinuxModule2 BashShell

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Bash Shell

2
Different Shells
• There are many different shells you could run into in the wild:
– sh - Bourne shell

– bash - Bourne again shell (our focus)

– ksh - korn shell

– zsh - z shell (newer shell)

– csh - c shell (based on c programming language)

– tcsh - c shell with command completion

– many others …

3
Starting the Login Shell
• When a new shell is started scripts run to setup the environment

• For an interactive login shell or non-interactive with --login, the


following scripts are run in order:
– /etc/profile -- system wide environment and startup programs

– ~/.bash_profile -- user specific environment


• Often calls ~/.bashrc

– ~/.bash_login -- user specific environment

– ~/.profile -- used by /bin/sh

4
Starting a Non-login Shell
• For a interactive non-login shell startup
– /etc/bashrc -- system wide functions and aliases

– /etc/profile.d/* -- scripts run by /etc/bashrc

– ~/.bashrc -- user specific environment info

• When logging out of the system


– ~/.bash_logout is run

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

• Use the set command to view all of the variables


– Includes global variables and functions as well

• Use the env command to view all variables

• To access a variable, place a $ in front of the name.


– Remember that case matters in *nix
• echo $PATH

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)

[mark@localhost etc]$ OS="CentOS 6.3“


[mark@localhost etc]$ export OS
[mark@localhost etc]$ echo $OS
CentOS 6.3

• Use echo to print the contents of a variable

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

– $? - Exit status of previous command

– $_ - The last item of the previous command string

[mark@localhost ~]$ ls /var/www


cgi-bin error html icons
[mark@localhost ~]$ echo $_
/var/www

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

– Default history file in bash is ~/.bash_history

– Default history file in csh is ~/.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

– !<str> - Searches for last instance of str and executes command

– !! - Run the last command again

– !$ - The last argument of the previous command (think $_)

– Any instance of ! in a normal command or string must be escaped


with “\”, otherwise it attempts to run !<str>
• echo -e “#\!/bin/bash \n echo $OS”

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.*

– ? - Represents 1 character in it’s position

ls -l /var/log/messages?.tar.gz

– […] - Allows for a range to be entered in its place

ls -l /var/log/messages[123].tar.gz

16
Shell Substitution & Expansion
• The ~ (tilde)
– Represents your home directory

[root@localhost ~]# cd ~

– Navigate to other’s home directory

[root@localhost ~]# cd ~mark

– Can be used to navigate to current directory

[root@localhost ~]# ls -al ~+

– Can be used to navigate to previous directory (not all systems)

[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

[mark@localhost etc]$ echo {1..12}


1 2 3 4 5 6 7 8 9 10 11 12
[mark@localhost etc]$ echo {a..f}
abcdef
[mark@localhost etc]$ echo {1,2{a..c},3{d..f}}
1 2a 2b 2c 3d 3e 3f
[mark@localhost etc]$ ls -al /var/log/{messages,maillog}
-rw-------. 1 root root 0 Dec 10 11:07 /var/log/maillog
-rw-------. 1 root root 252016 Dec 11 08:26 /var/log/messages

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

– STDOUT (1) - The standard output channel, default is the terminal

– STDERR (2) - Error messages channel, default is the terminal

• Looking in /proc/$$/fd
lrwx------ 1 root root 64 Aug 1 15:41 0 -> /dev/pts/4

lrwx------ 1 root root 64 Aug 1 15:41 1 -> /dev/pts/4

lrwx------ 1 root root 64 Aug 1 15:41 2 -> /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 |&

[root@fedora17 student]# ls -al /etc | grep ssh


drwxr-xr-x. 2 root root 4096 Dec 5 23:18 ssh
[root@fedora17 student]# grep student /var/log/secure | grep failed | more
[root@fedora17 student]# strace |& grep jynx.so

22
Redirection
• < is used to redirect input for the command
mysql -u root < /home/mysqldump

• > will redirect STDOUT, will create or overwrite a file


cat /etc/passwd | grep mark > ~/mypasswdentry

• >> again redirects STDOUT, but will append to a file


echo “toor:x:0:0::/root:/bin/bash” >> /etc/passwd

• 2> redirect STDERR only


find / -name \*.txt -uid 0 2> /dev/null

• &> redirects both STDERR and STOUT


strace ls /root &> ~/ls_strace

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

[root@fedora17 student]# cat > test <<- EOF


> sometext
> more
> EOF
[root@fedora17 student]# cat test
sometext
more

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

You might also like