Jump to content

Bash Shell Scripting/How To Read

From Wikibooks, open books for an open world

(1) It's ideal to practice Bash while reading

[edit | edit source]

You will probably find this book most useful if you have a Bash command line open in another window while you are going through it, so you can try the examples. More important is to try things out that aren't just copied from the book. If you think something probably works a certain way, test out your expectations!

Except where noted otherwise, the examples in this book do not require any special privileges or permissions, and do not require any software aside from Bash itself and very common utilities that are generally found on any system where Bash is.

(2) Try consulting documentation for unknown commands

[edit | edit source]

When you are experimenting, it will be helpful to consult the documentation for various commands. For commands that are built in to Bash, you can use the built-in help command. For example, help echo will display information about the built-in echo command, which is used to print text to the screen.

For programs that are not built-in, it is likely that their manual pages are installed on your system, in which you can view using the man ("manual") command. For example, type man cp for information about the cp ("copy") command. In order to navigate the man page viewer, use the arrow keys, spacebar, PgUp, and PgDn keys to navigate the manual page; press h for more help and q to quit.

Finally, most programs have a -h or --help option, which when supplied to the program as an argument, will print some usage information. In some cases (for example, cp --help), you get almost as much information as the man page, though generally -h / --help serves only as a quick reference.

(3) Warning against blindly entering commands

[edit | edit source]

A word of caution: this book is part of a wiki, which means that anyone may have edited it. It is possible — unlikely, but possible — that someone has added an "example" that is actually malicious code, or inserted malicious code into an existing example. Trust your instincts; if an example seems suspicious to you, if the surrounding text doesn't adequately explain what it does, if it doesn't seem to match the description in the surrounding text, then don't run it.

Even if it's clear that an example is non-malicious, it's a good idea to run all of your experiments using an account that has no special privileges or permissions on the system, so you don't (say) accidentally delete something important. Bash is a very powerful systems administration tool, which sometimes means it's a powerful way to accidentally cripple your system! Virtual machines, containers, and other types of sandboxed environments are ideally-suited for this purpose, because if you make a mistake you can (either try to recover from it, or) restore a VM snapshot or pull a new container image and start over.

(4) How to proceed from here

[edit | edit source]

If you are completely new to Bash, or if you have some experience using the command line but are new to programming concepts like loops and conditionals, you will probably find this book most useful if you begin at the beginning, and work your way through in linear order. If the topic of any section is completely familiar to you, you might want to skim it quickly for possible surprises, rather than skipping it completely; after all, you may not know what you may not know.

If you already have some Bash experience and some programming experience, you can probably skim the entire first half of the book, focusing only on examples that show Bash syntax you aren't already familiar with. That done, you should be in a good position to get the most out of the second half of the book.

If you already have a lot of Bash shell-scripting experience, this may not be the book for you; you will probably learn some things, but not as much as you would learn from the Bash Reference Manual on the Free Software Foundation's web-site, or simply from reading the entirety of man bash. (But this being a wiki, you are invited to share the fruits of your experience!) This book is not a comprehensive reference manual; it focuses on techniques that are either widely used or widely useful, and mostly assumes a typical setup rather than exhaustively documenting all the unusual features that can affect the behavior of a command.