Skip this Video
Download Presentation
BASH Shell Interactive Usage – Intermediate-Level Topics

Loading in 2 Seconds...

play fullscreen
1 / 71

BASH Shell Interactive Usage – Intermediate-Level Topics - PowerPoint PPT Presentation

  • Uploaded on

BASH Shell Interactive Usage – Intermediate-Level Topics. Presented by: Brian A. Seklecki < [email protected] > Brian A. Seklecki < [email protected] > Brian A. Seklecki < [email protected] >. Presentation Overview.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' BASH Shell Interactive Usage – Intermediate-Level Topics' - raanan

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

BASH Shell Interactive Usage – Intermediate-Level Topics

Presented by:

Brian A. Seklecki <[email protected]>

Brian A. Seklecki <[email protected]>

Brian A. Seklecki <[email protected]>


Presentation Overview

Goal: To improve day-to-day UNIX/POSIX-system proficiency by utilizing advanced of features of the BASH shell (*)

NOTE: This presentation will be limited to topics related to interactive shell usage (“at the command prompt”). Shell scripting can be covered by another presentation.

(*) Available on EIS-ITO GNU/Linux Systems and some AIX systems.


Presented in 4 Acts

  • Act I: Interactive Shell Features (History, Command editing, VI / EMACS Modes, Tab Completion, etc.)
  • Act II: Job Control and Process Management
  • Intermission
  • Act III: Customizing Bash Behavior
  • Act IV: Advanced Topics (and GNU/Linux-specific commands to save you time)

NOTE: This presentation will be limited to topics related to interactive shell usage (“at the command prompt”). Shell scripting can be covered by another presentation.


Interactive Presentation

  • Please have your laptop out and be logged into your assigned learning environment!
  • If you are logged into a Midrange environment system, please ensure that you are running bash by typing ‘bash’

Shell Prerequisite Knowledge

  • UNIX/POSIX Process Concepts
  • Variable Declaration
  • Standard UNIX I/O Concepts
    • stdin, stdout, stderr, etc.
    • Pipes & Redirection
  • File and Directory Concepts
  • Basic Shell Glob Expression Handling
  • Basic External Commands: grep, Awk, Sed
  • Basic VI and EMACS style editing

Introducing Bash

  • Bash is the standard shell for RedHat-based GNU/Linux systems
  • It implments a superset of the Bourne POSIX shell (sh)
  • Borrows features from sh, csh, tcsh, and ksh, zsh
  • Created by the Free Software Foundation licensed uner GNU Public License (GNU = GNU is Not UNIX)

What’s new in Bash

  • Many features from KSH
  • Powerful command line editing including tab completion
  • Simplified VIM and EMACS editing mode
  • Enhanced/Simplified History
  • Additional special variables and builtins (*) for simplified operation
  • Exceptional Documentation!

(*) Does everyone know what a ‘builtin’ is?


Act I: Interactive Shell Features

  • Tab Completion
  • History
  • Command Editing
  • A few cheap hacks to save you time!

Tab Completion

  • Tab completion is a quick way to avoid unnecessary ls(1) searching (for both files and commands) by allowing one to easily tab through a ever-narrowing list of commands based on:

1) $PATH variable contents or 2) A specific path

  • In BASH, tab completion is completed by two [TAB] keys in immediate succession
  • You can tab complete both your commands and file arguments to commands

Tab Completion (How it works)

  • Tab completion is an assisted search
  • Tab completion works by offering an array search result candidates based on the current input on the command line
  • The less-ambiguous of an argument you provide before tab-completion, the more-narrow your search results will be
  • If you already have a valid command (e.g., ls, cd, rm, etc.) entered, and optionally any arguments/switches, tab completion will assume that you want to tab complete a UNIX path as an argument.
    • Unless it is prefixed with ‘/’, ‘..’, or any other absolute/relative path prefix identifier, it will assume that you want to complete the path argument relative to “./” (aka, $PWD)

Tab Completion (Example 1)

Example 1: Completing from search $PATH variable

