Linux FAQ's & Manuals
- Linux Scripts
- Debian Install
- Bash For Beginners
- Bugzilla
- Consultants Guide
- GCC Manual
- Linux Command Line Tools
- Gnu Pascal Coding Standards
- Linux Installation Disk
- Labolatorium Linux(PL)
- Budowa systemu Linux(PL)
- Linux Dictionary
- Network Administrators
- Rescue Disk for Linux
- Red Hat Installation
- Red Hat Customization
- Red Hat Getting Started
- Red Hat Security
- Secure & Optimize
- Slackware Manual
- Suse Support
- Suse FAQ
Is there something new that open source development methods and values can bring to the economy? How about something old...?
WWdN: In Exile: 10d10t
"I've written a few times about how I love technology, and how technology, which is supposed to make our lives easier and less stressful, often does exactly the opposite..."
Free Software Magazine: The Emerging FOSS Revolution in Cuba
Now Cuba stands on the precipice of a revolution in the use and development of free and open source software...
Saddler: Gentoo is Not About Choice
Let's take a look at what we do see. Words like adaptability. Tools. Not choice. So, what is the strength, the purpose of Gentoo...?
OSWeekly: The Terrible State of WiFi in Linux
Understand, however, that I applaud the ongoing efforts of Ralink, Intel and Atheros. These groups have bent over backwards to make life easier for the casual Linux enthusiast...
advanced bash-scripting guide
an in-depth exploration of the art of shell scripting
mendel cooper
4.018 june 2006
| revision history | ||
|---|---|---|
| revision 3.8 | 26 feb 2006 | revised by: mc |
| 'blaeberry' release: minor update. | ||
| revision 3.9 | 15 may 2006 | revised by: mc |
| 'spiceberry' release: minor update. | ||
| revision 4.0 | 18 may 2006 | revised by: mc |
| 'winterberry' release: major update. | ||
this tutorial assumes no previous knowledge of scripting or programming, but progresses rapidly toward an intermediate/advanced level of instruction . . . all the while sneaking in little snippets of unix® wisdom and lore. it serves as a textbook, a manual for self-study, and a reference and source of knowledge on shell scripting techniques. the exercises and heavily-commented examples invite active reader participation, under the premise that the only way to really learn scripting is to write scripts.
this book is suitable for classroom use as a general introduction to programming concepts.
the latest update of this document, as an archived, bzip2-ed "tarball" including both the sgml source and rendered html, may be downloaded from the author's home site. a pdf version is also available. see the change log for a revision history.
dedication
for anita, the source of all the magic
- table of contents
- part 1. introduction
- part 2. basics
- part 3. beyond the basics
- part 4. commands
- part 5. advanced topics
- 17. regular expressions
- 18. here documents
- 19. i/o redirection
- 20. subshells
- 21. restricted shells
- 22. process substitution
- 23. functions
- 24. aliases
- 25. list constructs
- 26. arrays
- 27. /dev and /proc
- 28. of zeros and nulls
- 29. debugging
- 30. options
- 31. gotchas
- 32. scripting with style
- 33. miscellany
- 34. bash, versions 2 and 3
- 35. endnotes
- 35.1. author's note
- 35.2. about the author
- 35.3. where to go for help
- 35.4. tools used to produce this book
- 35.5. credits
- bibliography
- a. contributed scripts
- b. reference cards
- c. a sed and awk micro-primer
- d. exit codes with special meanings
- e. a detailed introduction to i/o and i/o redirection
- f. command-line options
- g. important files
- h. important system directories
- i. localization
- j. history commands
- k. a sample .bashrc file
- l. converting dos batch files to shell scripts
- m. exercises
- m.1. analyzing scripts
- m.2. writing scripts
- n. revision history
- o. mirror sites
- p. to do list
- q. copyright
- list of tables
- 14-1. job identifiers
- 30-1. bash options
- 33-1. numbers representing colors in escape sequences
- b-1. special shell variables
- b-2. test operators: binary comparison
- b-3. test operators: files
- b-4. parameter substitution and expansion
- b-5. string operations
- b-6. miscellaneous constructs
- c-1. basic sed operators
- c-2. examples of sed operators
- d-1. "reserved" exit codes
- l-1. batch file keywords / variables / operators, and their shell equivalents
- l-2. dos commands and their unix equivalents
- n-1. revision history
- list of examples
- 2-1. cleanup: a script to clean up the log files in /var/log
- 2-2. cleanup: an improved clean-up script
- 2-3. cleanup: an enhanced and generalized version of above scripts.
- 3-1. code blocks and i/o redirection
- 3-2. saving the results of a code block to a file
- 3-3. running a loop in the background
- 3-4. backup of all files changed in last day
- 4-1. variable assignment and substitution
- 4-2. plain variable assignment
- 4-3. variable assignment, plain and fancy
- 4-4. integer or string?
- 4-5. positional parameters
- 4-6. wh, whois domain name lookup
- 4-7. using shift
- 5-1. echoing weird variables
- 5-2. escaped characters
- 6-1. exit / exit status
- 6-2. negating a condition using !
- 7-1. what is truth?
- 7-2. equivalence of test, /usr/bin/test, [ ], and /usr/bin/[
- 7-3. arithmetic tests using (( ))
- 7-4. testing for broken links
- 7-5. arithmetic and string comparisons
- 7-6. testing whether a string is null
- 7-7. zmore
- 8-1. greatest common divisor
- 8-2. using arithmetic operations
- 8-3. compound condition tests using && and ||
- 8-4. representation of numerical constants
- 9-1. $ifs and whitespace
- 9-2. timed input
- 9-3. once more, timed input
- 9-4. timed read
- 9-5. am i root?
- 9-6. arglist: listing arguments with $* and $@
- 9-7. inconsistent $* and $@ behavior
- 9-8. $* and $@ when $ifs is empty
- 9-9. underscore variable
- 9-10. inserting a blank line between paragraphs in a text file
- 9-11. converting graphic file formats, with filename change
- 9-12. converting streaming audio files to ogg
- 9-13. emulating getopt
- 9-14. alternate ways of extracting substrings
- 9-15. using parameter substitution and error messages
- 9-16. parameter substitution and "usage" messages
- 9-17. length of a variable
- 9-18. pattern matching in parameter substitution
- 9-19. renaming file extensions:
- 9-20. using pattern matching to parse arbitrary strings
- 9-21. matching patterns at prefix or suffix of string
- 9-22. using declare to type variables
- 9-23. indirect references
- 9-24. passing an indirect reference to awk
- 9-25. generating random numbers
- 9-26. picking a random card from a deck
- 9-27. random between values
- 9-28. rolling a single die with random
- 9-29. reseeding random
- 9-30. pseudorandom numbers, using awk
- 9-31. c-type manipulation of variables
- 10-1. simple for loops
- 10-2. for loop with two parameters in each [list] element
- 10-3. fileinfo: operating on a file list contained in a variable
- 10-4. operating on files with a for loop
- 10-5. missing in [list] in a for loop
- 10-6. generating the [list] in a for loop with command substitution
- 10-7. a grep replacement for binary files
- 10-8. listing all users on the system
- 10-9. checking all the binaries in a directory for authorship
- 10-10. listing the symbolic links in a directory
- 10-11. symbolic links in a directory, saved to a file
- 10-12. a c-like for loop
- 10-13. using efax in batch mode
- 10-14. simple while loop
- 10-15. another while loop
- 10-16. while loop with multiple conditions
- 10-17. c-like syntax in a while loop
- 10-18. until loop
- 10-19. nested loop
- 10-20. effects of break and continue in a loop
- 10-21. breaking out of multiple loop levels
- 10-22. continuing at a higher loop level
- 10-23. using "continue n" in an actual task
- 10-24. using case
- 10-25. creating menus using case
- 10-26. using command substitution to generate the case variable
- 10-27. simple string matching
- 10-28. checking for alphabetic input
- 10-29. creating menus using select
- 10-30. creating menus using select in a function
- 11-1. stupid script tricks
- 11-2. generating a variable from a loop
- 11-3. finding anagrams
- 14-1. a script that forks off multiple instances of itself
- 14-2. printf in action
- 14-3. variable assignment, using read
- 14-4. what happens when read has no variable
- 14-5. multi-line input to read
- 14-6. detecting the arrow keys
- 14-7. using read with file redirection
- 14-8. problems reading from a pipe
- 14-9. changing the current working directory
- 14-10. letting "let" do arithmetic.
- 14-11. showing the effect of eval
- 14-12. forcing a log-off
- 14-13. a version of "rot13"
- 14-14. using eval to force variable substitution in a perl script
- 14-15. using set with positional parameters
- 14-16. reversing the positional parameters
- 14-17. reassigning the positional parameters
- 14-18. "unsetting" a variable
- 14-19. using export to pass a variable to an embedded awk script
- 14-20. using getopts to read the options/arguments passed to a script
- 14-21. "including" a data file
- 14-22. a (useless) script that sources itself
- 14-23. effects of exec
- 14-24. a script that exec's itself
- 14-25. waiting for a process to finish before proceeding
- 14-26. a script that kills itself
- 15-1. using ls to create a table of contents for burning a cdr disk
- 15-2. hello or good-bye
- 15-3. badname, eliminate file names in current directory containing bad characters and whitespace.
- 15-4. deleting a file by its inode number
- 15-5. logfile: using xargs to monitor system log
- 15-6. copying files in current directory to another
- 15-7. killing processes by name
- 15-8. word frequency analysis using xargs
- 15-9. using expr
- 15-10. using date
- 15-11. word frequency analysis
- 15-12. which files are scripts?
- 15-13. generating 10-digit random numbers
- 15-14. using tail to monitor the system log
- 15-15. emulating "grep" in a script
- 15-16. looking up definitions in webster's 1913 dictionary
- 15-17. checking words in a list for validity
- 15-18. toupper: transforms a file to all uppercase.
- 15-19. lowercase: changes all filenames in working directory to lowercase.
- 15-20. du: dos to unix text file conversion.
- 15-21. rot13: rot13, ultra-weak encryption.
- 15-22. generating "crypto-quote" puzzles
- 15-23. formatted file listing.
- 15-24. using column to format a directory listing
- 15-25. nl: a self-numbering script.
- 15-26. manview: viewing formatted manpages
- 15-27. using cpio to move a directory tree
- 15-28. unpacking an rpm archive
- 15-29. stripping comments from c program files
- 15-30. exploring /usr/x11r6/bin
- 15-31. an "improved" strings command
- 15-32. using cmp to compare two files within a script.
- 15-33. basename and dirname
- 15-34. checking file integrity
- 15-35. uudecoding encoded files
- 15-36. finding out where to report a spammer
- 15-37. analyzing a spam domain
- 15-38. getting a stock quote
- 15-39. updating fc4
- 15-40. using ssh
- 15-41. a script that mails itself
- 15-42. monthly payment on a mortgage
- 15-43. base conversion
- 15-44. invoking bc using a "here document"
- 15-45. calculating pi
- 15-46. converting a decimal number to hexadecimal
- 15-47. factoring
- 15-48. calculating the hypotenuse of a triangle
- 15-49. using seq to generate loop arguments
- 15-50. letter count"
- 15-51. using getopt to parse command-line options
- 15-52. a script that copies itself
- 15-53. exercising dd
- 15-54. capturing keystrokes
- 15-55. securely deleting a file
- 15-56. filename generator
- 15-57. converting meters to miles
- 15-58. using m4
- 16-1. setting a new password
- 16-2. setting an erase character
- 16-3. secret password: turning off terminal echoing
- 16-4. keypress detection
- 16-5. checking a remote server for identd
- 16-6. pidof helps kill a process
- 16-7. checking a cd image
- 16-8. creating a filesystem in a file
- 16-9. adding a new hard drive
- 16-10. using umask to hide an output file from prying eyes
- 16-11. killall, from /etc/rc.d/init.d
- 18-1. broadcast: sends message to everyone logged in
- 18-2. dummyfile: creates a 2-line dummy file
- 18-3. multi-line message using cat
- 18-4. multi-line message, with tabs suppressed
- 18-5. here document with parameter substitution
- 18-6. upload a file pair to "sunsite" incoming directory
- 18-7. parameter substitution turned off
- 18-8. a script that generates another script
- 18-9. here documents and functions
- 18-10. "anonymous" here document
- 18-11. commenting out a block of code
- 18-12. a self-documenting script
- 18-13. prepending a line to a file
- 18-14. parsing a mailbox
- 19-1. redirecting stdin using exec
- 19-2. redirecting stdout using exec
- 19-3. redirecting both stdin and stdout in the same script with exec
- 19-4. avoiding a subshell
- 19-5. redirected while loop
- 19-6. alternate form of redirected while loop
- 19-7. redirected until loop
- 19-8. redirected for loop
- 19-9. redirected for loop (both stdin and stdout redirected)
- 19-10. redirected if/then test
- 19-11. data file "names.data" for above examples
- 19-12. logging events
- 20-1. variable scope in a subshell
- 20-2. list user profiles
- 20-3. running parallel processes in subshells
- 21-1. running a script in restricted mode
- 23-1. simple functions
- 23-2. function taking parameters
- 23-3. functions and command-line args passed to the script
- 23-4. passing an indirect reference to a function
- 23-5. dereferencing a parameter passed to a function
- 23-6. again, dereferencing a parameter passed to a function
- 23-7. maximum of two numbers
- 23-8. converting numbers to roman numerals
- 23-9. testing large return values in a function
- 23-10. comparing two large integers
- 23-11. real name from username
- 23-12. local variable visibility
- 23-13. recursion, using a local variable
- 23-14. the towers of hanoi
- 24-1. aliases within a script
- 24-2. unalias: setting and unsetting an alias
- 25-1. using an "and list" to test for command-line arguments
- 25-2. another command-line arg test using an "and list"
- 25-3. using "or lists" in combination with an "and list"
- 26-1. simple array usage
- 26-2. formatting a poem
- 26-3. various array operations
- 26-4. string operations on arrays
- 26-5. loading the contents of a script into an array
- 26-6. some special properties of arrays
- 26-7. of empty arrays and empty elements
- 26-8. initializing arrays
- 26-9. copying and concatenating arrays
- 26-10. more on concatenating arrays
- 26-11. an old friend: the bubble sort
- 26-12. embedded arrays and indirect references
- 26-13. complex array application: sieve of eratosthenes
- 26-14. emulating a push-down stack
- 26-15. complex array application: exploring a weird mathematical series
- 26-16. simulating a two-dimensional array, then tilting it
- 27-1. using /dev/tcp for troubleshooting
- 27-2. finding the process associated with a pid
- 27-3. on-line connect status
- 28-1. hiding the cookie jar
- 28-2. setting up a swapfile using /dev/zero
- 28-3. creating a ramdisk
- 29-1. a buggy script
- 29-2. missing keyword
- 29-3. test24, another buggy script
- 29-4. testing a condition with an "assert"
- 29-5. trapping at exit
- 29-6. cleaning up after control-c
- 29-7. tracing a variable
- 29-8. running multiple processes (on an smp box)
- 31-1. numerical and string comparison are not equivalent
- 31-2. subshell pitfalls
- 31-3. piping the output of echo to a read
- 33-1. shell wrapper
- 33-2. a slightly more complex shell wrapper
- 33-3. a generic shell wrapper that writes to a logfile
- 33-4. a shell wrapper around an awk script
- 33-5. a shell wrapper around another awk script
- 33-6. perl embedded in a bash script
- 33-7. bash and perl scripts combined
- 33-8. a (useless) script that recursively calls itself
- 33-9. a (useful) script that recursively calls itself
- 33-10. another (useful) script that recursively calls itself
- 33-11. a "colorized" address database
- 33-12. drawing a box
- 33-13. echoing colored text
- 33-14. a "horserace" game
- 33-15. return value trickery
- 33-16. even more return value trickery
- 33-17. passing and returning arrays
- 33-18. fun with anagrams
- 33-19. widgets invoked from a shell script
- 34-1. string expansion
- 34-2. indirect variable references - the new way
- 34-3. simple database application, using indirect variable referencing
- 34-4. using arrays and other miscellaneous trickery to deal four random hands from a deck of cards
- a-1. mailformat: formatting an e-mail message
- a-2. rn: a simple-minded file rename utility
- a-3. blank-rename: renames filenames containing blanks
- a-4. encryptedpw: uploading to an ftp site, using a locally encrypted password
- a-5. copy-cd: copying a data cd
- a-6. collatz series
- a-7. days-between: calculate number of days between two dates
- a-8. make a "dictionary"
- a-9. soundex conversion
- a-10. "game of life"
- a-11. data file for "game of life"
- a-12. behead: removing mail and news message headers
- a-13. ftpget: downloading files via ftp
- a-14. password: generating random 8-character passwords
- a-15. fifo: making daily backups, using named pipes
- a-16. generating prime numbers using the modulo operator
- a-17. tree: displaying a directory tree
- a-18. string functions: c-like string functions
- a-19. directory information
- a-20. object-oriented database
- a-21. library of hash functions
- a-22. colorizing text using hash functions
- a-23. more on hash functions
- a-24. mounting usb keychain storage devices
- a-25. preserving weblogs
- a-26. protecting literal strings
- a-27. unprotecting literal strings
- a-28. spammer identification
- a-29. spammer hunt
- a-30. making wget easier to use
- a-31. a "podcasting" script
- a-32. basics reviewed
- a-33. an expanded cd command
- c-1. counting letter occurrences
- k-1. sample .bashrc file
- l-1. viewdata.bat: dos batch file
- l-2. viewdata.sh: shell script conversion of viewdata.bat
- p-1. print the server environment