Differences between revisions 2 and 8 (spanning 6 versions)
Revision 2 as of 2008-07-06 07:27:59
Size: 7560
Editor: Lhunath
Comment:
Revision 8 as of 2008-07-07 07:41:20
Size: 10502
Editor: Lhunath
Comment: Fix up headers.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Concise Bash Reference Sheet = = Bash Reference Sheet =
Line 3: Line 3:
 '''Work in progress''' == Table Of Contents ==
Line 5: Line 5:
== Builtins == [[TableOfContents]]



== Basic Structures ==

=== Compound Commands ===

Compound commands are statements that can execute several commands but are considdered as a sort of command group by Bash.

==== Command Lists ====
 * `{` ''[command list]''; `}`: '''Execute the list of commands in the current shell'''.
 * `(`''[command list]''`)`: '''Execute the list of commands in a subshell'''.

==== Expressions ====
 * `((`''[arithmetic expression]''`))`: '''Evaluates the given ''expression'' in an arithmetic context'''. That means, strings are considdered names of integer variables, all operators are considdered arithmetic operators (such as `++`, `==`, `>`, `<=`, etc..)
   You should always use this for performing tests on numbers!
 * `[[` ''[test expression]'' `]]`: '''Evaluates the given ''expression'' as a `test`-compatible expression'''. All `test` operators are supported but you can also perform ''Glob pattern matching'' and several other more advanced tests. It is good to note that word splitting will '''not''' take place on unquoted parameter expansions here.
   You should always use this for performing tests on strings and filenames!

==== Loops ====
 * `do` ''[command list]''`; done`: '''This constitutes the actual loop that is used by the next few commands'''. The list of commands between the `do` and `done` are the commands that will be executed in every iteration of the loop.
 * `for` ''[name]'' `in` ''[words]'': '''The next loop will iterate over each ''WORD'' after the `in` keyword''' and run the loop's commands with the value of the variable denoted by `name` set to the word.
 * `for ((` ''[arithmetic expression]''`;` ''[arithmetic expression]''`;` ''[arithmetic expression]'' `))`: '''The next loop will run as long as the second ''arithmetic expression'' remains ''true''.''' The first ''arithmetic expression'' will be ran before the loop starts. The third ''arithmetic expression'' will be ran after the last command in each iteration has been executed.
 * `while` ''[command list]'': '''The next loop will be repeated for as long as the last command ran in the ''command list'' exits successfully.
 * `until` ''[command list]'': '''The next loop will be repeated for as long as the last command ran in the ''command list'' exits unsuccessfully ("fails").
 * `select` ''[name]'' `in` ''[words]'': '''The next loop will repeat forever, letting the user choose between the given words'''. The iteration's commands are executed with the variable denoted by `name`'s value set to the word chosen by the user. Naturally, you can use `break` to end this loop.


=== Builtins ===

Builtins are commands that perform a certain function that has been compiled into Bash. Understandably, they are also the only types of commands (other than those above) that can modify the Bash shell's environment.
Line 8: Line 39:
 * `true` (or `:`): ''These commands do nothing at all.'' They are ''NOP''s that always return successfully.
 * `false`: ''The same as above, except that the command always "fails".'' It returns an exit code of `1` indicating failure.
 * `true` (or `:`): '''These commands do nothing at all.''' They are ''NOP''s that always return successfully.
 * `false`: '''The same as above, except that the command always "fails".''' It returns an exit code of `1` indicating failure.
Line 12: Line 43:
 * `alias`: ''Sets up a Bash alias'', or print the bash alias with the given name.  * `alias`: '''Sets up a Bash alias''', or print the bash alias with the given name.
Line 14: Line 45:
 * `declare` (or `typeset`): ''Assign a value to a variable.'' Each argument is a new variable assignment. Each argument's part before the equal sign is the name of the variable, and after comes the data of the variable. Options to declare can be used to toggle special variable flags (like __r__ead-only/e__x__port/__i__nteger/__a__rray).
 * `export`: ''Export the given variable to the environment'' so that child processes inherit it. This is the same as `declare -x`.
 * `declare` (or `typeset`): '''Assign a value to a variable.''' Each argument is a new variable assignment. Each argument's part before the equal sign is the name of the variable, and after comes the data of the variable. Options to declare can be used to toggle special variable flags (like __r__ead-only/e__x__port/__i__nteger/__a__rray).
 * `export`: '''Export the given variable to the environment''' so that child processes inherit it. This is the same as `declare -x`.
