Git Product home page Git Product logo

bash-libraries's Introduction

bash-libraries

A set of useful Bash libraries

Current libraries

Use

Load the desired library on you Bash scripts as follows:

...
. /dev/stdin <<< "$(curl -s https://raw.githubusercontent.com/johnsmclay/bash-libraries/main/lib/liblog.bash)"
...

Bash best practices

Here you can find a series of tips when developing a Bash script:

  • Start your scripts with the proper shebang: #!/bin/bash.

  • Start each file with a description of its contents.

  • Use built-in options set -o OPTION:

    • errexit: make your script exit when a command fails. Add || true to allow a command to fail.
    • nounset: make your script exit when using undeclared variables.
    • pipefail: sets the exit code of a pipeline to that of the rightmost command to exit with a non-zero status, or to zero if all commands of the pipeline exit successfully.
    • xtrace: only for debugging.
  • Create a main function for scripts long enough to contain at least one other function.

  • Use local when declaring variables, unless there is reason to use declare.

  • Use readonly when defining "read only" constants.

  • Use :- for variables that could be undeclared.

  • Naming conventions:

    • Only use uppercase on constants or when a variable is exported to the environment.
    • Use underscore _ instead of - to separate words. Do not use camelCase.
    • Do not start variable names with special characters or numbers.
  • Use . xxx.sh instead of source xxx.sh.

  • Use $() over backticks.

  • Use ${var} instead of $var when concatenating strings.

  • Double quote variables, no naked $ signs.

  • Use my_func() { ... } instead of function my_func { ... }.

  • Use && and || for simple conditionals.

  • Put then;, do;, etc. on the same line.

  • Use [[ instead of [.

  • Prefer absolute paths.

  • Avoiding temporary files. Use process substitution (<()) to transform commands output into sth that can be used as a filename:

    # Note that if any of the wget fails, and the diff succeed, this
    # statement will success regarding of built-in options!
    diff <(wget -O - url1) <(wget -O - url2)
  • Warnings and errors should go to STDERR.

  • Use bash -x myscript.sh for debugging.

Note: You can use ShellCheck to ensure your bash syntax does not contain any warning/error. Linters will be included on repositories to ensure every commit passes a series of checks.

bash-libraries's People

Contributors

johnsmclay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.