Home / bash programming pdf / Bash Reference Manual

Bash Reference Manual - bash programming pdf


Bash Reference Manual-bash programming pdf

Bash Reference Manual
Reference Documentation for Bash
Edition 5.2, for Bash Version 5.2.
September 2022
Chet Ramey, Case Western Reserve University
Brian Fox, Free Software Foundation
This text is a brief description of the features that are present in the Bash shell (version
5.2, 19 September 2022).
This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual,
for Bash, Version 5.2.
Copyright c 1988-2022 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document under the
terms of the GNU Free Documentation License, Version 1.3 or any later version
published by the Free Software Foundation; with no Invariant Sections, no
Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included
in the section entitled "GNU Free Documentation License".
i
Table of Contents
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 What is Bash? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 What is a shell? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 Basic Shell Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1 Shell Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1.1 Shell Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1.2 Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1.2.1 Escape Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1.2.2 Single Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1.2.3 Double Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1.2.4 ANSI-C Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.1.2.5 Locale-Specific Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.3 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Shell Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2.1 Reserved Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2.2 Simple Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2.3 Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2.4 Lists of Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2.5 Compound Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2.5.1 Looping Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2.5.2 Conditional Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.2.5.3 Grouping Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2.6 Coprocesses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.2.7 GNU Parallel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.3 Shell Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4 Shell Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.4.1 Positional Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.4.2 Special Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.5 Shell Expansions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.5.1 Brace Expansion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.5.2 Tilde Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.5.3 Shell Parameter Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.5.4 Command Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.5.5 Arithmetic Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.5.6 Process Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.5.7 Word Splitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5.8 Filename Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.5.8.1 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.5.9 Quote Removal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

How to program with Bash?Writing a Simple Bash ScriptStarting Off. The “#!” combo is called a shebang by most Unix geeks. ...Variables. The above script is useful, but it has hard-coded paths. ...Taking Input. Non-interactive scripts are useful, but what if you need to give the script new information each time it’s run?