Line 17: Line 48:
 * `local`: ''Declare a variable to have a scope limited to the current function''. As soon as the function exits, the variable disappears. Assigning to it in a function also doesn't change a global variable with the same name, should one exist. The same options as taken by `declare` can be passed to `local`.
 * `type`: ''Show the type of the command name specified as argument''. The type can be either: ''alias'', ''keyword'', ''function'', ''builtin'', or ''file''.
 * `local`: '''Declare a variable to have a scope limited to the current function'''. As soon as the function exits, the variable disappears. Assigning to it in a function also doesn't change a global variable with the same name, should one exist. The same options as taken by `declare` can be passed to `local`.
 * `type`: '''Show the type of the command name specified as argument'''. The type can be either: ''alias'', ''keyword'', ''function'', ''builtin'', or ''file''.
Line 22: Line 52:
 * `read`: ''Read a line ''(unless the `-d` option is used to change the delimiter from ''newline'' to something else)'' and put it in the variables denoted by the arguments given to `read`.'' If more than one variable name is given, split the line up using the characters in `IFS` as delimitors. If less variable names are given than there are split chunks in the line, the last variable gets all data left unsplit.  * `read`: '''Read a line '''(unless the `-d` option is used to change the delimiter from ''newline'' to something else)''' and put it in the variables denoted by the arguments given to `read`'''. If more than one variable name is given, split the line up using the characters in `IFS` as delimitors. If less variable names are given than there are split chunks in the line, the last variable gets all data left unsplit.
Line 25: Line 55:
 * `echo`: ''Output each argument given to `echo` on one line, separated by a single space.'' The first arguments can be options that toggle special behaviour (like __n__o newline at end/evaluate __e__scape sequences).
 * `printf`: ''Use the first argument as a format specifier of how to output the other arguments.'' See `help printf`.
 * `pwd`: ''Output the absolute pathname of the current working directory''. You can use the `-P` option to make `pwd` resolve any symlinks in the pathname.
 * `echo`: '''Output each argument given to `echo` on one line, separated by a single space'''. The first arguments can be options that toggle special behaviour (like __n__o newline at end/evaluate __e__scape sequences).
 * `printf`: '''Use the first argument as a format specifier of how to output the other arguments'''. See `help printf`.
 * `pwd`: '''Output the absolute pathname of the current working directory'''. You can use the `-P` option to make `pwd` resolve any symlinks in the pathname.
Line 30: Line 60:
 * `cd`: ''Changes the current directory to the given path.'' If the path doesn't start with a slash, it is relative to the current directory.
 * `command`: ''Run the first argument as an application'', don't try to look for an alias, function, builtin or keyword by that name.
 * `.` ''or'' `source`: ''Makes Bash read the filename given as first argument and execute its contents in the current shell'' (kind of like `include` in other languages). If more arguments are given than just a filename to `source`, those arguments are set as the positional parameters during the execution of the sourced code.
   If the filename to source has no slash in it, PATH is searched for it.
 * `exec`: ''Run the command given as first argument and replace the current shell with it.'' Other arguments are passed to the command as its arguments. If no arguments are given to exec but you do specify ''Redirections'' on the exec command, the redirections will be applied to the current shell.
 * `exit`: ''End the execution of the current script''. If an argument is given, it is the exit status of the current script (an integer between 0 and 255).
 * `logout`: ''End the execution of a '''login''' shell''.
 * `return`: ''End the execution of the current '''function'''''. An exit status may be specified just like with the `exit` builtin.
 * `ulimit`: ''Modify resource limitations of the current shell's process''. These limits are inherited by child processes.
 * `cd`: '''Changes the current directory to the given path'''. If the path doesn't start with a slash, it is relative to the current directory.
 * `command`: '''Run the first argument as an application''', don't try to look for an alias, function, builtin or keyword by that name.
 * `.` ''or'' `source`: '''Makes Bash read the filename given as first argument and execute its contents in the current shell''' (kind of like `include` in other languages). If more arguments are given than just a filename to `source`, those arguments are set as the positional parameters during the execution of the sourced code.
   If the filename to source has no slash in it, `PATH` is searched for it.
 * `exec`: '''Run the command given as first argument and replace the current shell with it'''. Other arguments are passed to the command as its arguments. If no arguments are given to exec but you do specify ''Redirections'' on the exec command, the redirections will be applied to the current shell.
 * `exit`: '''End the execution of the current script'''. If an argument is given, it is the exit status of the current script (an integer between 0 and 255).
 * `logout`: '''End the execution of a ''login'' shell'''.
 * `return`: '''End the execution of the current ''function'''''. An exit status may be specified just like with the `exit` builtin.
 * `ulimit`: '''Modify resource limitations of the current shell's process'''. These limits are inherited by child processes.
