Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Similar documents
Review of Fundamentals

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CST Algonquin College 2

Bourne Shell Reference

Introduction to UNIX Part II

Standard. Shells. tcsh. A shell script is a file that contains shell commands that perform a useful function. It is also known as shell program.

Useful Unix Commands Cheat Sheet

5/20/2007. Touring Essential Programs

Shells and Shell Programming

Linux shell programming for Raspberry Pi Users - 2

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Basic Linux (Bash) Commands

COMP 4/6262: Programming UNIX

Bash Reference Manual

UNIX Shell Programming

Implementation of a simple shell, xssh

Shells and Shell Programming

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Chapter 9. Shell and Kernel

Assignment clarifications

Lab 2: Linux/Unix shell

Processes and Shells

Linux shell scripting Getting started *

Unix Processes. What is a Process?

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Introduction to the Shell

5/8/2012. Specifying Instructions to the Shell Chapter 8

Shell Start-up and Configuration Files

bash, part 3 Chris GauthierDickey

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

Bash Reference Manual Reference Documentation for Bash Edition 2.5b, for Bash Version 2.05b. July 2002

Module 8 Pipes, Redirection and REGEX

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

GNU Bash. An Introduction to Advanced Usage. James Pannacciulli Systems (mt) Media Temple

Creating a Shell or Command Interperter Program CSCI411 Lab

Part III. Shell Config. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Shell Programming for System Administrators Student Guide

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Implementation of a simple shell, xssh

GNU Bash. an introduction to advanced usage. James Pannacciulli Systems Engineer.

Title:[ Variables Comparison Operators If Else Statements ]

Last Time. on the website

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

Linux 系统介绍 (III) 袁华

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

A shell can be used in one of two ways:

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

The Command Line. Matthew Bender. Friday 18 th September, CMSC Command Line Workshop

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

UNIX shell scripting

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Basics. I think that the later is better.

BASH SHELL SCRIPT 1- Introduction to Shell

System Programming. Unix Shells

CS 307: UNIX PROGRAMMING ENVIRONMENT REVIEW FOR EXAM 2

Topic 2: More Shell Skills

Vi & Shell Scripting

UNIX and Linux Essentials Student Guide

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Bourne Shell Programming Topics Covered

Lecture #10 - Interactive Korn Shell (Chapter 11)

Useful information mostly written by me, the conspicuous exception being the bash manpage... BASH(1) 1998 Apr 1 BASH(1)

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Introduction to Linux Organizing Files

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

1004 Index awk (cont.): commands: from within a file, in a script file, 111 comparison expressions, compound patterns, computatio

Introduction to UNIX Shell Exercises

bash startup files Linux/Unix files stty Todd Kelley CST8207 Todd Kelley 1

Here redirection. Case statement. Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March 5, 2004

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

Unix Shell Environments. February 23rd, 2004 Class Meeting 6

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Lecture 3 Tonight we dine in shell. Hands-On Unix System Administration DeCal

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Shell Scripting. Xiaoxu Guan High Performance Computing, LSU February 17, 2016

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

85321, Systems Administration Chapter 6: The shell

bash Execution Control COMP2101 Winter 2019

Bash Programming. Student Workbook

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

The Online Unix Manual

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions?

Linux Command Line Interface. December 27, 2017

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Transcription:

Bashed One Too Many Times Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

What is a Shell? The shell interprets commands and executes them It provides you with an environment in which to work It provides an entire programming language which can be used either in scripts or directly from the command line 2

Why Bash? It is the standard shell on Linux and is available for almost every version of Unix As we'll see, it is feature-rich It is mostly compatible with the earlier Bourne shell and Korn shell It is even available for Windows under Cygwin 3

What We'll Cover Metacharacters Variables Quoting Pattern Matching File Descriptors and Redirection Built-in Commands Startup Files Keyboard Shortcuts History Job Control and Processes Options Tests Flow Control 4

Metacharacters Metacharacters are used to pass special directions to the shell. They include *, [ ],?,&,`,\,{ },>,>>,<,( ), ;,/, $, and Metacharacters must be escaped if you do not want them interpreted by the shell 5

Escaping Metacharacters 6

Variables Variables are containers for some value They are designated by a '$' unless they are being initialized They are restricted to the current shell unless they are exported View them with the 'env' command 7

Variable Expansion If there is any ambiguity over exactly what the variable is, use {} to specify 8

Unexported Variables 9

Exported Variables 10

Special Variables $? - exit code of the last command $! - PID of last backgrounded command $$ - PID of current shell 11

Built-in Variables PS1 PS2 HOME PATH HISTFILESIZE HISTSIZE OLDPWD 12

Quoting (from Uwe Waldman) Single quotes ('...') protect everything (even backslashes, newlines, etc.) except single quotes, until the next single quote. Double quotes ("...") protect everything except double quotes, backslashes, dollar signs, and backquotes, until the next double quote. A backslash can be used to protect ", \, $, or ` within double quotes. A backslashnewline pair disappears completely; a backslash that does not precede ", \, $, `, or newline is taken literally. 13

