43

The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Embed Size (px)

Citation preview

Page 1: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and
Page 2: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

The Bash Shell• Why use the Bash Shell and the CLI?

– 1. Control – the command line permits users to include options that provide both flexibility and power over the defaults when issuing commands by GUI.

– 2. Speed – many programs issued at the command line are text-based and therefore will start faster and run faster than GUI-based programs.

– 3. Resources – as with speed, a text-based program uses fewer system resources.

– 4. Remote access – if you are using a program like ssh to reach a computer, you may be limited to a text–based interface, so learning it is important and once learned, you gain the other benefits.

– 5. Reduced health risk – yes, this one sounds odd, but mouse usage is one of the most harmful aspects of using a computer as it strains numerous muscles in your hand and wrist, while typing is less of a strain (although poor hand placement can also damage your wrist).

Page 3: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

The Command Line Prompt

• You can tailor your prompt but by default, it will look like this:

– [foxr@localhost ~]$

• What do these parts mean?– Username– Hostname– Current directory• ~ means “user’s home directory”

– $ the user prompt (# for root)

Page 4: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Entering Commands• Type the name of your command and press <enter>• Some commands do not require additional

information, so the command is just the name of the command itself (which is also a program’s name)– The following instructions output information similar to

what we find from our prompt• whoami – output the user’s username • pwd – output the current working directory • hostname – output the name of the host (this will either be

localhost.localdomain or the IP address or IP alias of the machine logged in to)

• ls – list the contents of the current working directory

Page 5: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Other Commands• passwd – used to change your password

– you are first prompted to input your current password and then your new password (twice). If the password you enter is not sufficiently strong, you are warned

• uname – output basic information about your operating system• arch – output basic information about your computer’s hardware• who – list the users currently logged in to the computer you are

operating on, including their terminal – which terminal window they have opened or whether they are on console)

• bash – start a new Bash shell (if in a shell, this starts a new session so that the outer shell session is hidden)– sh starts a new shell in the default shell type which may not be Bash

• exit – leave the current Bash shell and if this is the outermost Bash shell, close the window

Page 6: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Example Session

[foxr@localhost ~]$ uname Linux [foxr@localhost ~]$ arch x86_64 [foxr@localhost ~]$ who foxr tty7 2013-10-11 09:42 (:0) foxr pts/0 2013-10-11 15:14 (:0) [foxr@localhost ~]$ uname; arch; who Linux x86_64 foxr tty7 2013-10-11 09:42 (:0) foxr pts/0 2013-10-11 15:14 (:0)

prompt user input program output

Page 7: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Commands with Options/Parameters

• Most Linux commands allow or require additional information– Options – user-specified variations in how the

command should be performed– Parameters – files/directories that the command

should operate on

• The typical format for a command is– command [option(s)] [parameter(s)]

• The [ ] indicate “optional”

Page 8: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

The ls Command• The ls (list) command lists the contents of a

directory or information about directories and files– examples

• ls – list all contents• ls –l – perform a long listing (-l is lower case L)• ls –l file1.txt – perform long listing on file1.txt• ls –l file1.txt file2.txt file3.txt – perform long listing on

several files• ls –l *.txt – perform long listing on all items in this directory

that end with .txt• ls –a – show all files including “hidden” files• ls –al (also ls –la and ls –a –l) – both ls –a and ls –l

combined

Page 9: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

The ls Command: Long Listings• A long listing will display for each item

– the type of object (file, directory, symbolic link, etc where a hyphen means a regular file)

– the permissions of the object (who can read, write and execute it, which appear using r, w, and x, with a hyphen indicating no permission, we examine permissions in detail in chapter 3)

– the number of names linked to the object – the user owner and group owner of the object – the size of the object– the last modification (or creation) date and time of the object – the name of the object (and if a symbolic link, the item it is

linked to)

Page 10: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

The ls Command: Long ListingsExample long listing of a part of a directory, all of these are files with the same permissions, number of hard links, owner, group, creation date

Page 11: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

The ls Command: Other OptionsOption Meaning

-A Same as –a exact that . and .. are not shown-B Ignore items whose names end with ~ (~ is used for backup files)

-C List entries in columns (fits more items on the screen)-d List directories by name, do not list their content -F Append listings with item classifications (ends directory names with /, ends executable files with *)

-g Same as –l except that owner is not shown-G Same as –l except that group owner is not shown-h When used with –l, modifies file sizes to be “human readable”

-i Include inode number of items (inodes are discussed in chapter 10)

-L Dereference links – that is, display information about item being linked to, not the link itself (links are discussed in chapters 3 and 10)

