Shell

Variables

Local variables

Environment variables

  • Each process has an "environment", that is, a group of variables that the process may reference.
  • Every time a shell starts, it creates shell variables that correspond to its own environmental variables.
  • Updating or adding new environmental variables causes the shell to update its environment, and all the shell's child processes (the commands it executes) inherit this environment.
  • A script can export variables only to child processes, that is, only to commands or processes which that particular script initiates.

Positional parameters

  • Arguments passed to the script from the command line: $0, $1, $2, $3 ...
  • $0 is the name of the script itself, $1 is the first argument, $2 the second, $3 the third, and so forth. After $9, the arguments must be enclosed in brackets, for example, ${10}, ${11}, ${12}.
  • The special variables $* and $@ denote all the positional parameters.

Redirection

Redirection

  • Redirection with >

    CommandDescription
    command > fileSends standard output to <file>
    command 2> fileSends error output to <file>
    command 2>&1Sends error output to standard output
    command > file 2>&1Sends standard output and the error output to a file
    command &> fileSends standard output and the error output to a file
    command 2>&1 > fileSends error output to standard input and the standard input to a file
  • Append with >>

    CommandDescription
    command >> fileAppends standard output to a file
    command 2>> fileAppends error output to a file
    command >> file 2>&1Appends standard output and error output to a file
    command &>> fileAppends standard output and error output to a file
    command 2>&1 >> fileSends error output to standard input and appends standard input to a file
  • Redirect with <

    CommandDescription
    command < $fileSends file contents to standard input
    command << $delimiterSends a command or interactive program with a list defined by a delimiter; this is known as a here-document (heredoc)
    command <<< $wordSends a command with word; this is known as a here-string
  • Resources

Pipeline

Pipeline

  • |

    cmd1 | cmd2

    cmd1 -> stdout -> stdin -> cmd2

    The output of each command in the pipeline is connected via a pipe to the input of the next command. That is, each command reads the previous command’s output. This connection is performed before any redirections specified by command1.

  • |&

    cmd1 -> (stdout & stderr) -> stdin -> cmd2

    If |& is used, command1’s stderr, in addition to its stdout, is connected to command2’s stdin through the pipe; it is shorthand for 2>&1 |. This implicit redirection of the stderr to the stdout is performed after any redirections specified by command1.

  • Resources

Pipeline - Cheatsheet

When you don't want to see the output in the terminal

  • >> /dev/null

Redirect both stdout and stderr to a file

  • <command> >>file.txt 2>&1

    Target file must exist before running the command.

Shell Scripting

Bash features

Shell Scripting - Cheatsheet

source vs execute a script

  • source

    • Run the script in the current shell
    • Use return to exit script
  • execute

    • Run the script in a new shell
    • Use exit to exit script

Loop through all files and directories under the current directory

  • for file in *; do echo "$file"; done

    Files and directories except those starting with .

  • for file in .*; do echo "$file"; done

    Files and directories starting with . only

Cheatsheet

Generate a sequence of numbers

for i in $(seq -f "%06g" 1 5); do echo $i; done
# Example
> for i in $(seq -f "%06g" 1 5); do echo $i; done
000001
000002
000003
000004
000005

Get the function arguments from the second to the last

Use shift to remove the first argument, and then $@ now contains the arguments from the second to the last.

example_function() {
  shift
  echo $@
}