This is a field guide to bash and Unix-like commands.

The bash shell provides a human-readable environment and command language interpreter for the Linux kernel. It is one of many shells. It can pass in both its own commands and Unix-like commands.

Bash shells can either be entered in the command line or passed in a script. When executing a bash script, bash forks a new bash process, which then reads, interprets, and executes each line linearly. The process terminates when there are no more lines, wakes up the main process, and prompts the user for a new command.

Here is a basic syntax for a shell script:

  1. !#/bin/bash # Shebang Magic Number

    The shebang byte string, a magic number, allows an external bash script file to be evaluated as a bash script.

    The shell is executed in the execution environment.

  2. # Comment
    # Comment

    This is a comment.

    A word beginning with “#” causes it and all remaining characters to be ignored if the interactive_comments option is enabled.

  3. exampleCommand
    # Command Call

    Commands are sequences of words separated by blank spaces (spaces or tabs), terminating in a control operator:

    • ||
    • &&
    • &
    • ;
    • ;;
    • |
    • |&
    • (
    • )

    Commands exit with an exit status.

  4. exampleCommand1 | exampleCommand2 | exampleCommand3
    # Pipeline

    Commands separated by a vertical bar form a pipeline.

    Each command in a pipeline sends its standard output to the standard input of the next command.

    The pipeline exits with the return status of the last command. If an exclamation mark precedes the pipeline, it outputs with the logical negation of the last return status. If the reserved word “time” precedes the pipeline, timing statistics for the pipeline are printed.

  5. exampleCommand1 |& exampleCommand2 |& exampleCommand3
    # Pipeline with Standard Error

    Commands separated by a vertical bar and ampersand are pipelines where the preceding command sends both its standard output and standard error th the standard input of the next command.

    It is semantically equivalent to “2>&1”.

  6. exampleCommand1 | exampleCommand2 || exampleCommand3
    # List of Commands

    Commands and pipelines separated by the operators:

    • &&
    • ||
    • ;
    • &

    are lists.

    “&&” and “||” have equal, highest precedence. “;” and “&” have equal, lesser precedence. Within that, precedence is read left to right.

  7. exampleCommand1 && exampleCommand2
    # Conditional AND List Command Execution

    The “&&” operator makes multiple commands on the same line execute sequentially only if the return status of the preceding command is zero.

  8. exampleCommand1 || exampleCommand2
    # Conditional OR List Command Execution

    The “&&” operator makes multiple commands on the same line execute sequentially only if the return status of the preceding command is non-zero.

  9. exampleCommand1; exampleCommand2
    # Sequential List Command Execution

    The “;” operator makes multiple commands on the same line execute sequentially.

  10. exampleCommand1 & exampleCommand2
    # Concurrent/Subshell List Command Execution

    The “&” runs exampleCommand1 in a background subshell, thus allowing the two commands to execute concurrently.

    It forks processes. It can be used on its: “exampleCommand1 &” is perfectly valid.

    See also coprocesses and GNU Parallel.

  11. ( exampleCommand1 & exampleCommand2 )
    # Grouped Commands

    Commands between parantheses are performed as a group within a subshell.

    Variable assignments within parantheses are not remembered.

  12. { exampleCommand1 & exampleCommand2; }
    # Grouped Commands

    Commands between curly braces are performed as a group within the current shell.

    Variable assignments within curly braces are remembered.

  13. exampleFunctionName () { exampleCommand1 & exampleCommand2 }
    # Function Declaration

    This command creates a function name for grouped commands and executes them when that function name is called.

  14. function exampleFunctionName { exampleCommand1 & exampleCommand2 }
    # Alternate Function Declaration
  15. if exampleCommand1;
    # “if” Conditional Construct

    This construct performs a command or group which produces a return status.

  16. then exampleCommand2;
    # “then” Construct

    This construct performs its commands if the evaluating command returns a non-zero status.

  17. elif exampleCommand3;
    # “elif” Construct

    This construct performs a command or group if the previous evaluating command returns zero.

  18. then exampleCommand4;
  19. else exampleCommand5;
    # “else” Construct

    This construct performs its commands if all the previous evaluating commands return zero.

  20. fi
    # “fi” Closing “if” Construct

    This construct closes the “if” command body.

  21. case exampleWord in
    # “case” Conditional Construct

    This construct executes the commands in the clause corresponding to the first examplePattern that matches the exampleWord.

    The word goes through tilde, arithmetic, and parameter expansion, quote removal, and command substituion before matchin.

  22. examplePattern1 | examplePattern2 | examplePattern3) exampleCommand;;
    # “;;” Case Construct Operator

    A clause terminated by this operator causes the “case” construct to cease executing after the clause is executed.

  23. examplePattern4 | examplePattern5 | examplePattern6) exampleCommand;&
    # “;&” Case Construct Operator

    A clause terminated by this operator causes the “case” construct to execute the next commands associated with the next clause before stopping.

  24. examplePattern7 | examplePattern8 | examplePattern9) exampleCommand;;&
    # “;;&” Case Construct Operator

    A clause terminated by this operator causes the “case” construct to test and – if successful – execute the commands in the next clause.

  25. esac
    # “esac” Closing Case Construct

  26. select exampleName in exampleWords1;
    # “select” Conditional Construct

    This construct selects a list of words, prints the on the standard error output stream, compares them to the standard input stream, and outputs any matching lines corresponding to the exampleName, until EOF is read.

  27. do
    # “do” Select Construct

    This construct begins the commands to be executed when a “select” match is found.

  28. exampleCommand;
  29. done
    # “done” Closing “Do” Construct

    This construct ends the commands to be executed when a “select” match is found.

  30. until exampleCommand1; do exampleCommand2; done;
    # “until” Looping Construct

    This construct executes commands as long as the evaluating command has a non-zero return status.

    If no commands are executed, the return status is zero.

  31. while exampleCommand1; do exampleCommand2; done
    # “while” Looping Construct

    This construct executes commands as long as the evaluating command has a zero return status.

    If no commands are executed, the return status is zero.

  32. for exampleName in exampleWords; do exampleCommand; done
    # “for” Looping Construct

    This construct, sort of like the “select” construct, executes commands once for each member in the list of words, setting exampleName to be the current member.

    The return status is the exit status of the last executed command.

  33. for (( expr1; expr2; expr3 )); do exampleCommand; done
    # “for” Alternate Looping Construct

    This construct evaluates expr1, then repeatedly evaluates expr2 until it evaluates to zero. Each time expr2 evaluates to a non-zero value, exampleCommand (or commands) are evaluated and expr3 (an arithmetic expression) is evaluated.

    The return value is the exit status of the last command in exampleCommand to be executed, or non-zero if any expression is invalid.

  34. coproc exampleName exampleCommand
    # “coproc” Asynchronous Process Command

    This command is a shell command that executes asynchronously in a subshell, like the terminating “&” operator.

    The name of the coprocess subshell is exampleName, or “COPROC” if none is supplied.

  35. exampleCommand1 | parallel exampleCommand2
    # “parallel” Asynchronous GNU Parallel Command

    This command builds and runs commands in parallel.

  36. exampleCommand -option1 -option2 argument1 argument2
    # Command with Options and Arguments

    Commands can be run by declaring their name.

  37. file=”exampleFile”
    #


  38. #

Here are some useful bash and Linux commands: