summaryrefslogtreecommitdiffstats
path: root/software/bash.page
blob: 0d5325b74e7cdce6f9496fdf378ff0e4709d8a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
---
format: markdown
toc: no
title: Bash Shell
...

## Job Control

The syntax for "job number" or "JOBSPEC" (when using `kill` or similar) is
`%4` or `%5`.


## Startup

`bash` by default takes a very long time to initialize because the
auto-completion scripts are loaded multiple times; disable this in
`~/.bashrc`?


## Piping

You can pipe both `stdout` and `stderr` together either to a file or two another command::

    grep --asdf >& /some/file
    grep --asdf |& less


## Prelude

I frequently add a one-line version of the following to shell scripts:

    set -e              # fail on error
    set -u              # fail if variable not set in substitution
    set -o pipefail     # fail if part of a '|' command fails

Note that `join`, `grep`, and others sometimes exit non-zero return codes on
purpose (eg, pipe input closed or found no matches, as expected), which makes
life difficult. Sometimes `|| true` is enough to get around this.

## General Style

Google has a style guide: https://google.github.io/styleguide/shell.xml

Shellcheck is a lint tool: https://www.shellcheck.net/

http://redsymbol.net/articles/unofficial-bash-strict-mode/