bash Cookbook
Solutions and Examples for bash Users
By
Carl Albing,
JP Vossen,
Cameron Newham
May 2007
Pages: 622
| Table of Contents
| Index
| Sample Chapter
| Colophon
Table of Contents
-
Chapter 1 Beginning bash
-
Why bash?
-
The bash Shell
-
Decoding the Prompt
-
Showing Where You Are
-
Finding and Running Commands
-
Getting Information About Files
-
Showing All Hidden (dot) Files in the Current Directory
-
Using Shell Quoting
-
Using or Replacing Built-ins and External Commands
-
Determining If You Are Running Interactively
-
Setting bash As Your Default Shell
-
Getting bash for Linux
-
Getting bash for xBSD
-
Getting bash for Mac OS X
-
Getting bash for Unix
-
Getting bash for Windows
-
Getting bash Without Getting bash
-
Learning More About bash Documentation
-
Chapter 2 Standard Output
-
Writing Output to the Terminal/Window
-
Writing Output but Preserving Spacing
-
Writing Output with More Formatting Control
-
Writing Output Without the Newline
-
Saving Output from a Command
-
Saving Output to Other Files
-
Saving Output from the ls Command
-
Sending Both Output and Error Messages to Different Files
-
Sending Both Output and Error Messages to the Same File
-
Appending Rather Than Clobbering Output
-
Using Just the Beginning or End of a File
-
Skipping a Header in a File
-
Throwing Output Away
-
Saving or Grouping Output from Several Commands
-
Connecting Two Programs by Using Output As Input
-
Saving a Copy of Output Even While Using It As Input
-
Connecting Two Programs by Using Output As Arguments
-
Using Multiple Redirects on One Line
-
Saving Output When Redirect Doesn't Seem to Work
-
Swapping STDERR and STDOUT
-
Keeping Files Safe from Accidental Overwriting
-
Clobbering a File on Purpose
-
Chapter 3 Standard Input
-
Getting Input from a File
-
Keeping Your Data with Your Script
-
Preventing Weird Behavior in a Here-Document
-
Indenting Here-Documents
-
Getting User Input
-
Getting Yes or No Input
-
Selecting from a List of Options
-
Prompting for a Password
-
Chapter 4 Executing Commands
-
Running Any Executable
-
Telling If a Command Succeeded or Not
-
Running Several Commands in Sequence
-
Running Several Commands All at Once
-
Deciding Whether a Command Succeeds
-
Using Fewer if Statements
-
Running Long Jobs Unattended
-
Displaying Error Messages When Failures Occur
-
Running Commands from a Variable
-
Running All Scripts in a Directory
-
Chapter 5 Basic Scripting: Shell Variables
-
Documenting Your Script
-
Embedding Documentation in Shell Scripts
-
Promoting Script Readability
-
Separating Variable Names from Surrounding Text
-
Exporting Variables
-
Seeing All Variable Values
-
Using Parameters in a Shell Script
-
Looping Over Arguments Passed to a Script
-
Handling Parameters with Blanks
-
Handling Lists of Parameters with Blanks
-
Counting Arguments
-
Consuming Arguments
-
Getting Default Values
-
Setting Default Values
-
Using null As a Valid Default Value
-
Using More Than Just a Constant String for Default
-
Giving an Error Message for Unset Parameters
-
Changing Pieces of a String
-
Using Array Variables
-
Chapter 6 Shell Logic and Arithmetic
-
Doing Arithmetic in Your Shell Script
-
Branching on Conditions
-
Testing for File Characteristics
-
Testing for More Than One Thing
-
Testing for String Characteristics
-
Testing for Equal
-
Testing with Pattern Matches
-
Testing with Regular Expressions
-
Changing Behavior with Redirections
-
Looping for a While
-
Looping with a read
-
Looping with a Count
-
Looping with Floating-Point Values
-
Branching Many Ways
-
Parsing Command-Line Arguments
-
Creating Simple Menus
-
Changing the Prompt on Simple Menus
-
Creating a Simple RPN Calculator
-
Creating a Command-Line Calculator
-
Chapter 7 Intermediate Shell Tools I
-
Sifting Through Files for a String
-
Getting Just the Filename from a Search
-
Getting a Simple True/False from a Search
-
Searching for Text While Ignoring Case
-
Doing a Search in a Pipeline
-
Paring Down What the Search Finds
-
Searching with More Complex Patterns
-
Searching for an SSN
-
Grepping Compressed Files
-
Keeping Some Output, Discarding the Rest
-
Keeping Only a Portion of a Line of Output
-
Reversing the Words on Each Line
-
Summing a List of Numbers
-
Counting String Values
-
Showing Data As a Quick and Easy Histogram
-
Showing a Paragraph of Text After a Found Phrase
-
Chapter 8 Intermediate Shell Tools II
-
Sorting Your Output
-
Sorting Numbers
-
Sorting IP Addresses
-
Cutting Out Parts of Your Output
-
Removing Duplicate Lines
-
Compressing Files
-
Uncompressing Files
-
Checking a tar Archive for Unique Directories
-
Translating Characters
-
Converting Uppercase to Lowercase
-
Converting DOS Files to Linux Format
-
Removing Smart Quotes
-
Counting Lines, Words, or Characters in a File
-
Rewrapping Paragraphs
-
Doing More with less
-
Chapter 9 Finding Files: find, locate, slocate
-
Finding All Your MP3 Files
-
Handling Filenames Containing Odd Characters
-
Speeding Up Operations on Found Files
-
Finding Files Across Symbolic Links
-
Finding Files Irrespective of Case
-
Finding Files by Date
-
Finding Files by Type
-
Finding Files by Size
-
Finding Files by Content
-
Finding Existing Files and Content Fast
-
Finding a File Using a List of Possible Locations
-
Chapter 10 Additional Features for Scripting
-
"Daemon-izing" Your Script
-
Reusing Code with Includes and Sourcing
-
Using Configuration Files in a Script
-
Defining Functions
-
Using Functions: Parameters and Return Values
-
Trapping Interrupts
-
Redefining Commands with alias
-
Avoiding Aliases, Functions
-
Chapter 11 Working with Dates and Times
-
Formatting Dates for Display
-
Supplying a Default Date
-
Automating Date Ranges
-
Converting Dates and Times to Epoch Seconds
-
Converting Epoch Seconds to Dates and Times
-
Getting Yesterday or Tomorrow with Perl
-
Figuring Out Date and Time Arithmetic
-
Handling Time Zones, Daylight Saving Time, and Leap Years
-
Using date and cron to Run a Script on the Nth Day
-
Chapter 12 End-User Tasks As Shell Scripts
-
Starting Simple by Printing Dashes
-
Viewing Photos in an Album
-
Loading Your MP3 Player
-
Burning a CD
-
Comparing Two Documents
-
Chapter 13 Parsing and Similar Tasks
-
Parsing Arguments for Your Shell Script
-
Parsing Arguments with Your Own Error Messages
-
Parsing Some HTML
-
Parsing Output into an Array
-
Parsing Output with a Function Call
-
Parsing Text with a read Statement
-
Parsing with read into an Array
-
Getting Your Plurals Right
-
Taking It One Character at a Time
-
Cleaning Up an SVN Source Tree
-
Setting Up a Database with MySQL
-
Isolating Specific Fields in Data
-
Updating Specific Fields in Data Files
-
Trimming Whitespace
-
Compressing Whitespace
-
Processing Fixed-Length Records
-
Processing Files with No Line Breaks
-
Converting a Data File to CSV
-
Parsing a CSV Data File
-
Chapter 14 Writing Secure Shell Scripts
-
Avoiding Common Security Problems
-
Avoiding Interpreter Spoofing
-
Setting a Secure $PATH
-
Clearing All Aliases
-
Clearing the Command Hash
-
Preventing Core Dumps
-
Setting a Secure $IFS
-
Setting a Secure umask
-
Finding World-Writable Directories in Your $PATH
-
Adding the Current Directory to the $PATH
-
Using Secure Temporary Files
-
Validating Input
-
Setting Permissions
-
Leaking Passwords into the Process List
-
Writing setuid or setgid Scripts
-
Restricting Guest Users
-
Using chroot Jails
-
Running As a Non-root User
-
Using sudo More Securely
-
Using Passwords in Scripts
-
Using SSH Without a Password
-
Restricting SSH Commands
-
Disconnecting Inactive Sessions
-
Chapter 15 Advanced Scripting
-
Finding bash Portably for #!
-
Setting a POSIX $PATH
-
Developing Portable Shell Scripts
-
Testing Scripts in VMware
-
Using for Loops Portably
-
Using echo Portably
-
Splitting Output Only When Necessary
-
Viewing Output in Hex
-
Using bash Net-Redirection
-
Finding My IP Address
-
Getting Input from Another Machine
-
Redirecting Output for the Life of a Script
-
Working Around "argument list too long" Errors
-
Logging to syslog from Your Script
-
Sending Email from Your Script
-
Automating a Process Using Phases
-
Chapter 16 Configuring and Customizing bash
-
bash Startup Options
-
Customizing Your Prompt
-
Change Your $PATH Permanently
-
Change Your $PATH Temporarily
-
Setting Your $CDPATH
-
Shortening or Changing Command Names
-
Adjusting Shell Behavior and Environment
-
Adjusting readline Behavior Using .inputrc
-
Keeping a Private Stash of Utilities by Adding ~/bin
-
Using Secondary Prompts: $PS2, $PS3, $PS4
-
Synchronizing Shell History Between Sessions
-
Setting Shell History Options
-
Creating a Better cd Command
-
Creating and Changing into a New Directory in One Step
-
Getting to the Bottom of Things
-
Adding New Features to bash Using Loadable Built-ins
-
Improving Programmable Completion
-
Using Initialization Files Correctly
-
Creating Self-Contained, Portable RC Files
-
Getting Started with a Custom Configuration
-
Chapter 17 Housekeeping and Administrative Tasks
-
Renaming Many Files
-
Using GNU Texinfo and Info on Linux
-
Unzipping Many ZIP Files
-
Recovering Disconnected Sessions Using screen
-
Sharing a Single bash Session
-
Logging an Entire Session or Batch Job
-
Clearing the Screen When You Log Out
-
Capturing File Metadata for Recovery
-
Creating an Index of Many Files
-
Using diff and patch
-
Counting Differences in Files
-
Removing or Renaming Files Named with Special Characters
-
Prepending Data to a File
-
Editing a File in Place
-
Using sudo on a Group of Commands
-
Finding Lines in One File But Not in the Other
-
Keeping the Most Recent N Objects
-
Grepping ps Output Without Also Getting the grep Process
Itself
-
Finding Out Whether a Process Is Running
-
Adding a Prefix or Suffix to Output
-
Numbering Lines
-
Writing Sequences
-
Emulating the DOS Pause Command
-
Commifying Numbers
-
Chapter 18 Working Faster by Typing Less
-
Moving Quickly Among Arbitrary Directories
-
Repeating the Last Command
-
Running Almost the Same Command
-
Substituting Across Word Boundaries
-
Reusing Arguments
-
Finishing Names for You
-
Playing It Safe
-
Chapter 19 Tips and Traps: Common Goofs for Novices
-
Forgetting to Set Execute Permissions
-
Fixing "No such file or directory" Errors
-
Forgetting That the Current Directory Is Not in the $PATH
-
Naming Your Script Test
-
Expecting to Change Exported Variables
-
Forgetting Quotes Leads to "command not found" on
Assignments
-
Forgetting That Pattern Matching Alphabetizes
-
Forgetting That Pipelines Make Subshells
-
Making Your Terminal Sane Again
-
Deleting Files Using an Empty Variable
-
Seeing Odd Behavior from printf
-
Testing bash Script Syntax
-
Debugging Scripts
-
Avoiding "command not found" When Using Functions
-
Confusing Shell Wildcards and Regular Expressions
-
Appendix A Reference Lists
-
bash Invocation
-
Prompt String Customizations
-
ANSI Color Escape Sequences
-
Built-in Commands and Reserved Words
-
Built-in Shell Variables
-
set Options
-
shopt options
-
Adjusting Shell Behavior Using set, shopt, and Environment
Variables
-
Test Operators
-
I/O Redirection
-
echo Options and Escape Sequences
-
printf
-
Date and Time String Formatting with strftime
-
Pattern-Matching Characters
-
extglob Extended Pattern-Matching Operators
-
tr Escape Sequences
-
Readline Init File Syntax
-
emacs Mode Commands
-
vi Control Mode Commands
-
Table of ASCII Values
-
Appendix B Examples Included with bash
-
Startup-Files Directory Examples
-
Appendix C Command-Line Processing
-
Command-Line Processing Steps
-
Appendix D Revision Control
-
CVS
-
Subversion
-
RCS
-
Other
-
Appendix E Building bash from Source
-
Obtaining bash
-
Unpacking the Archive
-
What's in the Archive
-
Who Do I Turn To?
-
Colophon
Return to bash Cookbook