Sourcegraph DocsSourcegraph Docs
  • Code Intelligence

    • Cody
    • Code Search
  • Code Management

    • Batch Changes
    • Code Navigation
    • Code Monitoring
    • Code Ownership
    • Code Insights
    • Notebooks
  • Platform

    • Sourcegraph Admin
    • Sourcegraph Cloud
    • Integrations
    • Development
  • CLI & API

    • Sourcegraph CLI
    • Sourcegraph GraphQL API
    • Sourcegraph Stream API
  • Help & Support

    • SLAs & Premium Support
    • Tutorials
    • Sourcegraph Accounts
    • Changelog
  1. Docs
  2. dev
  3. background-information
  4. languages
  5. bash

Bash style

Bash is frequently used in our build, CI and deployment systems. Some general guidelines and recommended reading

  • Bash style
    • Reading from a file
    • Set -eu -o pipefail
    • References

Reading from a file

Prefer using mapfile instead of while IFS to read a file

mapfile -t myArray < file.txt
mapfile -t myArray < <(find -d .)

instead of

input="/path/to/txt/file"
while IFS= read -r line
do
  echo "$line"
done < "$input"

Set -eu -o pipefail

This is generally "bash strict mode" and sets

  • e exit if error
  • u error on variable unset (and exit)
  • -o pipefail fail if items in the pipe | fail. Bash otherwise continues if error | pass which causes some unexpected behavior.

Recommend using these at the start of all scripts and specifically disabling if a section of a bash script does not need them (for example, you want to let a pipe fail).

Utilities in use

We use shfmt and shellcheck for our shell script linters

References

  • https://wiki.bash-hackers.org/
  • https://www.notion.so/daxmc99/One-liners-Basic-Cheat-sheet-Linux-Command-Library-b3676fb5a49b44f8a507cce0185ca5d7>

On this page

  1. Bash style

    1. References