Title: Theory / Unimportant / CS / Programming / Bash / Google Styleguide
Tags: declaration seen_description todo_declaration
On every note of this deck, unless a note says otherwise:
Page: https://google.github.io/styleguide/shellguide.html
Page: https://google.github.io/styleguide/shellguide.html
Context: Google bash style guide

Instruction: #!/bin/bash on first line
Effect: Indicate file contains some bash code

Question: When to stop using bash?
Answer: 100 lines
Answer2: non-straightforward control flow logic
Answer3: Need speed

Question: What are the difference between library and executable bash file?
Answer: .sh for library, no extension for executable
Answer2: library have no executable bit set

Instruction: foo >&2
Effect: Output of foo is sent to standard error
Context: Google bash style guide

Instruction: !foo
Effect: execute foo
Context: Google bash style guide
Returns: The negation of foo

Question: How to return value from bash function
Answer: echoing it

Question: What should contain a function comment ?
Answer: description of the function
Answer2: Global variable used and modified
Answer3: Arguments taken
Answer4: outputs to stdout and stderr
Answer5: returns if not default

Instruction: function foo() {
  foo
}
Page: https://google.github.io/styleguide/shellguide.html
Meaning: Define a function named foo executing foo
Language: bash

Instruction: "${var}"
Language: bash
Returns: The value of the global variable var

Instruction: #TODO(name): bar
Effect: state that name is in charge of doing bar
Context: Google bash style guide

Question: indentation rules
Answer: already existing style if any
Answer2: 2 space indent, no tab

Question: How to deal with strings with more than 80 characters
Answer: here document
Answer2: multiline is acceptable
Answer3: for literal, accept them if required, try to avoid if possible

Question: How to format pipeline
Answer: Either a single line, or
Answer2: command1 \
  | command2 \
  | command3 \
  | command4

Question: Where to put `; do`
Answer: Same line as `for`

Question: Where to put `; then`
Answer: Same line as `if`

Question: Where to put `else`
Answer: it's own line

Question: Where to put `fi`, `done`
Answer: it's own line

Instruction: case "${var}" in
  pattern_1) foo_1;;
  ..
  pattern_n) foo_n;;
esac
Effect: If the value of var satisfies glob pattern pattern_i first, execute foo_i
Language: bash

Question: How to format cases
Answer: either single line
Answer2: one line for case, one for ;;, and code in between

Question: How to format variables
Answer: Stay consistent with existing code
Answer2: brace named variable
Answer3: don't brace numbered/special variable, except if it removes confusion

Question: When not to quote a value
Answer: Integral value and readonly special variables that are integers
Answer2: command options/path name
Answer3: [[ ... ]]

Instruction: "$@"
Language: bash
Returns: All arguments to the fuction as is

Instruction: "$*"
Language: bash
Returns: A string containing all arguments separated by space

Instruction: $(foo)
Effect: Execute command foo
Language: bash

Question: Why to prefer $( foo ) over `foo` ?
Answer: Backtick require escaping for nesting, and it's harder to read

Question: Why to prefer $@ over $* ?
Answer: $* will always be a string, even if empty string, that change the number of arguments

Question: Which test to prefer?
Answer: [[foo]]

Effect: Length of foo is 0
Language: bash
Context: [[
Abbreviation: -z foo

Language: bash
Context: [[
Returns: Length of foo is non zero
Abbreviation: -n foo

Instruction: foo == bar
Language: bash
Context: [[
Returns: Whether foo and bar are the same string

Instruction: foo < bar
Language: bash
Context: [[
Returns: Whether foo is lexicographically smaller than bar

Instruction: foo -gt bar
Language: bash
Context: [[
Returns: Whether foo as a number is greater than bar

Question: How to deal with getting list of documents of path
Answer: path/./*
Extra: This ensure that file name beginning by "-" are not considered as flags

Question: How to use eval ?
Answer: Don't use it

Question: Why to use array
Answer: Contains list of elements without quoting complication.

Instruction: var=(foo)
Effect: Create an array with content foo and assign it to var
Language: bash

Instruction: ar+=(foo)
Effect: Append foo to array ar
Language: bash
Variables: ar an array

Question: How to iterate over lines of an output
Answer: last_line='NULL'
readarray -t lines < <(your_command)
for line in "${lines[@]}"; do
  foo
done
echo "${last_line}"

Question: how to do arithmetic
Answer: With (( ... ))

Question: how to name variable
Answer: library_name::lower_case

Question: how to name constant
Answer: CAPITALIZED

Question: how to name exported variable
Answer: CAPITALIZED_NAME

Instruction: readonly NAME ='value'
Effect: Create a constant NAME with value
Language: Bash

Instruction: readonly NAME
Effect: State that NAME is readonly
Language: Bash

Question: how to name file name
Answer: lower_case

Instruction: local name
Effect: State that NAME is a local variable
Language: Bash
Context: function

Instruction: $?
Effect: Exit status of last command
Language: Bash
Context: function

Question: What to do after calling a command ?
Answer: Checking return status

Instruction: PIPESTATUS
Effect: Array of return status of the last executed pipe
Language: Bash

Question: Given the choice between invoking a shell builtin and invoking a separate process, choose
Answer: the builtin