• AstroLightz@lemmy.world
    link
    fedilink
    arrow-up
    32
    ·
    2 days ago

    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.

    • tetris11@feddit.uk
      link
      fedilink
      English
      arrow-up
      4
      ·
      22 hours 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   
      
    • TechLich@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      24 hours 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