// How to start with Bash

Writing Bash scripts is fundamental to automate and understand many tasks on common Unix-like environments. If you are new to Bash (or even Unix-like systems) you'll have it a lot easier if you take a weekend to learn some fundamental principles before starting to script your stuff. No matter how experienced you are in programming, everything you'll find regarding Bash is much easier to follow and understand if you know some things before.

High quality online readings

IMHO, the following resources are good starting points:

  1. Linux Shell Scripting Tutorial (LSST) :!:Extremely good resource, especially for beginners.
  2. Bash FAQ – Many answers to frequently asked questions.
  3. The official GNU bash manual – Reference work. If you are generally familiar with Bash/*ix or simply search for a specific thing: look at the manual. Nearly everything should be documented there.

Things you should understand before you write serious scripts

To make it much easier for yourself, make sure you understand1) the following (the resources mentioned above will help you):

  1. Streams and redirection
    Standard Input (stdin, 0), Standard Output (stdout, 1) and Standard Error (stderr, 2). It is really fundamental to understand the stream concept. How streams are working. And how to manipulate, redirect and merge them. This is particularly important before writing much code or libraries. Otherwise you may have to change many things over and over again to make your scripts are interacting smoothly with the system (→ error logging etc.).
    Example of things you should be able to do blindfolded:
    • merge foobar's stderr (2) into foobar's stdout (1):
      foobar 2>&1
    • If you redirect foobar's stdout (1) into a file now, all error messages will also be written into the file because stderr (2) is merged into stdout (1):
      foobar > ~./logfile.log 2>&1
    • If you understand the two steps above, it is clear what the following does:
      echo "message" 1>&2

      Explanation: echo prints something to stdout (1). 1>&2 merges stdout into stderr. Therefore “message” will be printed as error to stderr(2) instead of creating simple standard output – this is the way to go if you want to print a simple error message. You also need redirections if you want to use sudo.

  2. Exit codes
    This is particularly important before writing much code to make sure your scripts are interacting smoothly with other applications.
  3. Pipes and Filters
  4. Subshells and the so called Subshell Problematic
  5. Background processes
    E.g. how to start them (→ &).
  6. Globbing and the Internal Field Separator (IFS)
    Make sure you be able to change and restore the IFS. And know how to use find and why you need it.
  7. Regular Expressions and useful tools
    The sed, cut, grep and find commands are available on nearly every system and are very helpful when you need to edit, find, manipulate or create files and data.
  8. Learn how to detect if you are root
    if [ "$(id -u)" != "0" ]
    then
        echo "This script needs root privileges!" 1>&2
        exit 1
    fi
  9. Learn how to define functions
  10. Here Documents
    See the Here Documents at the LSST for details.

Last but not least: find a project

You'll learn the most if your scripts are doing real jobs. Create a backup script. Or try to configure your whole desktop by script to make things comfortable on freshly installed systems.

1)
at least: know when you will need them and where you can look it up. Or better: be able to do the things blindfolded.

Leave a comment…




F᠎ U᠎ Z M᠎ L
  • E-Mail address will not be published.
  • Formatting:
    //italic//  __underlined__
    **bold**  ''preformatted''
  • Links:
    [[http://example.com]]
    [[http://example.com|Link Text]]
  • Quotation:
    > This is a quote. Don't forget the space in front of the text: "> "
  • Code:
    <code>This is unspecific source code</code>
    <code [lang]>This is specifc [lang] code</code>
    <code php><?php echo 'example'; ?></code>
    Available: html, css, javascript, bash, cpp, …
  • Lists:
    Indent your text by two spaces and use a * for
    each unordered list item or a - for ordered ones.
I'm no native speaker (English)
Please let me know if you find any errors (I want to improve my English skills). Thank you!
QR Code: URL of current page
QR Code: URL of current page 2010:10:10:how-to-start-with-bash (generated for current page)