Interview Prep/Shell Scripting/ShellCheck, debugging, testing, portability and security

How do you check a downloaded script or binary before running it?

IntermediateConceptShellCheck, debugging, testing, portability and security
Answer

Avoid curl ... | bash for anything you do not fully control: a truncated download can execute a partial script, and there is no verification.

curl -fsSLo install.sh https://example.com/install.sh
curl -fsSLo install.sh.sha256 https://example.com/install.sh.sha256
sha256sum -c install.sh.sha256 || die "checksum mismatch"
less install.sh                 # read it
bash install.sh

For binaries in CI, pin a version and its SHA-256 in the script, and verify GPG or cosign signatures where the project publishes them.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on ShellCheck, debugging, testing, portability and security