24.07.2018 Views

Bash-Beginners-Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

Table of Contents<br />

Introduction.........................................................................................................................................................1<br />

1. Why this guide?...................................................................................................................................1<br />

2. Who should read this book?.................................................................................................................1<br />

3. New versions, translations and availability.........................................................................................2<br />

4. Revision History..................................................................................................................................2<br />

5. Contributions.......................................................................................................................................3<br />

6. Feedback..............................................................................................................................................3<br />

7. Copyright information.........................................................................................................................3<br />

8. What do you need?...............................................................................................................................4<br />

9. Conventions used in this document.....................................................................................................4<br />

10. Organization of this document...........................................................................................................5<br />

Chapter 1. <strong>Bash</strong> and <strong>Bash</strong> scripts......................................................................................................................6<br />

1.1. Common shell programs...................................................................................................................6<br />

1.1.1. General shell functions............................................................................................................6<br />

1.1.2. Shell types...............................................................................................................................6<br />

1.2. Advantages of the Bourne Again SHell............................................................................................7<br />

1.2.1. <strong>Bash</strong> is the GNU shell.............................................................................................................7<br />

1.2.2. Features only found in bash.....................................................................................................7<br />

1.3. Executing commands......................................................................................................................12<br />

1.3.1. General..................................................................................................................................12<br />

1.3.2. Shell built-in commands........................................................................................................12<br />

1.3.3. Executing programs from a script.........................................................................................13<br />

1.4. Building blocks...............................................................................................................................13<br />

1.4.1. Shell building blocks.............................................................................................................13<br />

1.5. Developing good scripts.................................................................................................................15<br />

1.5.1. Properties of good scripts......................................................................................................16<br />

1.5.2. Structure................................................................................................................................16<br />

1.5.3. Terminology..........................................................................................................................16<br />

1.5.4. A word on order and logic.....................................................................................................16<br />

1.5.5. An example <strong>Bash</strong> script: mysystem.sh..................................................................................17<br />

1.5.6. Example init script.................................................................................................................18<br />

1.6. Summary.........................................................................................................................................19<br />

1.7. Exercises.........................................................................................................................................19<br />

Chapter 2. Writing and debugging scripts.....................................................................................................21<br />

2.1. Creating and running a script..........................................................................................................21<br />

2.1.1. Writing and naming...............................................................................................................21<br />

2.1.2. script1.sh................................................................................................................................22<br />

2.1.3. Executing the script...............................................................................................................23<br />

2.2. Script basics....................................................................................................................................24<br />

2.2.1. Which shell will run the script?.............................................................................................24<br />

2.2.2. Adding comments..................................................................................................................24<br />

2.3. Debugging <strong>Bash</strong> scripts..................................................................................................................25<br />

2.3.1. Debugging on the entire script..............................................................................................25<br />

2.3.2. Debugging on part(s) of the script.........................................................................................26<br />

2.4. Summary.........................................................................................................................................28<br />

2.5. Exercises.........................................................................................................................................28<br />

i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!