Giter Club home page Giter Club logo

bash_scripting_examples's Introduction

Bash examples

Small examples of bash scripting.

Bash scripting naming conventions

Element Notation Example Notes
Constants SCREAMING_SNAKE_CASE DEST_PATH Use readonly or declare -r to ensure they are readonly.
Environment variable names SCREAMING_SNAKE_CASE PATH
File snake_case my_script Executables should not have extension (strongly preferred) or a .sh extension.1
Functions snake_case do_something(){ } The keyword function it's optional, but must be used consistently troughout a project.2
Hashbang #!/usr/bin/env bash #!/usr/bin/bash asumes it's always installed in /bin, which can cause issues.3
Local variables snake_case my_local_variable Ensure that local variables are only seen inside a function and it's children by using local when declaring them.
Variables snake_case user_name

Multiline comments

: '
   line1
   line2
'

Debug

Executing

bash -x ./script

With hashbang

#/usr/bin/env bash -x

Debug a block

set -x

echo "Code block"

set +x

Command calls

Check return values

Always check return values and give informative return values. For unpiped commands use $? or check directly via an if statement.

if ! mv "${file_list[@]}" "${dest_dir}/"; then
  echo "Unable to move ${file_list[*]} to ${dest_dir}" >&2
  exit 1
fi

# Or
mv "${file_list[@]}" "${dest_dir}/"
if (( $? != 0 )); then
  echo "Unable to move ${file_list[*]} to ${dest_dir}" >&2
  exit 1
fi

Builtin Commands vs External Commands

Given the choice between invoking a shell builtin and invoking a separete process, choose the builtin.

# Prefer this:
addition=$(( X + Y ))
substitution="${string/#foo/bar}"

# Instead of this:
addition="$(expr "${X}" + "${Y}")"
substitution="$(echo "${string}" | sed -e 's/^foo/bar/')"

Static analysis

Use ShellCheck to get warnings and suggestions for your scripts.

Sources

Support

If you find these examples useful, you can star this repo.

Footnotes

  1. Libraries must have a .shextension and should not be executable.

  2. The use of the keyword functionreduces compatibility with older versions of bash.

  3. Google does recommend #!/bin/bash

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.