$ ls /usr/{,s}bin/*ipmi*

/usr/bin/ipmicmd /usr/bin/ipmilan /usr/bin/ipmish /usr/bin/ipmi_ui

/usr/bin/openipmicmd /usr/bin/openipmish

[email protected]:/home/bs827200$ [TAB-TAB]

Display all 2128 possibilities? (y or n) n

[email protected]:/home/bs827200$ open[TAB-TAB]

open openipmicmd openipmish openjade openssl openvt

[email protected]:/home/bs827200$ openipmi[TAB-TAB]

openipmicmd openipmish

[email protected]:/home/bs827200$ openipmish

> help

domain - Commands dealing with domains



Tab Completion (Example 2)

Example 2: Completing from absolute/relative path

$ ls /opt/fedex/swak/[TAB-TAB]

1.0/ 1.4/ 1.4.1/ 2.0-BETA2/ .ssh/

$ ls /opt/fedex/swak/1.[TAB-TAB]

1.0/ 1.4/ 1.4.1/

$ ls /opt/fedex/swak/1.4/



You Try It: Tab Completion

Example 3: Completing from absolute/relative path

$ ls /etc/s[TAB]



sasl2/ selinux/ setuptool.d/ shells snmp/

scrollkeeper.conf sensors.conf skel/ sound/ sysconfig/

NOTE: Append ‘sy’ manually to your command

$ ls /etc/sy[TAB]

NOTE: The command will change to

$ ls /etc/sys

$ ls /etc/sys[TAB]

sysconfig/ sysctl.conf syslog.conf

$ ls /etc/syscon[TAB]

NOTE: the command will change to (note the trailing slash indicating sub-dir entry):

$ ls /etc/sysconfig/

$ ls /etc/sysconfig/sys[TAB]

syslog system-config-netboot system-config-securitylevel system-config-users

$ ls /etc/sysconfig/system[TAB]

NOTE: The command will change to 

$ ls /etc/sysconfig/system-config-[TAB]

system-config-netboot system-config-securitylevel system-config-users

$ ls /etc/sysconfig/system-config-n[TAB]

NOTE: The command will change to 

$ ls /etc/sysconfig/system-config-netboot

-rw-r--r-- 1 root root 36 Apr 7 2008 /etc/sysconfig/system-config-netboot


History: Command Synopsis

Bash keeps track of your commands in a database file in your home directory:


Several ways to use history:

  • ‘history’ Command / Database File
  • The ‘fc’ mechanism
  • Previous command line editing (vi, emacs mode)

History: Command Synopsis (cont.)

Some important environmental variables related to history:

$ HISTFILE=/home/user/.bash_history

$ HISTFILESIZE=1000 - The number of commands in the history file

$ HISTSIZE=1000 – The number of commands in history memory

The ‘history’ command shows all of the contents of the history database in reverse order by default. You can grep/head/tail this command with a pipe.


  • history [-rh]


r – reverse listing

h –hide numbers

% ![historylinenumber] – allows one to re-execute a given command

% ![prefix] – allows one to locate a command beginning with prefix

% !?[string] – allows one to locate a previous command containing the string


History: Command (Contd.)

$ history 10

586 ps gauxww -u bs827200|head

587 ps gauxww -u bs827200|head -1

588 ps gauxww -u bs827200|head -5

589 ps gauxww -u 827200|head -5

590 top

591 top

592 top

593 cd

594 history --help

595 history 10

$ !590

[top is re-run]


History: Command (You Try It!)

  • Log into any Linux or AIX System where Bash is your default shell
    • If bash is not your default shell, try the command ‘chsh’
  • Start bash
  • Enter a 10-15 commands (ls /prod, ls /prod/sms, etc.)
  • Type history
  • Type !10 (example)

History: Edit a history entry with FC (Special)

$ export EDITIOR=vim


$ history 10

589 ps gauxww -u 827200|head -5

595 history 10

$ fc

$ ps gauxww –u bs827200|head -10 # modified


History: Command Line Editing

  • Use history to reduce keystrokes needed
  • Re-run the previous command with slight variations

Command Line Editing

Command line editing lets you use the up/down arrow keys to:

  • Interactively navigate the history file to retrieve recent commands.
  • Once you select a command, you can edit it using the key macros for your edit mode
  • To select VI vs EMACS edit mode, simply run either:
    • % set –o emacs
    • % set –o vi

Command Line Editing (EMACS)

bash provides a number of line editing commands; many are the same as emacs editing commands

  • M-b Move back one word
  • M-f Move forward one word
  • C-a Move to beginning of line
  • C-e Move to end of line
  • C-k Kill text from cursor to end of line
  • C-r Re-draw/refresh the screen
  • “M(meta)” in Bash with Putty default config is the “ESC” key

Command Line Editing (Vi/vim)

In Vi mode, bash provides a number of line editing commands; many are the same as vi(1)editing commands

  • w Move forward one word
  • B Move backward one word
  • 0 (zero) Move to beginning of line
  • $ Move to end of line
  • dw Delete word
  • cw Change word
  • d$ Delete to end of line

(*) See VIM documentation for more


VIM v.s. EMACS (The final Word)


Command Line Editing (YouTube link not visible from PNC Proxy)


History: Drill Down History Recall

  • To easily find a command outside of immediate history (that one would use keyboard history navigation to find)
  • Press Control+R (^R) at the shell and enter a substring text search query and Bash will search your memory and file history and bring it into the buffer
    • Optional: Once found, use your shell’s history navigation mode keys to edit before entering

Act II: Job Control

  • Job Control
  • A bit of advanced process management
  • A few cheap hacks to save you time

Interactive Job Control

  • All POSIX shells have basic interactive shell control
  • Jobs are localized / relative in the context of the current shell’s instance (different shell instances cant control another shell’s jobs)
  • Job controls allow a single TTY/Emulator session and single bash shell process run many commands
  • Analogous to multi-threaded window manager in a GUI In BASH, many improvements make the

Job Control and Process Mgmnt


  • Reduce the number of shells and terminals
  • Simplified Debugging Tasks

Job Control and Process Mgmnt

Job States:

  • Foreground (running, controls STDIN)
  • Background (running, STDIN returned to shell)
  • Suspended (Job process is paused, at prompt)

Process Scheduling and ControlJob/Process Control Command Overview

ps: Reports the process[es] status.

jobs: Lists the jobs that you are running in the background and in the foreground. If the prompt is returned with no information no jobs are present. Not supported by all shells. 

top: Display top CPU processes (external)

sleep: Waits or (sleeps) an x amount of seconds (external)

fg: Continues a stopped process by running it in the foreground. Not supported by all shells.

kill: Kills a process.

wait: Make process wait until completion of other process.


Jobs – command synopsis

jobs command shows a list of current jobs for current user in current shell in the following format.

Sample output:

Job no. Status command

[1]- Stopped man ps

[2]+ Stopped top

[3] Running sleep 120 &

jobs can show PIDs as well with ‘-l’ on Linux to avoid the need for ps(1) + grep(1)

$ jobs -l

[1] 29411 Stopped ssh qpswkd01

[2]- 29412 Stopped ssh qpswkd02


Background Jobs

  • Background processing
    • Use the & meta-character tailing argument to run programs in the background
    • May run more than one process in background
    • PID number is displayed for background process
    • Each background process is given a job number
    • Can execute other commands with existing shell while background program is running
    • STDOUT/STDERR can be redirected, otherwise it may pollute other jobs

Managing Jobs

  • A SIGINT (interrupt, pause) signal may be sent from the terminal to a foreground job using key sequence: CONTROL + Z
  • You may background the jobs (so that it will continue running) with the ‘bg’ shell builtin command
    • ‘bg’ takes an optional argument of Job ID
  • Use the ‘jobs’ command to view the child processes and jobs of the current shell
  • Once a job is backgrounded, it will change state to RUN; loosing control of STDIN but not STDOUT/STDERR
  • You may bring a single job back to foreground with ‘fg’

Background Jobs (example)

You may start a program directly into the background:

[1] 79116

[1] Running tail -F ITO_LOG &

[date] App starting at …


Managing Jobs (example)

linux$ sleep 90


[1]+ Stopped sleep 90

linux$ sleep 1000 &

[2] 29200

linux$ jobs

[1]+ Stopped sleep 90

[2]- Running sleep 1000 &

linux$ sleep 15 &

[3] 29203

linux$ fg 1

sleep 90

[3]- Done sleep 15

linux$ # NOTE: Job 1 completed, Job 3 Completed as well; Job 2 rages on

Luinux$ jobs

[2]+ Running sleep 1000 &


Managing Jobs (You Try It)

linux$ top &

[1]+ Stopped top

# Note: This will fail because ‘top’ cannot be run in the background

linux$ dd if=/dev/urandom > ~/ &

linux$ jobs

[1]+ Stopped top

[2]- Running dd if=/dev/urandom > ~/ &

linux$ fg 2

[no output]


linux$ jobs

[1]- Stopped top

[2]+ Stopped dd if=/dev/urandom > ~/

linux$ kill -9 %2

[2]+ Stopped dd if=/dev/urandom > ~/

[2]+ Killed dd if=/dev/urandom > ~/

# Cleanup here

linux$ du -h


linux$ rm !$




Job Control: Signal Review

  • Historic Note: SIGHUP (“hangup”) was historically used to restart mgetty(1) dialup modem TTY handler. E.g., drop the current dial in call session, respawn, and re-listen on the serial line for “RING”
  • When a SSH connection is reset to the client, the child Shell process of the forked SSHD process will receive SIGHUP, and so will all child processes that haven‘t forked from the current TTY

Managing Jobs (kill a process review)

  • Sometimes a user process becomes unresponsive or ghosted from the terminal (Lost session, etc.)
  • Kill the process with kill(1) command

Kill with an explicit Signal:

  • 1 - hang up
  • 15 - terminate (same as control-c ): default signal sent by kill without arguments
  • 9 - kill: will terminate processes that do not terminate with 15
  • Example:

% kill -9 35679 forces killing process 35679


Tangent: Nohup to avoid SIGTERM

  • nohup command
    • At logoff background processes would be terminated by cascading SIGHUP of parent process (SSH daemon instance, shell, etc.)
    • Use nohup command to keep background processes running, even after logout
    • If running more than one command must use in a shell script (ex: sleep 30; echo HI)
    • Just prefix the command with ‘nohup’

% nohup ./ [&]


Tangent: Controlling STDOUT with SIGSTOP / SIGCONT

  • In Bash, SIGSTOP is mapped to Control+Q [^Q] and SIGCONT is mapped to Control+S [^S]
  • You may halt the current forground process to examine (for example) verbose debug logging messages to STDOUT/STDERR
  • Example:

linux$ find /etc



[control+q] # Output stopped

[control+s] # Output resumed



Tangent: Using GNU screen to virtualize the terminal (contd.)

  • Screen can simplify long-running interactive jobs
  • Screen is useful for bypassing unstable / unreliable parts of the network (Wifi, VPN)
  • [Quick preview here]

More on Job Control

  • shell-job.ppt attached – Great visual demonstration

Act III: Customizing Bash

  • Login Scripts
  • Aliases
  • Prompts
  • Writeable Variabables
  • Shell Options
  • Editing Mode
  • Editors

Login Scripts

  • Use the login scripts to permanently customize your shell’s behavior / configuration
  • Use login scripts to declare reusable macros, aliases, and functions to save time
    • You don’t want to have to manually enter aliases, set environment variables, etc., each time you log in
  • All of these things can be done in a script that is run each time the shell is started

Shell Modes

Different scripts are sourced/read/executed based on the mode that the shell was started in

  • Interactive Shell: Started when a remote shell session is started via SSHD, Telnet, {m,}getty(1) + login(1) on the console or serial line
  • Non-interactive Shell: Started when a remote session is started (rcp, rexec, ssh, cron(8) job, sub-shell (bash ./, etc.))
  • HINT: If your default shell is not bash, and you want to spawn bash from sh/ksh in interactive mode, run:

lifesucks% bash --login


.bash_profile and .bashrc

Every time you log in to bash, the commands in ~/.bash_profile are run

  • you can put any common startup commands you want into this file
  • useful for setting up aliases and other settings for remote login

Every time you launch a non-login bash terminal, the commands in ~/.bashrc are run

  • useful for setting up persistent commands for local shell usage, or when launching multiple shells
  • often, .bash_profile is configured to also run .bashrc, but not always

Note: a dot (.) in front of a filename indicates a normally hidden file, use ls –a to see


Login Scripts (contd.)

  • For bash in interactive mode, order is . . .
    • /etc/profile
    • ~/.bash_profile
      • ~/.bash_login (if no .bash_profile)
      • ~/.profile (if neither are present)
    • ~/.bashrc
  • After logout . . .
    • ~/.bash_logout
  • For non-interactive shell:
    • ~/.bashrc

Example .bash_profile (partial)

% cat ~/.bash_profile

# include .bashrc if it exists

if [ -f ~/.bashrc ]; then

. ~/.bashrc


# Set variables for a warm fuzzy environment

export CVSROOT=~/.cvsroot

export EDITOR=vim

export PAGER=/usr/local/bin/less # AIX / Solaris

export http_proxy=


Example .bashrc (partial)

% .bashrc

  • # abbreviations for some common commands
  • alias f=finger
  • alias j=jobs
  • alias l=\'ls -lF\'
  • alias la=\'ls -alF\'
  • alias ls=\'ls -F\'
  • ssh=“/usr/bin/ssh –v”
  • dev=“ssh dpPRJa08” # Compound

Change your shell with chsh(1)

Linux $ chsh --list-shells







$ chsh -s /bin/bash

Changing shell for bs827200.

Password: […]

$ finger bs827200 | grep -B1 Shell

Login: bs827200 Name: Brian A. Seklecki

Directory: /home/bs827200 Shell: /bin/bash



Alias allows you to create a new canonical name for an existing command (i.e. a shortcut)

  • alias [newcommand[=command]]
  • unalias [-a] [command]

where –a means remove all aliases and

command is a specific alias to remove

  • Command line arguments ($*) to the alias will be passed to the resulting command
  • Examples:

alias ls=“/bin/ls –alF” # Need absolute path to avoid recursion

alias mx="nslookup -query-type=MX"



Create macros that can be called as commands

function getNumberOfJobs {

wcOut=$(jobs | wc -l | sed -e \'s/[[:space:]]//g\')

echo -n $wcOut


$ echo "Number of background jobs: [$(getNumberOfJobs)]"

Number of background jobs: [0]


Customizing Bash Prompt

We can display the date, time, command #, .. in prompt by setting the PS1, PS2, and PS3 variables.

  • “\d “  \d displays date
  • “\t “  \t displays time
  • “\s “  \s name of shell
  • “\! “  command number
  • “\@”  time
  • “\h”  hostname

Customizing Bash (Examples)


[[email protected] ~]$ export PS1=\'[\d @ \@] \[email protected]\h:$PWD\$ ‘

[Mon Nov 26 @ 09:40 AM] [email protected]:/home/bs827200$

[[email protected] ~]$ export PS2=‘more please> ‘

[[email protected] ~]$ for varName in foo bar biz; do

more please> echo $varName; done




HINT: This syntax can be useful for when copy/pasting the results of commands into trouble tricket


Environment and Configuration Variables

Guide to UNIX Using Linux, Third Edition



Useful Writeable and Read-Only Variables

Two types of shell variables are shell environments variables (writable or read-only) and user-defined variables.


Useful Writeable and Read-Only

Variables (Example $? Variable)

  • The $? variable returns the exit/return value from the last command executed. It will contain a high value (12x) if the shell couldn’t successfully execute the last command (command not found, etc.)

% grep -i Brian /etc/passwd 2>&1 > /dev/null

% echo $?


% grep -i Brian /etc/profile 2>&1 > /dev/null

% echo $?


% fooBarBiz 2>&1 > /dev/null

% echo $?



Useful Writeable and Read-Only

Variables (Example !$ Variable)

  • The !$ variable contains the last argument to the last command previously run. It can be useful for passing a single argument to multiple commands in succession

$ ls -al test

-rw------- 1 lavalamp lavalamp 571 Sep 23 2011 test

$ sha256 !$

SHA256 (test) = c3dbc1e98db5ed3ee41dbcdfa24

$ tar czvf !$.tar !$

tar czvf test.tar test

a test


Shell Options

The shell has dozens of boolean tunable options that affect interactive and non-interactive use:


Shell Options (shop builtin synopsis)

shopt Shell Options

Syntax shopt [-pqsu] [-o] [optname ...]


-s Enable (set) each optname

-u Disable (unset) each optname.

-p Display a list of all settable options, with an indication of

whether or not each is set. The output is displayed in a form

that may be reused as input. (-p is the default action)

-q Suppresses normal output; the return status indicates whether the optname

is set or unset. If multiple optname arguments are given with `-q\',

the return status is zero if all optnames are enabled; non-zero otherwise.

-o Restricts the values of optname to be those defined for the `-o\' option to

the set builtin.

If either `-s\' or `-u\' is used with no optname arguments, the display is limited to

those options which are set or unset, respectively. Unless otherwise noted, the shopt

options are disabled (off) by default.

The return status is zero unless an optname was *not* enabled or was not a valid shell option.


If this is set, an argument to the cd builtin command that

is not a directory is assumed to be the name of a variable whose value is

the directory to change to.



Shell Options (set command)

The shell has man boolean tunable options that affect interactive and non-interactive use:


Customize your Bash Profile

$ vim ~/.bash_profile

# Variables

# Set Options (set –x, shopt –y)

# Aliases

# Function Macros


Next Steps!

  • Change your login shell to Bash wherever you go (‘chsh’)
  • Customize the hell out of your Bash shell!
  • Start to study non-interactive bash shell usage (scripting)