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.
More on ShellCheck, debugging, testing, portability and security
What is ShellCheck and how do you use it in a team?BeginnerHow do you disable a ShellCheck warning, and when is that acceptable?IntermediateName the ShellCheck warnings you see most and what each means.IntermediateHow do you debug a Bash script?BeginnerHow do you make `set -x` output more useful with `PS4`?IntermediateWhat is bats, and how do you test a script that calls external commands?Intermediate