-r List items in reverse alphabetical order-R Recursive listing (list contents of all subdirectories)-s When used with –l, outputs sizes in blocks rather than bytes-S Sort files by size rather than name-t Sort files by modification time rather than name-X Sort files by extension name rather than name-1 (numeric 1) List files one per line (do not use columns)

Page 12: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

man Pages

• man – command to list a command’s man page (manual)– gives information about the command

Page 13: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

man Pages: Types of InformationEntry Meaning

Name Command name with one line descriptionSynopsis One or more entries that show the syntax of the command’s usage

Description Detailed description of what the command doesOptions Description of every available option, including additional parameters expected

by options, and options that options can have (options may be listed under description)

Arguments/Parameters Additional parameters expected or allowedEnvironment Environment variables that this command might useBugs Known errors or fixesNotes Additional information that might be usefulFiles Files that this command might accessExit or return values Error or exit codesExamples In rare cases, some man pages include examples of how to use the command

Functions Names of functions that can be called by the commandAuthor(s) Person/people who wrote the command’s programming code

See also Other Linux instructions related to this one or worth exploring

Page 14: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

man Pages: Moving Through Them• forward one screen – ‘f’, ‘z’, space bar• forward half a screen – ‘d’• forward one line – ‘e’, ‘j’, enter key, down arrow• back one screen – ‘b’, ‘w’ • back one half screen – ‘u’ • back one line – ‘b’, up arrow• return to top of man page – ‘g’• go to bottom of man page – ‘G’• go to line # - #G (# is a number like 10G for line 10)• obtain help – ‘h’, ‘H’ • to move forward # lines - # (# is a number)• to search forward for a string - /string <enter>• to search backward for a string - ?string <enter>• exit man page – ‘q’, ‘Q’

Page 15: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

The apropos Command• Used to list all commands whose description

matches a given string– the description is taken from all of the man pages– format: apropos string or apropos “string”

• if you use “” then the string must match exactly whereas if you don’t and the string is multiple words, then apropos matches descriptions that contain any of the words

– example: apropos virtual memory lists over 100 instructions while apropos “virtual memory” lists• mremap (2) - re-map a virtual memory address• vfork (3p) - create a new process; share virtual memory• vmstat (8) - Report virtual memory statistics

Page 16: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: History• Every instruction entered is stored in a history

list• You can recall instructions from the history list

to save time from re-typing a previous instruction

• To obtain your history, use the history command– history – lists the previous n instructions where n

is a default value– history n – list the last n instructions in the history

list

Page 17: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: History

• Given the history list on the right, how do we recall instructions?– control+p or up arrow to backward through

the history list– control+n or down arrow to move forward

through the history list– !! – re-execute last instructino– !# - re-execute instruction # (e.g., !10 to

repeat cat script1)– !str – re-execute most recent instruction

that starts with the string str (!c re-executes 10 while !cd re-executes 8)

1 ls2 cd ~3 pwd4 man cd5 ls –l6 cd /bin7 ls –l | grep root8 cd ~9 vi script110cat script111 rm script112history

Page 18: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: History

• Other history options aside from n• -c – clear the history list• -d # – where # is a number, delete that numbered

entry from the history list• -w – write the current history list to the history

file• -r – input the history file and use it instead of the

current history• # – where # is a number, display only the last #

items in the list

Page 19: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Variables• A variable is a name given to a storage

location– In Bash, variables by default store strings but can

also store integer numbers (no decimal point) or arrays of strings/integers

• Three types of variables– Normal variables are defined from the command

line and available in your current session– Script variables which are known only in the script– Environment variables are defined in a script but

exported to be known in your environment

Page 20: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Variables• You assign variables their values in an

assignment statement– var=value• var is the name of the variable which can consist of

letters, digits and underscores (_) as long as the name does not start with a digit• value is a literal value, the output returned from a Linux

program, the value stored in another variable, or the result of some operation on the value stored in a variable or any combination of these• values which contain blank spaces must be entirely

enclosed within quote marks (“” or ‘’ although we prefer “”)

Page 21: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Environment Variables• These variables are pre-defined at the time the

user logs in and opens a shell

Variable Meaning (value)DESKTOP_SESSION GUI being used (Gnome, KDE)

DISPLAY The terminal window being used, denoted as :0.0 if you are logged in through a GUIHISTSIZE Number of entries retained in the history listHOME User’s home directory nameHOSTNAME Name of computerLOGNAME User’s user nameMAIL Location of user’s email directoryOLDPWD Last directory visited before current directory (see PWD)PATH The list of directories that the interpreter checks with each commandPS1 Specifies the user’s prompt (explained in more detail in table 2.5)PWD The current directorySHELL Name of the program being used as the user’s shell (for Bash, this is usually /bin/bash)

