19 KiB
bash4NOTOC
DISCUSSION:off
====== The Bash Hackers Wiki ======
{{keywords>bash shell linux scripting}}
<WRAP center round box 90%> This wiki is intended to hold documentation of any kind about GNU Bash. The main motivation was to provide //human-readable documentation// and information so users aren't forced to read every bit of the Bash manpage - which can be difficult to understand. However, the docs here are not meant as a newbie tutorial.
This wiki and any programs found in this wiki are free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This wiki and its programs are distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
wishes (outdated and locked, please use the discussions)
Stranger! Feel free to register and comment or edit the contents. There is a Bash Hackers Wiki needs love page that lists some things to do. The registration is only there to prevent SPAM. </WRAP>
===== Scripting and general information =====
- bash4 (unmaintained, since Bash 4 is more or less standard)
- scripting:style -- an assorted collection of style and optic hints
- scripting:basics
- scripting:newbie_traps
- scripting:bashbehaviour
- scripting:posparams
- scripting:processtree
- scripting:obsolete
- scripting:nonportable
- scripting:debuggingtips
- scripting:terminalcodes
- scripting:tutoriallist
===== Code snippets ====
There is a section that holds small code snippets.
See also some Bash source code excerpts.
===== How to.... ===== Doing specific tasks: concepts, methods, ideas:
- Simple locking (against parallel run)
- Rudimentary config files for your scripts
- Editing files with ed(1)
- Collapsing Functions
- Illustrated Redirection Tutorial
- Calculate with dc(1)
- Introduction to pax - the POSIX archiver
- howto:getopts_tutorial (//under construction!//)
- howto:dissectabadoneliner An example of a bad oneliner, breakdown and fix (by ''kojoro'')
- Write tests for ./your-script.sh by using bashtest util
===== Bash syntax and operations =====
- Bash features overview by version
- Basic grammar rules
- Quoting and character escaping
- Parsing and execution
- Some words about words...
- Patterns and pattern matching
- Arithmetic expressions
- List of shell options
- Redirection
- Special parameters and shell variables
- Arrays
<WRAP column 40%> //Compound commands//
^ Compound commands overview ^^ ^Grouping ^^ |''{ ...; }''| command grouping| |''( ... )''| command grouping in a subshell| ^Conditionals ^^ |''<nowiki> ... </nowiki>''| conditional expression| |''if ...; then ...; fi''| conditional branching| |''case ... esac''| pattern-based branching| ^Loops ^^ |''for word in ...; do ...; done''| classic for-loop| |''<nowiki>for ((x=1; x<=10; x++)); do ...; done</nowiki>''| C-style for-loop| |''while ...; do ...; done''| while loop| |''until ...; do ...; done''| until loop| ^Misc ^^ |''<nowiki>(( ... ))</nowiki>''| arithmetic evaluation| |''select word in ...; do ...; done''| user selections| </WRAP>
<WRAP column 40%> //Expansions and substitutions//
^ Introduction to expansions and substitutions ^^
|''{A,B,C} {A..C}''| Brace expansion|
|''~/ ~root/''| Tilde expansion|
|''$FOO ${BAR%.mp3}''| Parameter expansion|
|''command
$(command)''| Command substitution|
|''<(command) >(command)''| Process substitution|
|''<nowiki>$((1 + 2 + 3)) $[4 + 5 + 6]</nowiki>''| Arithmetic expansion|
|''Hello <nowiki><---></nowiki> Word!''| Word splitting|
|''/data/-av/.mp?''| Pathname expansion|
</WRAP>
<WRAP clear></WRAP>
===== Builtin Commands =====
This is a selection of builtin commands and command-like keywords, loosely arranged by their common uses. These are provided directly by the shell, rather than invoked as standalone external commands.
<WRAP column 46%> ^ Declaration commands\ <wrap center round lo todo box 80%>Commands that set and query attributes/types, and manipulate simple datastructures.</wrap> ^^ Alt ^ Type ^ | declare|Display or set shell variables or functions along with attributes.| ''typeset'' | builtin | | export|Display or set shell variables, also giving them the export attribute.| ''typeset -x'' | special builtin | | eval|Evaluate arguments as shell code.| - | special builtin | | local|Declare variables as having function local scope.| - | builtin | | readonly|Mark variables or functions as read-only.| ''typeset -r'' | special builtin | | unset|Unset variables and functions.| - | special builtin | | shift|Shift positional parameters| - | special builtin | ^ I/O\ <wrap center round lo todo box 80%>Commands for reading/parsing input, or producing/formatting output of standard streams.</wrap> ^^ Alt ^ Type ^ | coproc|Co-processes: Run a command in the background with pipes for reading / writing its standard streams.| - | keyword | | echo|Create output from arguments.| - | builtin | | mapfile|Read lines of input into an array.| ''readarray'' | builtin | | printf|"advanced ''echo''."| - | builtin | | read|Read input into variables or arrays, or split strings into fields using delimiters.| - | builtin | ^ Configuration and Debugging\ <wrap center round lo todo box 80%>Commands that modify shell behavior, change special options, assist in debugging.</wrap> ^^ Alt ^ Type ^ | caller|Identify/print execution frames.| - | builtin | | set|Set the positional parameters and/or set options that affect shell behaviour.| - | special builtin | | shopt|set/get some bash-specific shell options.| - | builtin | </WRAP>
<WRAP column 46%> ^ Control flow and data processing\ <wrap center round lo todo box 80%>Commands that operate on data and/or affect control flow.</wrap> |^ Alt ^ Type ^ | colon | "true" null command. | true | special builtin | | dot | Source external files. | source | special builtin | | false | Fail at doing nothing. | - | builtin | | continue / break | continue with or break out of loops. | - | special builtin | | let | Arithmetic evaluation simple command. | - | builtin | | return | Return from a function with a specified exit status. | - | special builtin | | [ | The classic ''test'' simple command. | test | builtin | ^ Process and Job control\ <wrap center round lo todo box 80%>Commands related to jobs, signals, process groups, subshells.</wrap> |^ Alt ^ Type ^ | exec | Replace the current shell process or set redirections. | - | special builtin | | exit | Exit the shell. | - | special builtin | | kill | Send a signal to specified process(es) | - | builtin | | trap | Set signal handlers or output the current handlers. | - | special builtin | | times | Display process times. | - | special builtin | | wait | Wait for background jobs and asynchronous lists. | - | builtin | ^ |^ Alt ^ Type ^ </WRAP> <WRAP clear></WRAP> <WRAP hide> previous alphabetical version <WRAP column 40%> ^A-G^^ | caller|identify/print execution frames (Bash builtin) | | coproc|Co-processes (Bash keyword) | | declare|display or set shell variables or functions along with attributes (Bash builtin, synonym: ''typeset'') | | exec | replace the shell, set redirections (Bash builtin) | ^I-N^^ | let|arithmetic evaluation - an old fashioned way (Bash builtin) | | mapfile|Mapping lines of input to an array, also named ''readarray'' (Bash builtin) | </WRAP>
<WRAP column 40%> ^O-T^^ | printf|"advanced ''echo''" (Bash builtin) | | read|getting input from ''stdin'' (Bash builtin) | | readonly|mark variables or functions as read-only (Bash builtin) | | set|control positional parameters and shell behaviour (Bash builtin) | | shopt|set/get shell options (Bash builtin) | | test|the classic ''test'' command (Bash builtin) | ^U-Z^^ | unset|unset variables and functions (Bash builtin) | </WRAP> <WRAP clear></WRAP> </WRAP> ===== Dictionary ======
<note tip>A list of expressions, words, and their meanings is here.</note>
===== Links =====
==== Official Bash links ====
- Chet Ramey's Bash page and its FAQ.
- GNU Bash software page
- Official Bash mailing lists:
- Official Bash git repository:
- Browse: cgit
- Clone: //git:// <nowiki>ssh://git.sv.gnu.org/srv/git/bash.git</nowiki> • //ssh:// <nowiki>ssh://git.sv.gnu.org/srv/git/bash.git</nowiki> • //http:// <nowiki>http://git.savannah.gnu.org/r/bash.git</nowiki>
==== Recommended Shell resources ====
- Greg's wiki - Greg Wooledge's (aka "greycat") wiki -- with MASSIVE information about Bash and UNIX(r) in general.
- Sven Mascheck's pages - A goldmine of information. A must-read.
- #ksh channel page - #ksh Freenode channel page maintains a solid collection of recommended links.
- The Grymoire Unix pages - Good scripting information, especially read the quoting guide.
- Heiner's "Shell Dorado" - Tips, tricks, links - for every situation.
- The Single Unix Specification (version 4, aka POSIX-2008)
- The Austin Group - http://dir.gmane.org/gmane.comp.standards.posix.austin.general , http://austingroupbugs.net/main_page.php
- comp.unix.shell FAQ
- Advanced Bash-Scripting Guide - last update: 10 Mar 2014, but still very useful guide.
=== Documentation / Reference ===
- Bash: man page info page
- AT&T ksh: ksh88 ksh93
- mksh (pdksh successor)
- zsh
- dash
- Heirloom Bourne shell
- Thompson shell
==== Assorted interesting links ====
-
History and development of the traditional Bourne shell family - very interesting and nice to read!
-
Interview with Steve Bourne • Stephen Bourne - BSDCan 2015 keynote
-
Linux in general, with some shell related stuff: nixCraft: Linux Tips, Hacks, Tutorials and Ideas
-
Linux tutorials, guides and how-tos: RoseHosting Blog, bash script for installing WordPress and some basic shell commands
-
How To Make an Awesome Custom Shell with ZSH ==== Bash Libraries (needs review) ====
-
An oo-style bash library for bash 4 - provides tools for rapid script development and huge libraries.
-
General purpose shell framework for bash 4 - in development.
-
General purpose bash library for bash 4 - active development
<div hide> ===== Most wanted =====
^Statistics for Month: April 2012^^^ |Rank | Views | Article | ^1 | 6833|howto:getopts_tutorial (March: 11663) | ^2 | 4025|commands:builtin:printf (March: 7079) | ^3 | 2861|syntax:quoting (March: 5730) | ^4 | 2854|commands:classictest (March: 6145) | ^5 | 2222|syntax:pe (March: 4540) | ^6 | 1760|scripting:posparams (March: 3591) | ^7 | 1249|bash4 (March: --) | ^8 | 1212|howto:edit-ed (March: 2727) | ^9 | 1205|commands:builtin:read (March: --) | ^10 | 1182|howto:mutex (March: --) |
- "Total visits": 35144
- The overall "start" page is not counted </div> ===== Contact =====
<WRAP column 40%> Visit us in ''irc.freenode.net'', channel ''#bash'' ;-)
If you have critiques or suggestions, please feel free to send a mail using the contact form on the right. Note that there is a simple discussion option below every article.
Please also see the imprint if you have problems with the site and its contents (legality, ...)!
It also would be nice to drop a line when
- it helped you
- it didn't help you (something missing / unclear)
- you like it
- you don't like it
- you found mistakes / bugs Simply: Reader's feedback </WRAP>
<WRAP column 40%> {{contact>subj=Wiki reader feedback}} </WRAP>