Code Monkey home page Code Monkey logo

bashstyle's Introduction

progrium/bashstyle

Bash is like the JavaScript of systems programming. Although in some cases it's better to use a systems language like C or Go, Bash is actually an ideal systems language for many smaller POSIX-oriented or command line tasks. Here's three quick reasons why:

  • It's everywhere. Like JavaScript for the web, Bash is already there ready for systems programming.
  • It's neutral. Unlike Ruby, Python, JavaScript, or PHP, Bash offends equally across all communities. ;)
  • It's made to be glue. Write complex parts in C or Go (or whatever!), and glue them together with Bash.

The same way people hated JavaScript before people got serious about it, Bash just needs to be used in a way that makes it feel like a real programming language. Part of this is establishing a consistent style and best practices.

This document is how I write Bash and how I'd like collaborators to write Bash with me in my open source projects. It's based on a lot of experience and time collecting best practices. Most of them come from these two articles, but here integrated, slightly modified, and focusing on the most bang for buck items. Plus some new stuff!

Big Rules

  • Always double quote variables, including subshells. No naked $ signs
  • All code goes in a function. Even if it's one function, main.
    • Unless a library script, you can do global script settings and call main. That's it.
    • Avoid global variables. Though when defining constants use readonly
  • Always have a main function for runnable scripts, called with main or main "$@"
    • If script is also usable as library, call it using [[ "$0" == "$BASH_SOURCE" ]] && main "$@"
  • Always use local when setting variables, unless there is reason to use declare
    • Exception being rare cases when you are intentionally setting a variable in an outer scope.
  • Always use set -eo pipefail. Fail fast and be aware of exit codes.
    • Use || true on programs that you intentionally let exit non-zero.
  • Never use deprecated style. Most notably:
  • Always use declare and name variable arguments at the top of functions that are more than 2-lines
    • Example: declare arg1="$1" arg2="$2". You'll write/see this a lot now.
    • The exception is when defining variadic functions. See below.

If you know what you're doing, you can bend or break some of these rules, but generally they will be right and be extremely helpful.

Best Practices and Tips

  • Use Bash variable substitution if possible before awk/sed
  • Generally use double quotes unless it makes more sense to use single quotes
  • For simple conditionals, try using && and ||.
  • Put then, do, etc on same line, not newline
  • Use .sh or .bash extension if file is meant to be included/sourced. Never on executable script.
  • Put complex one-liners of sed, perl, etc in a standalone function with a descriptive name
  • Good idea to include [[ "$TRACE" ]] && set -x
  • Avoid flag arguments and parsing, instead use optional environment instead
  • In large systems or for any CLI commands, add a description to functions
    • Use declare desc="description" at the top of functions, even above argument declaration
    • This can be queried/extracted with a simple function using reflection
  • No hard tabs?

Good References and Help

Examples

Regular function with named arguments

Defining functions with arguments

regular_func() {
	declare arg1="$1" arg2="$2" arg3="$3"

	# ...
}

Variadic functions

Defining functions with a final variadic argument

variadic_func() {
	local arg1="$1"; shift
	local arg2="$1"; shift
	local rest="$@"

	# ...
}

More todo

bashstyle's People

Contributors

progrium 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.