Definition:

How to read synopsis?

  • bold text type exactly as shown.
  • italic text replace with appropriate argument.
  • [-abc] any or all arguments within [ ] are optional.
  • -a|-b options delimited by | cannot be used together.
  • argument … argument is repeatable.
  • [expression] … entire expression within [ ] is repeatable.

1. Introduction

2. Definitions

3. Basic shell features

3.1 Syntax

Shell Operations
Quoting
  • Escape character: \, It preserves the literal value of the next character that follows, with the exception of newline
  • Single Quotes: (‘’’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
  • Double Quotes: (‘”’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’.
Comments
  • a word beginning with ‘#’ causes that word and all remaining characters on that line to be ignored.
  • An interactive shell without the interactive_comments option enabled does not allow comments.
  • The interactive_comments option is on by default in interactive shells.

3.3 Shell functions

  • fname () compound-command [ redirections ]
    • or
    • function fname [()] compound-command [ redirections ]
  • arguments are passed like in command-line

3.7 Executing commands

3.7.5 Exit status

3.8. Shell scripts

  • The shell first searches for the file in the current directory, and looks in the directories in $PATH if not found there.
  • otherwise, run with ./myscript.sh

4. Bash shell builtin commands

4.1. Bourne shell builtins

  • Inherit from Bourne shell
  • :
  • source, .
    • . filename [arguments]
    • Read and execute commands from the filename argument in the current shell context
      • so env vars and context will be update, whereas running script alone will not effect current shell
  • break
  • cd: similar to cd command
  • exec:
    • exec [-cl] [-a name] [command [arguments]]
    • If command is supplied, it replaces the shell without creating a new process.
    • can be useful to prevent user from returning to the parent process if an error is encountered
    • If no command is specified, redirections may be used to affect the current shell environment.
      • redirect stdout to a file
      #!/bin/bash
      exec > out.txt
      pwd
      ls -al
  • exit
  • export command
  • ..
  • pwd command
  • umask command
  • echo:
    • Output the args, separated by spaces, terminated with a newline.
    • similar to echo command
  • read:
    • One line is read from the standard input, assigned to name

4.2. Bash builtin commands

3.3.

5. Shell variables

6. Bash features

6.7 Arrays

  • declare command
    • declare -a name[subscript]
  • name[subscript]=value
    • my_arr=(1 "Hello" 3.1)
  • reference all of the values in an array by using the asterisk (*) or at symbol (@) in place of the index: echo ${my_arr[*]}

9. Using History Interactively

  • history command
  • When the shell starts up, the history is initialized from the file named by the HISTFILE variable (default ~/.bash_history)
  • run history to see the command history and !nb (ex: !2) to execute that command
  • !! is last command
    • ex: forgot to sudo, rerun with sudo !!