Bash Guide for Beginners - tldp.org ??Bash is the GNU shell.....7 1.2.2. Features only found in bash.....7 1.3. Executing commands.....12 ... Bash Guide for Beginners tille wants no

  • View
    217

  • Download
    4

Embed Size (px)

Text of Bash Guide for Beginners - tldp.org ??Bash is the GNU shell.....7 1.2.2. Features only found in...

  • Bash Guide for Beginners

    Machtelt Garrels

    Garrels BVBA

    Version 1.11 Last updated 20081227 Edition

    mailto:tille wants no spam _at_ garrels dot be

  • Table of ContentsIntroduction.........................................................................................................................................................1

    1. Why this guide?...................................................................................................................................12. Who should read this book?.................................................................................................................13. New versions, translations and availability.........................................................................................24. Revision History..................................................................................................................................25. Contributions.......................................................................................................................................36. Feedback..............................................................................................................................................37. Copyright information.........................................................................................................................38. What do you need?...............................................................................................................................49. Conventions used in this document.....................................................................................................410. Organization of this document...........................................................................................................5

    Chapter 1. Bash and Bash scripts......................................................................................................................61.1. Common shell programs...................................................................................................................6

    1.1.1. General shell functions............................................................................................................61.1.2. Shell types...............................................................................................................................6

    1.2. Advantages of the Bourne Again SHell............................................................................................71.2.1. Bash is the GNU shell.............................................................................................................71.2.2. Features only found in bash.....................................................................................................7

    1.3. Executing commands......................................................................................................................121.3.1. General..................................................................................................................................121.3.2. Shell built-in commands........................................................................................................121.3.3. Executing programs from a script.........................................................................................13

    1.4. Building blocks...............................................................................................................................131.4.1. Shell building blocks.............................................................................................................13

    1.5. Developing good scripts.................................................................................................................151.5.1. Properties of good scripts......................................................................................................161.5.2. Structure................................................................................................................................161.5.3. Terminology..........................................................................................................................161.5.4. A word on order and logic.....................................................................................................161.5.5. An example Bash script: mysystem.sh..................................................................................171.5.6. Example init script.................................................................................................................18

    1.6. Summary.........................................................................................................................................191.7. Exercises.........................................................................................................................................19

    Chapter 2. Writing and debugging scripts.....................................................................................................212.1. Creating and running a script..........................................................................................................21

    2.1.1. Writing and naming...............................................................................................................212.1.2. script1.sh................................................................................................................................222.1.3. Executing the script...............................................................................................................23

    2.2. Script basics....................................................................................................................................242.2.1. Which shell will run the script?.............................................................................................242.2.2. Adding comments..................................................................................................................24

    2.3. Debugging Bash scripts..................................................................................................................252.3.1. Debugging on the entire script..............................................................................................252.3.2. Debugging on part(s) of the script.........................................................................................26

    2.4. Summary.........................................................................................................................................282.5. Exercises.........................................................................................................................................28

    Bash Guide for Beginners

    i

  • Table of ContentsChapter 3. The Bash environment..................................................................................................................29

    3.1. Shell initialization files...................................................................................................................293.1.1. System-wide configuration files............................................................................................293.1.2. Individual user configuration files........................................................................................313.1.3. Changing shell configuration files........................................................................................33

    3.2. Variables.........................................................................................................................................343.2.1. Types of variables.................................................................................................................343.2.2. Creating variables..................................................................................................................373.2.3. Exporting variables................................................................................................................383.2.4. Reserved variables.................................................................................................................393.2.5. Special parameters.................................................................................................................413.2.6. Script recycling with variables..............................................................................................43

    3.3. Quoting characters..........................................................................................................................443.3.1. Why?.....................................................................................................................................453.3.2. Escape characters..................................................................................................................453.3.3. Single quotes.........................................................................................................................453.3.4. Double quotes........................................................................................................................453.3.5. ANSI-C quoting....................................................................................................................463.3.6. Locales...................................................................................................................................46

    3.4. Shell expansion...............................................................................................................................463.4.1. General..................................................................................................................................463.4.2. Brace expansion.........................