The Linux Shell The shell contains

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

Chapter 9: The TC Shell Everything you thought you knew is now wrong.
Chapter One The Essence of UNIX.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Chapter Seven Unix Shell Environments1 System Programming UNIX Shell Environments.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Linux+ Guide to Linux Certification, Second Edition
Linux+ Guide to Linux Certification, Second Edition
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.
Using Linux Commands 2 Lab#5
Shell Script Examples.
Linux Shell. 2 Linux Command-Line Interface ■ Linux shells: A shell is a command interpreter that allows you to type commands from the keyboard to interact.
Using Linux Commands 2 Lab#5. Sort command Sort the lines of text files. $ sort fileName by default it will sort in normal order(alphabetical 0-9 A-Z.
Introduction to Linux Workshop February Introduction Rob Lane & The HPC Support Team Research Computing Services CUIT.
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.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
– 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.
Shell Features CSCI N321 – System and Network Administration Copyright © 2000, 2005 by Scott Orr and the Trustees of Indiana University.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
Linux+ Guide to Linux Certification, Third Edition
Basic Shell Scripting - Part 1 Objective - Learn to: Read Start-up Files Edit Start-up Files Modify Your User Environment Communicate with Users Write.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Workbook 6 – Part 2 The Bash Shell
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
1 Operating Systems and Using Linux Topics What is an Operating System? Linux Overview Frequently Used Linux Commands Some content in this lecture added.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Lesson 2-Touring Essential Programs. Overview Development of UNIX and Linux. Commands to execute utilities. Communicating instructions to the shell. Navigating.
Unix Shell Environments February 23rd, 2004 Class Meeting 6.
UNIX shell environments CS 2204 Class meeting 6 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Shell Scripting – Putting it All Together. Agenda Escaping Characters Wildcards Redirecting Output Chaining and Conditional Chaining Unnamed and Named.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
1 Day 18 Bash and the.files. 2 The.files ls shows you the files in your directory –Or at least most of them. –Some files are hidden. Try: ls –a –This.
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.
Chapter 8: The Bourne Again Shell It’s a command interpreter, it’s a programming language, and it makes a mean martini.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Linux+ Guide to Linux Certification, Second Edition
Configuration your environment Many user-configurable Unix programs (such as your shell) read configuration files when they start up. These configuration.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 © 2012 John Urrutia. All rights reserved. Chapter 09 The TC Shell.
Linux Administration Working with the BASH Shell.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Implementation of a simple shell, xssh
Lesson 5-Exploring Utilities
SUSE Linux Enterprise Desktop Administration
Introduction to Shells
Shell Features CSCI N321 – System and Network Administration
Implementation of a simple shell, xssh (Section 1 version)
Implementation of a simple shell, xssh
Shell Script Assignment 1.
Shell Environments.
John Carelli, Instructor Kutztown University
Linux Shell Script Programming
CSE 303 Concepts and Tools for Software Development
Unix Shell Environments
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Presentation transcript:

The Linux Shell The shell contains an interpreter an environment of previously defined entities (e.g., variables, functions) and the results of previously executed instructions a command line You are placed into a shell when you open a new terminal/console window start a shell from a terminal/console window, e.g., by running bash open a remote text-based connection as with through the program ssh Linux provides several different shells sh (the bourne shell) csh – incorporated new ideas like history and command line editing tcsh – updated version of tcsh bash – updated version of Bourne shell (bourne again shell)

