you are viewing a single comment's thread
view the rest of the comments
[–] 34 points 5 months ago (2 children)

When writing BASH scripts:

  • The .sh extension isn't necessary.
  • Using /usr/bin/env bash is better than /bin/bash as not everyone has their bash located in bin.
  • Try to use local variables when you can. If you use global variables that won't be used anywhere else, unset them at the end of your script.
  • Don't use set -euo pipefail. It will only cause more issues down the road.
  • Functions are your friend. If your repeating code, put it in a function.

These are some of the tips I learned while learning bash.

  • source
  • hideshow 2 child comments
  • [–] 9 points 5 months ago

    If writing a lot of bash scripts, I really recommend shellcheck. It's a linter for bash that gives a lot of good advice and points out common issues/inefficiencies and errors. There's plugins for most editors or you can just run it in a terminal. I also like that it has good documentation that tells you why something might be wrong or inadvisable.

    https://github.com/koalaman/shellcheck

  • source
  • parent
  • [–] 6 points 5 months ago

    Don’t use set -euo pipefail. It will only cause more issues down the road.

    Wait why? It's dangerous if the script silently fails surely.

    Also a cool tip

    (
       Long Code here 
       You want to track
    ) | tee -a log.txt   
    
  • source
  • parent