Line 41: Line 71:
 * `jobs`: ''List the current shell's active jobs''.
 * `bg`: ''Send the previous job ''(or job denoted by the given argument)'' to run in the background.'' (The shell continues to run while the job is running. The shell's input is handled by itself, not the job.)
 * `fg`: ''Send the previous job ''(or job denoted by the given argument)'' to run in the foreground.'' (The shell waits for the job to end and the job can receive the input from the shell.)
 * `kill`: ''Send a signal(3) to a process or job''. As argument, give the process ID of the process or the ''jobspec'' of the job you want to send the signal to.
 * `trap`: ''Handle a signal(3) sent to the current shell''. The code that is in the first argument is executed whenever a signal is received denoted by any of the other arguments to `trap`.
 * `suspend`: ''Stops the execution of the current shell until it receives a '''SIGCONT''' signal''. This is much like what happens when the shell receives a '''SIGSTOP''' signal.
 * `wait`: ''Stops the execution of the current shell until active jobs have finished''. In arguments, you can specify which jobs (by ''jobspec'') or processes (by ''PID'') to wait for.
 * `jobs`: '''List the current shell's active jobs'''.
 * `bg`: '''Send the previous job '''(or job denoted by the given argument)''' to run in the background'''. (The shell continues to run while the job is running. The shell's input is handled by itself, not the job.)
 * `fg`: '''Send the previous job '''(or job denoted by the given argument)''' to run in the foreground'''. (The shell waits for the job to end and the job can receive the input from the shell.)
 * `kill`: '''Send a signal(3) to a process or job'''. As argument, give the process ID of the process or the ''jobspec'' of the job you want to send the signal to.
 * `trap`: '''Handle a signal(3) sent to the current shell'''. The code that is in the first argument is executed whenever a signal is received denoted by any of the other arguments to `trap`.
 * `suspend`: '''Stops the execution of the current shell until it receives a ''SIGCONT'' signal'''. This is much like what happens when the shell receives a ''SIGSTOP'' signal.
 * `wait`: '''Stops the execution of the current shell until active jobs have finished'''. In arguments, you can specify which jobs (by ''jobspec'') or processes (by ''PID'') to wait for.
Line 50: Line 80:
 * `break`: ''Break out of the current loop.'' When more than one loop is active, break out the last one declared. When a ''number'' is given as argument to `break`, break out of ''number'' loops, starting with the last one declared.
 * `continue`: ''Skip the code that is left in the current loop'' and start a new iteration of that loop. Just like with `break`, a ''number'' may be given to skip out more loops.
 * `break`: '''Break out of the current loop'''. When more than one loop is active, break out the last one declared. When a ''number'' is given as argument to `break`, break out of ''number'' loops, starting with the last one declared.
 * `continue`: '''Skip the code that is left in the current loop''' and start a new iteration of that loop. Just like with `break`, a ''number'' may be given to skip out more loops.
Line 54: Line 84:
 * `getops`: ''Puts an option specified in the arguments in a variable''. `getopts` Uses the first argument as a specification for which options to look for in the arguments. It then takes the first option in the arguments that is mentioned in this option specification (or next option, if getopts has been ran before), and puts this option in the variable denoted by the name in the second argument to `getopts`.
   This command is pretty much always used in a '''loop''': `while getopts abc opt; do case $opt in a) ...;; b) ...;; c) ...;; esac; done`. This way all options in the arguments are parsed and when they are either `-a`, `-b` or `-c`, the respective code in the `case` statement is executed. Following short style is also valid for specifying multiple options in the arguments that `getopts` parses: `-ac`.
 * `set`: ''The `set` command normally sets various '''Shell options''', but can also set '''Positional parameters'''''.
   '''Shell options''' are options that can be passed to the shell, such as `bash -x` or `bash -e`.
 * `set`: '''The `set` command normally sets various ''Shell options'', but can also set ''Positional parameters'''''.
   ''Shell options'' are options that can be passed to the shell, such as `bash -x` or `bash -e`.
Line 59: Line 87:
   '''Positional parameters''' are parameters that hold arguments that were passed to the script or shell, such as `bash myscript -foo /bar`.    ''Positional parameters'' are parameters that hold arguments that were passed to the script or shell, such as `bash myscript -foo /bar`.
Line 61: Line 89:
 * `shift`: ''Moves all positional parameters' values one parameter back''. This way, values that were in `$1` are discarted, values from `$2` go into `$1`, values from `$3` go into `$2`, and so on. You can specify an argument to shift which is an integer that specifies how many times to repeat this shift.  * `shift`: '''Moves all positional parameters' values one parameter back'''. This way, values that were in `$1` are discarted, values from `$2` go into `$1`, values from `$3` go into `$2`, and so on. You can specify an argument to shift which is an integer that specifies how many times to repeat this shift.
 * `getops`: '''Puts an option specified in the arguments in a variable'''. `getopts` Uses the first argument as a specification for which options to look for in the arguments. It then takes the first option in the arguments that is mentioned in this option specification (or next option, if getopts has been ran before), and puts this option in the variable denoted by the name in the second argument to `getopts`.
   This command is pretty much always used in a '''loop''':
   {{{
while getopts abc opt
do
   case $opt in
      a) ...;;
      b) ...;;
      c) ...;;
   esac
done}}}
   This way all options in the arguments are parsed and when they are either `-a`, `-b` or `-c`, the respective code in the `case` statement is executed. Following short style is also valid for specifying multiple options in the arguments that `getopts` parses: `-ac`.

