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.

Slides:



Advertisements
Similar presentations
RH030 Linux Computing Essentials
Advertisements

Tony Kombol.  A program that  receives commands from a text input device (e.g. keyboard)  passes them to the operating system to perform  In the.
Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
Chapter One The Essence of UNIX.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
The Linux Shell The shell contains
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
Linux+ Guide to Linux Certification, Second Edition
Linux+ Guide to Linux Certification, Second Edition
More Shell Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide to Linux Installation and Administration, 2e1 Chapter 6 Using the Shell and Text Files.
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Shell Script Examples.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Using the “CLI” Unix / Linux Preparation Course May 25 Djibouti.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter 9 Part II Linux Command Line Access to Linux Authenticated login using a Linux account is required to access a Linux system. The Linux prompt will.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
Introduction to Shell Script Programming
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
Linux+ Guide to Linux Certification, Third Edition
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Week 3 Exploring Linux Filesystems. Objectives  Understand and navigate the Linux directory structure using relative and absolute pathnames  Describe.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
Using Commands Unix / Linux Preparation Course May 6, 2012 Serrekunda, The Gambia.
Chapter Three The UNIX Editors.
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Introduction to Programming Using C An Introduction to Operating Systems.
Chapter Six Introduction to Shell Script Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Using the “CLI” Unix / Linux Preparation Course June 9, 2013 Lusaka, Zambia.
CSCI 330 UNIX and Network Programming Unit III Shell, Part 1.
Agenda The Bourne Shell – Part II Special Characters Ambiguous File Reference Variable Names and Values User Created Variables Read-only Variables (Positional.
Unix Advanced Shells Chapter 10. Unix Shells u Command Line Interpreter –once logged in, login gives control to a shell –it prompts for input, then parses,
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Linux+ Guide to Linux Certification, Second Edition
1 CS3695 – Network Vulnerability Assessment & Risk Mitigation – Introduction to Unix & Linux.
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Learning Unix/Linux Based on slides from: Eric Bishop.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Lesson 5-Exploring Utilities
SUSE Linux Enterprise Desktop Administration
Shell Features CSCI N321 – System and Network Administration
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
SQL and SQL*Plus Interaction
Unix Shells.
Some Linux Commands.
The Linux Operating System
Shell Environments.
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
A shell is a user interface.
Presentation transcript:

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

The Command Line Prompt You can tailor your prompt but by default, it will look like this: – ~]$ What do these parts mean? – Username – Hostname – Current directory ~ means “user’s home directory” – $ the user prompt (# for root)

Entering Commands Type the name of your command and press 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

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

Example Session ~]$ uname Linux ~]$ arch x86_64 ~]$ who foxr tty :42 (:0) foxr pts/ :14 (:0) ~]$ uname; arch; who Linux x86_64 foxr tty :42 (:0) foxr pts/ :14 (:0) prompt user input program output

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”

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

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)

The ls Command: Long Listings Example 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

The ls Command: Other Options OptionMeaning -ASame as –a exact that. and.. are not shown -BIgnore items whose names end with ~ (~ is used for backup files) -CList entries in columns (fits more items on the screen) -dList directories by name, do not list their content -FAppend listings with item classifications (ends directory names with /, ends executable files with *) -gSame as –l except that owner is not shown -GSame as –l except that group owner is not shown -hWhen used with –l, modifies file sizes to be “human readable” -iInclude 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) -rList items in reverse alphabetical order -RRecursive listing (list contents of all subdirectories) -sWhen used with –l, outputs sizes in blocks rather than bytes -SSort files by size rather than name -tSort files by modification time rather than name -XSort files by extension name rather than name (numeric 1) List files one per line (do not use columns)

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