TERM The terminal type being usedUSER The user’s usernameUSERNAME The user’s name (if provided)

Page 22: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: PATH Variable

• Whenever you type in a command, the interpreter must locate the executable program of that command–Many are stored in /bin and /usr/bin

• To prevent the user from having to type in the full path, such as /bin/ls or /usr/bin/apropos, we use a variable called PATH– This variable stores directories that the interpreter

will test in searching for the program

Page 23: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: PS1 Variable

• PS1 encodes the information that is displayed in your prompt– for instance: [\u@\h \d] $ is your current

PS1Character Meaning

\u User name\h Host name\w Current working directory\! Number of current command (as it will appear in the history list)\t Current time\d Date (as in Mon Aug 13)\$? Status of last command\a Bell character (makes a tone)\j Number of jobs in the current shell (we explore jobs in chapter 4)\n New line[`command`] Output of the Linux command

Page 24: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: echo

• The echo command is an output statement to output the value stored in a variable– Format: echo string– If the string is to include variable names, put a $

before each variable name as in $var to output the value stored in the variable

– If name=Richard then• echo $name outputs Richard while echo name outputs

name

Page 25: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: echo

• echo has a few options of note– -n – do not output a new line character at the end

of the output so that the next echo statement continues on the same line

– -e – enable \ escapes (see table below)– -E – disable \ escapes

Escape Character Meaning\\ Output a \\b Backspace (back cursor up 1

position)\n Newline – start a new line\t Tab\! \$ \& \; \' \" !, $, &, ;, ' and " respectively

Page 26: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: alias

• In Linux, an alias is a name applied to a command• You define aliases– to reduce the amount of typing– so that complicated instructions do not have to be

remembered– so that common typos made by the user can be

interpreted correctly– so that dangerous commands are made safer

• Format: alias name=command– If the command includes spaces, enclose the entire

command in ‘’

Page 27: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: alias

• Examples– alias ..='cd ..' less typing– alias lss=less common typo– alias sl=ls common typo– alias mountcd='mount /dev/cdrom /cd iso9660

ro,user,noauto' long or hard to remember– alias rm='rm –i' safety

• To remove an alias, type unalias name – as in unalias sl

• To see all defined aliases, type alias by itself

Page 28: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Feature: Command Line Editing

• Bash has a number of useful keystrokes that save you from excessive typing on the CLI

• These are combinations of control or escape characters– control+key means hold control and press key– escape+key means press escape and then press key

• These sequences are based on the emacs editor (see chapter 5)

• Learning these can save you a lot of time and effort!

Page 29: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Command Line EditingKeystroke Meaning

ctrl+a Move cursor to beginning of linectrl+e Move cursor to end of linectrl+n (also up arrow) Move to next instruction in history listctrl+p (also down arrow) Move to previous instruction in history listctrl+f (also right arrow) Move cursor one character to the rightctrl+b (also left arrow) Move cursor one character to the leftctrl+d (also delete key) Delete character at cursorctrl+k Delete all characters from cursor to end of linectrl+u Delete everything from the command linectrl+w Delete all characters from front of word to cursorctrl+y Yank, return all deleted characters (aside from c+d) to cursor

positionctrl+_ Undo last keystrokeesc+f Move cursor to space after current wordesc+b Move cursor to beginning of current wordesc+d Delete the remainder of the word

Page 30: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Redirection• By default, Linux programs obtain input from STDIN

(standard input) and send output to STDOUT (standard output)

• You can alter this through redirection– > - redirect output from terminal window to file (if file already

exists, it is deleted and a new one created)– >> - redirect output from terminal window and append to file– < - redirect input from keyboard to file (nearly all Linux

instructions receive input from file so this is not usually necessary)

– << - redirect input from file to keyboard– | - pipe, or redirect output to be input to another Linux

instruction

Page 31: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Redirection• For instance, we want to combine several files

using the cat (concatenate) command– cat file1.txt file2.txt file3.txt > joined_files.txt

• Without the redirection, the combined files are displayed to the screen

Page 32: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Redirection

• Here we redirect the output of the ls command of several users’ home directories to the file user_entries.txt– At first, we create the file anew and then we

append the remainder of the commands to the same file• ls /home/dukeg > user_entries.txt• ls /home/marst >> user_entries.txt• ls /home/underwoodi >> user_entries.txt• ls /home/underwoodr >> user_entries.txt• ls /home/zappaf >> user_entries.txt

Page 33: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Redirection

• For input from keyboard, we use << as in– cat <<