Bash Reference Sheet

Table Of Contents

TableOfContents

Basic Structures

Compound Commands

Compound commands are statements that can execute several commands but are considdered as a sort of command group by Bash.

Command Lists

  • { [command list]; }: Execute the list of commands in the current shell.

  • ([command list]): Execute the list of commands in a subshell.

Expressions

  • (([arithmetic expression])): Evaluates the given expression in an arithmetic context. That means, strings are considdered names of integer variables, all operators are considdered arithmetic operators (such as ++, ==, >, <=, etc..)

    • You should always use this for performing tests on numbers!
  • [[ [test expression] ]]: Evaluates the given expression as a test-compatible expression. All test operators are supported but you can also perform Glob pattern matching and several other more advanced tests. It is good to note that word splitting will not take place on unquoted parameter expansions here.

    • You should always use this for performing tests on strings and filenames!

Loops

  • do [command list]; done: This constitutes the actual loop that is used by the next few commands. The list of commands between the do and done are the commands that will be executed in every iteration of the loop.

  • for [name] in [words]: The next loop will iterate over each WORD after the in keyword and run the loop's commands with the value of the variable denoted by name set to the word.

  • for (( [arithmetic expression]; [arithmetic expression]; [arithmetic expression] )): The next loop will run as long as the second arithmetic expression remains true. The first arithmetic expression will be ran before the loop starts. The third arithmetic expression will be ran after the last command in each iteration has been executed.

  • while [command list]: The next loop will be repeated for as long as the last command ran in the command list exits successfully.

  • until [command list]: The next loop will be repeated for as long as the last command ran in the command list exits unsuccessfully ("fails").

  • select [name] in [words]: The next loop will repeat forever, letting the user choose between the given words. The iteration's commands are executed with the variable denoted by name's value set to the word chosen by the user. Naturally, you can use break to end this loop.

Builtins

Builtins are commands that perform a certain function that has been compiled into Bash. Understandably, they are also the only types of commands (other than those above) that can modify the Bash shell's environment.

Dummies

  • true (or :): These commands do nothing at all. They are NOPs that always return successfully.

  • false: The same as above, except that the command always "fails". It returns an exit code of 1 indicating failure.

Declarative

  • alias: Sets up a Bash alias, or print the bash alias with the given name.

    • Aliasses replace a word in the beginning of a command by something else. They only work in interactive shells (not scripts).
  • declare (or typeset): Assign a value to a variable. Each argument is a new variable assignment. Each argument's part before the equal sign is the name of the variable, and after comes the data of the variable. Options to declare can be used to toggle special variable flags (like read-only/export/integer/array).

  • export: Export the given variable to the environment so that child processes inherit it. This is the same as declare -x.

    • Remember that for the child process, the variable is not the same as the one you exported. It just holds the same data. Which means, you can't change the variable data and expect it to change in the parent process, too.
  • local: Declare a variable to have a scope limited to the current function. As soon as the function exits, the variable disappears. Assigning to it in a function also doesn't change a global variable with the same name, should one exist. The same options as taken by declare can be passed to local.

  • type: Show the type of the command name specified as argument. The type can be either: alias, keyword, function, builtin, or file.

