Bash Guide

  • View
    15

  • Download
    2

Embed Size (px)

DESCRIPTION

fhdfhgdfgh

Text of Bash Guide

  • BashGuide

    October 16, 2013

    1

  • Contents

    1 Introduction 51.1 About This Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.2 A Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.3 Using Bash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.4 Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    2 Commands and Arguments 82.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.2 Types of Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.3 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    3 Special Characters 16

    4 Parameters 184.1 Special Parameters and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.2 Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224.3 Parameter Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

    5 Patterns 265.1 Glob Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265.2 Extended Globs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285.3 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285.4 Brace Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

    6 TestsAndConditionals 316.1 Exit Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316.2 Control Operators (&& and ||) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326.3 Grouping Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336.4 Conditional Blocks (if, test and [[) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346.5 Conditional Loops (while, until and for) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396.6 Choices (case and select) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

    7 Arrays 467.1 Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467.2 Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497.3 Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    2

  • 8 Input and Output 538.1 Command-line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538.2 The Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548.3 File Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558.4 Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

    8.4.1 File Redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578.4.2 File Descriptor Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608.4.3 Heredocs And Herestrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

    8.5 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648.6 Miscellaneous Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

    8.6.1 Process Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

    9 Compound Commands 689.1 Subshells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689.2 Command grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689.3 Arithmetic Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709.4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719.5 Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739.6 Destroying Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

    10 Sourcing 75

    11 Job Control 7611.1 Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7611.2 Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7611.3 Job Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7711.4 See Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

    12 Practices 7812.1 Choose Your Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7812.2 Quoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7912.3 Readability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8012.4 Bash Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8212.5 Dont Ever Do These . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8412.6 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

    12.6.1 Diagnose the Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8612.6.2 Minimize the Codebase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8612.6.3 Activate Bashs Debug Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8712.6.4 Step Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8812.6.5 The Bash Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8812.6.6 Reread the Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

    3

  • 12.6.7 Read the FAQ / Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8912.6.8 Ask Us on IRC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

    4

  • 1 IntroductionYou are invited to make additions or modifications at http://mywiki.wooledge.org/BashGuide so long as youcan keep them accurate. Please test any code samples you write.

    All the information here is presented without any warranty or guarantee of accuracy. Use it at your own risk. When indoubt, please consult the man pages or the GNU info pages as the authoritative references.

    1.1 About This Guide

    This guide aims to aid people interested in learning to work with BASH1. It aspires to teach good practice techniques for usingBASH, and writing simple scripts.

    This guide is targeted at beginning users. It assumes no advanced knowledge -- just the ability to login to a Unix-likesystem and open a command-line (terminal) interface. It will help if you know how to use a text editor; we will not becovering editors, nor do we endorse any particular editor choice. Familiarity with the fundamental Unix tool set, or with otherprogramming languages or programming concepts, is not required, but those who have such knowledge may understand someof the examples more quickly.

    If something is unclear to you, you are invited to report this (use http://mywiki.wooledge.org/BashGuideFeedback,or the #bash channel on irc.freenode.org) so that it may be clarified in this document for future readers.

    You are invited to contribute to the development of this document by extending it or correcting invalid or incompleteinformation.

    The primary maintainer(s) of this document:

    -- Lhunath2 (primary author)

    -- GreyCat3

    1.2 A DefinitionBASH is an acronym for Bourne Again Shell. It is based on the Bourne shell and is mostly compatible with its features.

    Shells are command interpreters. They are applications that provide users with the ability to give commands to theiroperating system interactively, or to execute batches of commands quickly. In no way are they required for the execution ofprograms; they are merely a layer between system function calls and the user.

    Think of a shell as a way for you to speak to your system. Your system doesnt need it for most of its work, but it isan excellent interface between you and what your system can offer. It allows you to perform basic math, run basic tests andexecute applications. More importantly, it allows you to combine these operations and connect applications to each other toperform complex and automated tasks.

    BASH is not your operating system. It is not your window manager. It is not your terminal (but it oftens runs inside yourterminal). It does not control your mouse or keyboard. It does not configure your system, acti