man Pages: Types of Information EntryMeaning NameCommand name with one line description SynopsisOne or more entries that show the syntax of the command’s usage DescriptionDetailed description of what the command does Options Description of every available option, including additional parameters expected by options, and options that options can have (options may be listed under description) Arguments/ParametersAdditional parameters expected or allowed EnvironmentEnvironment variables that this command might use BugsKnown errors or fixes NotesAdditional information that might be useful FilesFiles that this command might access Exit or return valuesError or exit codes ExamplesIn rare cases, some man pages include examples of how to use the command FunctionsNames of functions that can be called by the command Author(s)Person/people who wrote the command’s programming code See alsoOther Linux instructions related to this one or worth exploring

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 to search backward for a string - ?string exit man page – ‘q’, ‘Q’

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

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

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) 1ls 2cd ~ 3pwd 4man cd 5ls –l 6cd /bin 7ls –l | grep root 8 cd ~ 9vi script1 10cat script1 11rm script1 12history

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

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

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 “”)

Bash Features: Environment Variables These variables are pre-defined at the time the user logs in and opens a shell VariableMeaning (value) DESKTOP_SESSIONGUI being used (Gnome, KDE) DISPLAYThe terminal window being used, denoted as :0.0 if you are logged in through a GUI HISTSIZENumber of entries retained in the history list HOMEUser’s home directory name HOSTNAMEName of computer LOGNAMEUser’s user name MAILLocation of user’s directory OLDPWDLast directory visited before current directory (see PWD) PATHThe list of directories that the interpreter checks with each command PS1Specifies the user’s prompt (explained in more detail in table 2.5) PWDThe current directory SHELLName of the program being used as the user’s shell (for Bash, this is usually /bin/bash) TERMThe terminal type being used USERThe user’s username USERNAMEThe user’s name (if provided)

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

Bash Features: PS1 Variable PS1 encodes the information that is displayed in your prompt – for instance: \d] $ is your current PS1 CharacterMeaning \uUser name \hHost name \wCurrent working directory \!Number of current command (as it will appear in the history list) \tCurrent time \dDate (as in Mon Aug 13) \$?Status of last command \aBell character (makes a tone) \jNumber of jobs in the current shell (we explore jobs in chapter 4) \nNew line [`command`]Output of the Linux command

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

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 CharacterMeaning \\Output a \ \b Backspace (back cursor up 1 position) \nNewline – start a new line \tTab \! \$ \& \; \' \"!, $, &, ;, ' and " respectively

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 ‘’

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

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!

Bash Features: Command Line Editing KeystrokeMeaning ctrl+aMove cursor to beginning of line ctrl+eMove cursor to end of line ctrl+n (also up arrow)Move to next instruction in history list ctrl+p (also down arrow)Move to previous instruction in history list ctrl+f (also right arrow)Move cursor one character to the right ctrl+b (also left arrow)Move cursor one character to the left ctrl+d (also delete key)Delete character at cursor ctrl+kDelete all characters from cursor to end of line ctrl+uDelete everything from the command line ctrl+wDelete all characters from front of word to cursor ctrl+y Yank, return all deleted characters (aside from c+d) to cursor position ctrl+_Undo last keystroke esc+fMove cursor to space after current word esc+bMove cursor to beginning of current word esc+dDelete the remainder of the word

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

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

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

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 so how do we end input? Use control+d We can also use cat << string – End input once you type string We might combine to create a file of text we enter by the command line – cat file.txt

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

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

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, Bash completes it – If the portion is not unique, Bash emits a beep – Press 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!)

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

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)

Other Shells: TC Shell Updated version of C shell, T for TENEX operating system, or tcsh Updated history commands KeystrokeMeaning !!Execute previous command !nExecute command n on the history list !stringExecute the most recent command that starts with the characters string !?stringExecute the most recent command that contains the characters string command !*Execute command but use all arguments from the previous command command !$Execute command but use the last argument from the previous command command !^Execute command but use the first argument from the previous command command !:nExecute command but use the nth argument from the previous command command !:m-nExecute command but use arguments m-n from the previous command

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

Comparing Bash and TC Shell FeatureBashTC Shell User prompt$% Assignment statement format var=valueset var=value Export statementexport var=valuesetenv var value Alias definition formatalias term=commandalias term command Default user definition file.bashrc.cshrc Automated spelling correction noyes

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.

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