Input

  • read: Read a line (unless the -d option is used to change the delimiter from newline to something else) and put it in the variables denoted by the arguments given to read. If more than one variable name is given, split the line up using the characters in IFS as delimitors. If less variable names are given than there are split chunks in the line, the last variable gets all data left unsplit.

Output

  • echo: Output each argument given to echo on one line, separated by a single space. The first arguments can be options that toggle special behaviour (like no newline at end/evaluate escape sequences).

  • printf: Use the first argument as a format specifier of how to output the other arguments. See help printf.

  • pwd: Output the absolute pathname of the current working directory. You can use the -P option to make pwd resolve any symlinks in the pathname.

Execution

  • cd: Changes the current directory to the given path. If the path doesn't start with a slash, it is relative to the current directory.

  • command: Run the first argument as an application, don't try to look for an alias, function, builtin or keyword by that name.

  • . or source: Makes Bash read the filename given as first argument and execute its contents in the current shell (kind of like include in other languages). If more arguments are given than just a filename to source, those arguments are set as the positional parameters during the execution of the sourced code.

    • If the filename to source has no slash in it, PATH is searched for it.

  • exec: Run the command given as first argument and replace the current shell with it. Other arguments are passed to the command as its arguments. If no arguments are given to exec but you do specify Redirections on the exec command, the redirections will be applied to the current shell.

  • exit: End the execution of the current script. If an argument is given, it is the exit status of the current script (an integer between 0 and 255).

  • logout: End the execution of a login shell.

  • return: End the execution of the current function. An exit status may be specified just like with the exit builtin.

  • ulimit: Modify resource limitations of the current shell's process. These limits are inherited by child processes.

Jobs/Processes

  • jobs: List the current shell's active jobs.

  • bg: Send the previous job (or job denoted by the given argument) to run in the background. (The shell continues to run while the job is running. The shell's input is handled by itself, not the job.)

  • fg: Send the previous job (or job denoted by the given argument) to run in the foreground. (The shell waits for the job to end and the job can receive the input from the shell.)

  • kill: Send a signal(3) to a process or job. As argument, give the process ID of the process or the jobspec of the job you want to send the signal to.

  • trap: Handle a signal(3) sent to the current shell. The code that is in the first argument is executed whenever a signal is received denoted by any of the other arguments to trap.

  • suspend: Stops the execution of the current shell until it receives a SIGCONT signal. This is much like what happens when the shell receives a SIGSTOP signal.

  • wait: Stops the execution of the current shell until active jobs have finished. In arguments, you can specify which jobs (by jobspec) or processes (by PID) to wait for.

Conditionals And Loops

  • break: Break out of the current loop. When more than one loop is active, break out the last one declared. When a number is given as argument to break, break out of number loops, starting with the last one declared.

  • continue: Skip the code that is left in the current loop and start a new iteration of that loop. Just like with break, a number may be given to skip out more loops.

Arguments

  • set: The set command normally sets various Shell options, but can also set Positional parameters.

    • Shell options are options that can be passed to the shell, such as bash -x or bash -e.

      • set toggles shell options like this: set -x, set +x, set -e, ...

      Positional parameters are parameters that hold arguments that were passed to the script or shell, such as bash myscript -foo /bar.

      • set assigns positional parameters like this: set -- -foo /bar.

  • shift: Moves all positional parameters' values one parameter back. This way, values that were in $1 are discarted, values from $2 go into $1, values from $3 go into $2, and so on. You can specify an argument to shift which is an integer that specifies how many times to repeat this shift.

  • getops: Puts an option specified in the arguments in a variable. getopts Uses the first argument as a specification for which options to look for in the arguments. It then takes the first option in the arguments that is mentioned in this option specification (or next option, if getopts has been ran before), and puts this option in the variable denoted by the name in the second argument to getopts.

    • This command is pretty much always used in a loop:

      while getopts abc opt
      do
         case $opt in
            a) ...;;
            b) ...;;
            c) ...;;
         esac
      done

      This way all options in the arguments are parsed and when they are either -a, -b or -c, the respective code in the case statement is executed. Following short style is also valid for specifying multiple options in the arguments that getopts parses: -ac.

BashSheet (last edited 2021-12-20 04:19:50 by 71)