Bash Shell Scripting
Crash Course
crowfly.net

<<   >>

Introduction

GNU_Tools

Example1

Example2

Execution_bit

Bang_bin_bash

Example3

The_dot

Example4

Example5

Example6

Example7

Example8

Background

Forking

Example9

Bash_profile

Pattern_match

Regex

Debug

Gory_details

References

<<   >>
Forking

When the shell asks Unix to run a command, it first forks the existing shell, cloning all its environment variables.

  dlink@viddev1> ps -f
  UID        PID  PPID  C STIME TTY          TIME CMD
  dlink    20837 20830  0 09:48 pts/0    00:00:01 -bash
  dlink    25541 20837  0 11:14 pts/0    00:00:48 emacs -T E viddev1 .
  dlink    22264 20837  0 18:40 pts/0    00:00:00 ps -f

Given a script that looks like this:

s1:

  do_one
  do_two

1.) You can run a command in the current shell, using dot space:

            .------.      .--------.     .--------.
  shell:    | . s1 | ---> | do_one | --> | do_two | --->
            `------'      `--------'     `--------'

  This is useful so that variables that you set in s1 remain set in your shell when the script ends.

2.) You let Unix fork a new subshell, this is the normal way:

            .------.   
  shell:    |  s1  | -.                              .-->
            `------'  |                              |
                      |   .--------.     .--------.  |
  subshell:           `-> | do_one | --> | do_two | -'
                          `--------'     `--------'

  Here you have to wait for s1 to complete before you can work again in the parent shell.

3.) You let Unix fork a new subshell, and release the parent, using the &:

            .------.   
  shell:    | s1 & | -.- - - - - - - - - - - - - - - .-->
            `------'  |                              |
                      |   .--------.     .--------.  |
  subshell:           `-> | do_one | --> | do_two | -'
                          `--------'     `--------'

  Here you do not have to wait for s1 to complete before you can work again in the parent shell.