• now whatever we type from keyboard is echoed to the terminal window

• but this includes <enter> so how do we end input? Use control+d

• We can also use cat << string– End input once you type string<enter>

• We might combine << and > to create a file of text we enter by the command line– cat << string > file.txt

Page 34: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Redirection• The pipe is very useful to take output from one

program and feed it as the input to another– ls | less

• Display the contents of the current directory but use the less program to display it– less displays 1 screen at a time and lets you scroll forward

and backward

• cat somefile | less– Again, display the content 1 screen at a time

• cat << string | sort > file.txt– like the previous example to create a file from keyboard

input but this time the input is sorted before being stored to file

Page 35: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Wildcards• The wildcard is a character used to match

against filenames (and directory names)• The simplest wildcard to understand is *

which means “match anything”– ls *.txt means list all files whose name ends in .txt– ls * matches all items in the current directory– ls a* matches all items that start with an a

• To other useful wildcards are ? (match any one character) and [ ] (match any one character in the brackets)–We will explore these in more detail in chapter 3

Page 36: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Bash Features: Tab Completion• The Bash interpreter can complete directory

and file names for you– Type a portion of the name that is unique, press

<tab>, Bash completes it– If the portion is not unique, Bash emits a beep– Press <tab><tab> and Bash displays all

directories/files that match• if there are too many, you are told this in a message like

Display all 153 possibilities? (y or n)

– Tab completion is very handy (if you remember to use it!)

Page 37: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Special Files• There are several files that define initial aliases,

functions and variables – these allow you to tailor your shell as you like

• These are script files, automatically executed when you first log in (/etc/profile, ~/.profile) or open a (new) Bash shell (/etc/bashrc, ~/.bashrc, ~/.bash_profile)– Some are located in /etc and can be modified by the system

administrator– Some are located in the user’s home directory for the user

to modify

• If you modify a file, to have it take effect, use source as in source .bashrc

Page 38: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Other Shells: csh

• The C shell uses syntax similar to the C programming language– Introduced numerous features now found in Bash• History• Command line editing• Aliases• Directory stack (see chapter 3)• ~• Escape completion (instead of tab completion)• Job control (see chapter 4)

Page 39: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Other Shells: TC Shell

• Updated version of C shell, T for TENEX operating system, or tcsh

• Updated history commands

Keystroke Meaning!! Execute previous command!n Execute command n on the history list!string Execute the most recent command that starts with the characters string!?string Execute the most recent command that contains the characters stringcommand !* Execute command but use all arguments from the previous commandcommand !$ Execute command but use the last argument from the previous commandcommand !^ Execute command but use the first argument from the previous commandcommand !:n Execute command but use the nth argument from the previous commandcommand !:m-n Execute command but use arguments m-n from the previous command

Page 40: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Other Shells

• KornShell (ksh) released in 1983– Includes WYSIWYG command line editing

• Almquist shell (ash) released in 1989– Scaled down and faster version of Bourne shell

(sh)

• Z Shell (zsh) released in 1990 adds spell checking to features found in sh and ksh

Page 41: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

Comparing Bash and TC Shell

Feature Bash TC ShellUser prompt $ % Assignment statement format

var=value set var=value

Export statement export var=value setenv var valueAlias definition format alias term=command alias term commandDefault user definition file

.bashrc .cshrc

Automated spelling correction

no yes

Page 42: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

The Bash Interpreter• Upon entering a command, the Bash interpreter

takes over performing the following steps in this order– The input is broken into individual tokens

• a token is a known symbol (e.g., <<, |, ~, *, !, etc) or• a word separated by spaces where the word is a command or• a word separated by spaces where the word is a filename,

directory name or path or• an option combining a hyphen (usually) with one or more

characters (e.g., -l, -al).

– If the instruction has any quotes, those quotes are handled.

– If an alias is found, that alias is replaced by the right-hand side of the definition.

Page 43: The Bash Shell Why use the Bash Shell and the CLI? – 1. Control – the command line permits users to include options that provide both flexibility and

The Bash Interpreter– The various words and operators and now broken up into

individual commands (if there are multiple instructions separated by semicolons).

– Brace expansion unfolds into individually listed items. – ~ is replaced by the appropriate home directory.– Variables are replaced by their values.– If any of the commands appears either in ` ` or $( ), execute

the command (we explore this in chapter 7).– Arithmetic operations (if any) are executed.– Redirections (including pipes) are performed.– Wildcards are replaced by a list of matching file/directory

names.– The command is executed and, upon completion, the exit

status of the command (if necessary) is displayed to the terminal window (if no output redirection was called for).