Quoting Examples 14

Pattern Matching? matches one character * matches zero or more characters [] is used to match characters in between brackets [a-z] will match any lowercase letter [1-9] will match any digit {} will match a comma-separated list {local,lib} will match either 'local' or 'lib' {} can also be used to generate substrings 15

Examples of Pattern Matching 16

Substrings {} can be used to match substrings ${VAR:0} is the entire variable name ${VAR:0:1} returns the first character ${VAR:0:2} returnes the first two...and so on. The first digit is your start position, the second is the number of digit to return 17

Substring Example 18

Posix Character Classes Support alnum alpha ascii blank cntrl digit graph lower print punct space upper word xdigit 19

Character Class Examples 20

File Descriptors By default, there are three file descriptors 0, STDIN 1, STDOUT 2, STDERR They are all pointed to your terminal by default, but can be re-directed 21

Redirection Operators takes the output of one command and feeds it into another > redirects STDOUT and overwrites >> redirects STDOUT and appends < redirects STDIN 22

Redirection Example 23

Using Redirection to Read Files 24

More Redirection Tying STDOUT and STDERR 25

Opening and Closing File Descriptors 26

Here Documents Here documents redirect multiple lines of input easily. They are often used in shell scripts and are ended by an arbitary string, which by convention is often 'EOF' 27

Here Document Example 28

Built-in Commands cd eval exec exit export test alias unset echo shopt source ulimit 29

Startup Files Login shells (those that had to start via /bin/login reading /etc/passwd) use these files, in order /etc/profile.bash_profile.bash_login..profile 30

Startup Files (continued) Non-login shells only read.bashrc It is not unusual for.bash_profile to source.bashrc 31

Keyboard shortcuts The readline library is used by bash to provide command-line editing CTL-A go to beginning of line CTL-E go to end of line CTL-U erase all of line CTL-K erase from cursor to end of line CTL-R reverse search CTL-D logout (technically EOF, and only at the beginning of a line otherwise, delete) 32

History Up arrow - go one command back in history The 'history' command shows the last ten commands and their numbers ('history -n' shows the last n commands) The 'fc' command can be used to edit and execute earlier commands The last command is aliased as '!!' The last argument to the last command is aliased as '!$' Other arguments have numbers assigned to them 33

History Examples 34

'history' and 'fc' The 'history' command recalls prior commands by number. You can specify n commands to recall with 'history n' 'fc' does much the same thing when run as 'fc -l' 'fc n' drops you into an editor (you may need to define your editor in $FCEDIT) to edit command n. Once you save and quit, your edited command is run. 35

Job Control Commands can be backgrounded with '&' Commands can be foregrounded with 'fg' Commands can be suspended with CTL-Z Commands can be killed with CTL-C Suspended jobs can be listed with the 'jobs' command Suspended jobs can be killed with 'kill %n' where n is the job number Backgrounded jobs can be detached from a terminal with 'disown' 36

Job Control (continued) Process can be started in subshells with parentheses Commands can be run in backticks to capture their output 37

Subshells A subshell is spawned by commands run inside of parentheses. When these commands are finished, the state of the parent shell is maintained. Subshells inherit the parent shell's environment. 38

Subshells in SSH 39

Command Substitution `` (backticks) are used to run a command and send its output to a variable or another command 40

Options Options can be configured with 'set -o optionname' They are un-configured with 'unset optionname' Some interesting options: emacs vi noclobber ignoreeof noglob 41

More options Options can be configured with the built-in shopt command 42

Math Examples 43

Tests Tests are used to evaluate the truth of an expression 'test expr' and '[ expr ]' are equivalent. [ is a built-in command '[[ expr ]]' is an alternate syntax, without file globbing. [[ is a reserved word Tests are generally negated with '!' 44

File Tests -d Directory -e Exists -f Regular file -h Symbolic link (also -L) -p Named pipe -r Readable by you -s Not empty -wwritable by you 45

Numeric Tests -lt less than -gt greater than -eq equal to -le less than or equal to -ge greater than or equal to 46

String Comparisons and Tests =,== Equal to!= Not equal to > ASCII value is greater than < ASCII value is less than -z String is zero length -n String is not null Caution always quote strings when testing them! 47

Single Bracket Tests Note the errors! 48

Double Bracket Tests 49

Compound Tests Use '-o' and '-a' inside of test/single brackets The equivalents in [[ are '&&' and ' ' 50

Flow Control If do something if condition is true If -then-else do one thing if something is true, otherwise do something else For do something upon each member of a list While Until Case like 'if-then-else' but gracefully handles more possibilities 51

If 52

If-then-else 53

For 54

While 55

Until 56

Case 57

References http://www.gnu.org/software/bash/manual/bashref.html http://tldp.org/ldp/abs/html/ http://learnlinux.tsf.org.za/courses/build/shell-scripting/ch12s04.html http://linuxshellaccount.blogspot.com/2008/02/finding-and-reading-files-in-shell-when.html 58

Questions? 59