All Projects → codica2 → script-best-practices

codica2 / script-best-practices

Licence: other
Best practices for writing bash scripts

Programming Languages

shell
77523 projects

Projects that are alternatives of or similar to script-best-practices

go-interfaces
This repos has lots of Go interface usage and best practice examples
Stars: ✭ 112 (+646.67%)
Mutual labels:  best-practices
MyScripts
No description or website provided.
Stars: ✭ 32 (+113.33%)
Mutual labels:  scripts
android-oss-best-practices
Best practices on creating Android OSS library projects [JA]
Stars: ✭ 32 (+113.33%)
Mutual labels:  best-practices
dx-scanner
CLI tool that allows you to measure quality of a team work and an app based on your source code.
Stars: ✭ 79 (+426.67%)
Mutual labels:  best-practices
best-practices
Unosquare Labs Best Practices
Stars: ✭ 19 (+26.67%)
Mutual labels:  best-practices
OrientPy
Seismic station orientation tools
Stars: ✭ 29 (+93.33%)
Mutual labels:  scripts
rofi-scripts
Small scripts for rofi
Stars: ✭ 61 (+306.67%)
Mutual labels:  scripts
vsSolutionBuildEvent
🎛 Event-Catcher with variety of advanced Actions to service projects, libraries, build processes, runtime environment of the Visual Studio, MSBuild Tools, and …
Stars: ✭ 66 (+340%)
Mutual labels:  scripts
dotstow
dotfiles managed with stow
Stars: ✭ 60 (+300%)
Mutual labels:  scripts
Examples
An complete examples and related support for various popular projects, and more.
Stars: ✭ 22 (+46.67%)
Mutual labels:  scripts
wireguard-tools
Wireguard helper scripts
Stars: ✭ 147 (+880%)
Mutual labels:  scripts
perl-scripts
A nice collection of day-to-day Perl scripts.
Stars: ✭ 92 (+513.33%)
Mutual labels:  scripts
a11y-contracting
Building Accessibility Best Practices into Contracting
Stars: ✭ 43 (+186.67%)
Mutual labels:  best-practices
ASP.NET-Core-Web-API-Best-Practices-Guide
ASP.NET Core Web API Best Practices Guide
Stars: ✭ 62 (+313.33%)
Mutual labels:  best-practices
harmony-ops
Harmony Ops Master Repository.
Stars: ✭ 36 (+140%)
Mutual labels:  scripts
installer-scripts
💻 Bash scripts for doing installations in one go.
Stars: ✭ 17 (+13.33%)
Mutual labels:  scripts
mIRC-Twitch-Scripts
Various scripts and games to use with a mIRC bot designed for Twitch.tv
Stars: ✭ 30 (+100%)
Mutual labels:  scripts
php-the-right-way
La version française de "PHP: The Right Way" qui est un condensé des meilleures pratiques sur le développement PHP (conventions, liens vers les tutoriels qui font référence, etc)
Stars: ✭ 51 (+240%)
Mutual labels:  best-practices
talk-dev-to-me-twitch
This repo is a collection of code samples and links to previous twitch live stream sessions. If you have any ideas or suggestions for future episodes, feel free to open an issue.
Stars: ✭ 118 (+686.67%)
Mutual labels:  best-practices
me
dotfiles of various sorts; scripts, configuration files, modifications, etc.
Stars: ✭ 15 (+0%)
Mutual labels:  scripts

Best script practices

Bash

Bash is an ideal systems language for UNIX or command line tasks. Of course, in some cases it's better to use systems languages like C or Go. However, Bash can solve almost all of your problems, you just need to use it.

Basic Rules

  • It’s recommended to apply Clean Code principles while working with Bash.

  • All code blocks should be commented. Thus, it makes the script easier to read and understand.

    • Always double quote variables, including subshells. No naked $ signs.

      echo "Names without double quotes"
      echo
      names="Codica Shell Practises"
      for name in $names; do
         echo "$name"
      done
      echo
      
      echo "Names with double quotes"
      echo
      for name in "$names"; do
         echo "$name"
      done

Variables

  • Variables is used to store your values. You should operate them carefully, because sometimes Bash sometimes can use undeclared variables.

  • Concerning variables, use names that reflect the values stored in them, but try to make it more briefly.

  • Make static variables read-only.

    # Password file variable
    readonly passwd_file='/etc/passwd'
    • Always use local when setting variables, unless there is reason to use declare.

      • An exception to this are cases when you are intentionally setting a variable in an outer scope.
      func(){
          cd $PWD
      }

Functions

  • Functions are created to reduce the amount of code. Try to write brief functions, that do only one task if it is possible.

  • Declare variables with a meaningful name for positional parameters of functions.

    • Use UNIX-like approach: a function does one thing.
     # Function description
    func(){
        printf("Hello world!")
    }

I/O

  • Your script will sometimes require input from user. Try to make a clear message of your script requirements and don't forget to handle all correctly.

  • You should prefer printf to echo.

    • printf gives more control over the output, it’s more portable and its behaviour is defined better.

    • Your script should always react to abnormal input.

    func(){
        echo -e "Unexpected argument!"
    }
    • It should always return 0 in case of a success and another value otherwise. It would be great if you can handle those.

    • If your script requires user input, use prompts.

      printf "Input email: "
      read email
      printf "$email"

      prompt

Security

  • Security is very important thing. When writing a script be prepared for that your script will be executed by a less experienced person, so you should make your script fool-proof.

  • Don't store your credentials in shell scripts, people can have access to your script. Use ENV variables or vaults instead.

    • You should always remember that your script can be executed on other machines or in container, so you need to use environment variables i.e.
    # Copying from remote machine with ENV path
    scp 192.168.0.1:$HOME $HOME
    • Instead of
    # Copying from remote machine with an absolute path
    scp 192.168.0.1:/home/myuser /home/otheruser
    • The matter is that there may not be those directories you want to copy.

    • Sometimes the script will be executed even when a command fails. Thus, will affect the rest of the script. Use set -o errexit exit a script when a command fails.

      • nounset flag to exit when your script tries to use undeclared variables.
      • xtrace helps to trace what gets executed (useful for debugging)
     #let the script exit if a command fails
     set -o errexit
     set -o nounset
     set -o xtrace

Useful Tips

  • You should prefer absolute paths /home/user/file instead to relative ~/file.

    • Use .sh or .bash extension if the file is meant to be included/sourced. Don’t use these extensions on executable script.

    • Do not use deprecated style.

      • Define functions as func() { }.

      • Always use [[...]] instead of [].

      • Do not use backticks, use $( ... ).

License

Best script practices is Copyright © 2015-2019 Codica. It is released under the MIT License.

About Codica

Codica logo

We love open source software! See our other projects or hire us to design, develop, and grow your product.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].