Command Line When you are in a shell, you are presented with a prompt typically the user prompt will look like $ (if you log in as root, the prompt will be #) the prompt is your command line, enter a command and press <enter> whatever you enter is executed and if any output is provided, it will be displayed to the window you are operating in Once a command is on your command line You can edit it as if it were a line in a word processor This allows you to recall a previous command (from history) and change it Command line editing features are similar to the commands used in emacs to move the cursor and edit a line

Commands

History The first feature built into bash to examine is history Every time you enter a command, it is stored in your history list for that shell You can recall the history list to see what you did, or select instructions from the history list to be re-executed history – show history list history –n # where # is a number displays the last # instructions on your history list to the right is an example of a partial history list

Recalling Commands from History To recall the last command use !! control+p – this places that command on your command line (you can edit it, discussed in the next slide) To move through the history list control+p – go back through history (also up arrow) control+n – go forward through history (also down arrow) To re-execute instruction # from the history list !# To re-execute the last instruction that started with string !string From the history list on the right: #7 will re-execute ls –l #l (lower case L) will re-execute ls –al #ls / will re-execute ls /home

Tab Completion Saves you from having to type a full directory or file name Type part of the name<tab> If unique, Bash completes the name If not unique, Bash beeps at you, type <tab><tab> to get a listing of all matches Example: current directory contains these files forgotten.txt frank.txt fresh.txt functions.txt funny.txt other_stuff.txt You type less fo<tab> bash completes with forgotten.txt You type less fr<tab> bash beeps at you You type less fr<tab><tab> bash lists frank.txt fresh.txt

Aliases Alias – a substitute name for a command alias name=command If command includes spaces, place the full thing in ‘ ’ as in alias up=‘cd ..’ The new name can be the same as an old name alias rm=‘rm –i’ You will define aliases to save typing (shorten commands) simplify complex commands eliminate directory paths safety (for instance, forcing the –i option with rm) typos (if you are commonly making the same mistake, for instance typing sl instead of ls) alias sl=ls Defined aliases at the command line prompt but the alias is then only known for this session, close the shell, lose the alias

Examples alias add=/usr/sbin/useradd alias rm=‘rm –i’ (safety) now you do not need to remember the path to perform useadd alias rm=‘rm –i’ (safety) alias lsl=‘ls –l’ (shorten a common instruction) alias ..=‘cd ..’ (aliases do not have to be letters) alias mr=rm (common typo) alias md=mkdir (shorten) alias h10=‘history –n 10’ (shorten) alias xt=‘xterm –bg black –fg white &’ shorten hard to remember command

Filename Expansion There are wildcard characters available in Linux ls * - list everything in the current directory ls *.txt – list everything that ends with .txt ls *.* - list everything that contains a period The * means “everything” The ? means “any one character” ls file?.txt – list everything whose name starts with file, has one character, followed by .txt such as file1.txt, filea.txt and file_.txt but not file1a.txt Use [ ] to enumerate a possible list or range as in ls file[0-9].txt or ls file[123].txt or ls file[a-z].txt we explore these in more detail in the next chapter

Variables You are allowed to create and use variables in your shell To create a variable use as assignment statement NAME=value Variables store strings only You can override this to store integer numbers by placing the value in ( ) as in AGE=(21) To set a variable to the value in another variable use NAME=$VAR If the right hand side string has one or more spaces, enclose the entire thing in “” (although ‘’ is available, don’t use it) NAME=“Frank Zappa” NAME=“$FIRST $LAST” If the right hand side is a computation, place it in $((…)) X=$((Y+1)) if a Linux instruction, use either $(…) or `…`

Examples AGE=(21) AGE=$((AGE+1)) // AGE becomes 22 AGE=$AGE+1 this sets AGE to be “21+1” (that is, the characters 2, 1, +, 1) NAME=“$FIRST $LAST” if $FIRST is Frank and $Last is Zappa then NAME is “Frank Zappa” MESSAGE=“Today is `date`” Sets MESSAGE to the string “Today is ” followed by the result from the Linux instruction date X=$((Y/5)) integer division, X is the quotient Q=$((Y%5)) integer remainder, Q is the remainder NOTE: the remainder will be between 0 and 4 A=$(((X+1)*Y)) added parens to control order of operations

echo Output instruction, form is echo string Where string is any combination of literal characters, $variables or $(Linux commands) `Linux commands` If you forget a $ you get the variable name without its value echo Hello $FIRST LAST outputs Hello followed by the value in FIRST followed by LAST literally because we forgot the $ Assume FIRST=Frank, LAST=Zappa echo Hello $FIRST $LAST outputs Hello Frank Zappa echo “Hello $FIRST $LAST” echo ‘Hello $FIRST $LAST’ outputs Hello $FIRST $LAST

Environment Variables We don’t generally use our own variables (unless we are shell scripting) but there are useful environment variables, defined by the OS To see your environment variables, type env HOSTNAME – name of your computer SHELL – name of the current shell (e.g., /bin/bash) USER – your user name HOME – your home directory PWD – current working directory (this is used by the pwd command) HISTLIST – number of commands to be retained in your history list PS1 – your prompt defined (details in two slides) PATH – a list of directories that bash will examine with every command (see next slide)

PATH & PS1 You will place commonly used directories in PATH (this is already established for you) Assume PATH stores /usr/bin:/usr/local/bin:/bin you can issue a command that is stored in /usr/bin, /usr/local/bin or /bin without including the path if the command is not found in those directories (or the current directory), you will receive an error To add to PATH type PATH=$PATH:nextdir To view your PATH variable, type echo $PATH PS1 defines your prompt It includes special characters to denote date, instruction number, username, etc For instance: PS1=“\u@\t $” will define your prompt to display your user name, the @ symbol, the command number, a space and a $ to indicate that you are a normal user (and not root)

Redirection By default, most Linux programs expect input to come from disk file and output to go to the window You can redirect input and output as follows command > file – redirect output to file, if file already exists, override it command >> file – redirect output to append to file, if file does not already exist, create it command < file – redirect input to come from file (this is the typical case for most instructions, so is not particularly useful except when running shell scripts) command << string – redirect input to come from keyboard, end the input when “string” is encountered command | command – this is known as a pipe, take the output of one command and use it as input to the next

Examples cat file1.txt file2.txt file3.txt >> file4.txt Take the contents of 3 files and append them to file4.txt cat << quit > list input items from keyboard one at a time until the word “quit” is entered, save list to the file list cat << quit | sort > list same as above except the items are sorted ls –l *.txt | less obtain the long listing of all items in the current directory that end with .txt and pass them to the less program to display the contents one screen at a time

Help Three forms of help in Linux man – manual page help – help page exists for most commands informs you of what the command does, what the options are for using the command, related files and instructions to this command, and in some cases, examples help – help page only exists for some instructions man is more commonly used and more useful Apropos – given a string, lists all commands that contain that string as part of the command’s description useful if you are not sure of a command’s name before you try to obtain its man page

How the bash Interpreter Works Enter command Interpreter breaks input into words, operators Handle quote marks Replace aliases Separate command into individual commands if there are multiple commands Perform brace expansion, tilde expansion (convert ~ to home directory), assign variables their values Handle commands in ` ` or $( ), execute arithmetic operations Perform redirections Perform filename expansion (match against wildcard characters) Execute command and display results

Tailoring your bash Shell If you enter a variable or alias from the command line, that item is only defined in the current session If you type bash, you enter a new session If you exit this session, you lose those definitions If you open another window, you do not have those definitions It is easier to define these items in a script that is executed at the start of each shell session This is where we will define our initial PATH variable and any aliases We, as users, are free to edit these files to add to or change these definitions and add our own definitions

Order of Script Execution There are several scripts involved and are executed in this order /etc/profile – for any new log in /etc/bashrc – for any new bash session these two files are controlled by the system administrator to define common variables and aliases for all users .profile – for any new log in .bash_profile – for any new log in into a bash shell .bashrc – for any new bash session .bash_profile invokes .bashrc and .bash_login so it is best to put your definitions in .bashrc