Differences between revisions 1 and 10 (spanning 9 versions)
Revision 1 as of 2009-07-06 19:33:21
Size: 279
Editor: SamatJain
Comment:
Revision 10 as of 2021-04-10 20:03:53
Size: 3141
Editor: SamatJain
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
<<TableOfContents>>
Line 6: Line 8:
 * https://github.com/Bash-it/bash-it: Community bash "framework"
 * [[https://google.github.io/styleguide/shellguide.html|Google's shell script style guide]]. Also good is [[https://bash3boilerplate.sh/|Bash 3 Boilerplate]]

== Neat features ==

=== Edit a command in $EDITOR ===

Ctrl-x Ctrl-e will edit a command in $EDITOR before execution.

With `set -o vi` in bash/readline, `v` in command mode will do the same.

=== -v for testing set variables, while handling undefined ones ===

-v: True if the shell variable varname is set (has been assigned a value).

bash >=4.2 supports -v for testing for *set* variables while handling empty ones, also no specials needs when running under 'set -u':

{{{#!highlight sh
% cat foo
FOO=''
[ -z "${FOO:-}" ] && echo 1
[ -z "${BAR:-}" ] && echo 2
[ -v FOO ] && echo 3
[ -v BAR ] && echo 4
% bash -u ./foo
1
2
3
}}}

[[https://mobile.twitter.com/mikagrml/status/1222225444558209024?s=19|source]]

also:

If you use bash, you should really use [[ ]] instead of [ ]. It can do everything [ can (and then some, like ERE and glob matches), but is a shell keyword, which improves ergonomics quite a bit. See [ $x = 0 ] vs. [[ $x = 0]] after `unset x`, for example.


== Special Variables ==

{{{#!text_markdown
 * $0 - The filename of the current script. Do not use!
 * $n - The Nth argument passed to script was invoked or function was called. E.g. $1, etc.


 * $# - The number of argument passed to script or function.
 * $@ - All arguments passed to script or function.
 * $* - All arguments passed to script or function.
 * $? - The exit status of the last command executed.
 * $$ - The process ID of the current shell. For shell scripts, this is the process ID under which they are executing.
 * $! - The process number of the last background command.

 * $- - current options set for the shell
 * $_ - most recent parameter, or the absolute path of the command used to start the current shell

See [Special Parameters from the Bash Reference Manual](https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html).
}}}

== "Strict Mode" ==

 * [[http://redsymbol.net/articles/unofficial-bash-strict-mode/|Unofficial Bash Strict Mode]]

== Tools ==

Use shellcheck with timonwong.shellcheck plugin.

Use bash-language-server.

== Gotchas ==

 * [[https://thomask.sdf.org/blog/2019/11/09/take-care-editing-bash-scripts.html|Take care editing bash scripts]]: Bash processes scripts as they execute, so saving over a script that is running can have side effects.

== Never Remember ==

[[https://en.wikipedia.org/wiki/Process_substitution|Process Substition]] is the name for `<(command)` syntax.

Redirect stderr to stdout with `2>&1`, e.g.:

{{{#!highlight sh numbers=off
Some-Command 2>&1 > log.txt
}}}

Interesting bash aliases and functions:

Neat features

Edit a command in $EDITOR

Ctrl-x Ctrl-e will edit a command in $EDITOR before execution.

With set -o vi in bash/readline, v in command mode will do the same.

-v for testing set variables, while handling undefined ones

-v: True if the shell variable varname is set (has been assigned a value).

bash >=4.2 supports -v for testing for *set* variables while handling empty ones, also no specials needs when running under 'set -u':

   1 % cat foo
   2 FOO=''
   3 [ -z "${FOO:-}" ] && echo 1
   4 [ -z "${BAR:-}" ] && echo 2
   5 [ -v FOO ] && echo 3
   6 [ -v BAR ] && echo 4
   7 % bash -u ./foo
   8 1
   9 2
  10 3

source

also:

If you use bash, you should really use ]] instead of [ ]. It can do everything [ can (and then some, like ERE and glob matches), but is a shell keyword, which improves ergonomics quite a bit. See [ $x = 0 ] vs. [[ $x = 0 after unset x, for example.

Special Variables

  • $0 - The filename of the current script. Do not use!
  • $n - The Nth argument passed to script was invoked or function was called. E.g. $1, etc.

  • $# - The number of argument passed to script or function.

  • $@ - All arguments passed to script or function.
  • $* - All arguments passed to script or function.
  • $? - The exit status of the last command executed.
  • $$ - The process ID of the current shell. For shell scripts, this is the process ID under which they are executing.
  • $! - The process number of the last background command.

  • $- - current options set for the shell

  • $_ - most recent parameter, or the absolute path of the command used to start the current shell

See Special Parameters from the Bash Reference Manual.

"Strict Mode"

Tools

Use shellcheck with timonwong.shellcheck plugin.

Use bash-language-server.

Gotchas

Never Remember

Process Substition is the name for <(command) syntax.

Redirect stderr to stdout with 2>&1, e.g.:

Some-Command 2>&1 > log.txt

SamatsWiki: Bash (last edited 2021-04-25 02:46:19 by SamatJain)