diff options
author | EuAndreh <eu@euandre.org> | 2025-03-31 21:51:40 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-03-31 21:51:40 -0300 |
commit | 570ec471d1605318aeefb030cd78682ae442235b (patch) | |
tree | 51e17eabe37c6689f8799b55e6875c3480329a2c /src/content/tils/2021/01/17 | |
parent | Makefile, mkdeps.sh: Derive index.html and feed.xml from more static "sortdat... (diff) | |
download | euandre.org-570ec471d1605318aeefb030cd78682ae442235b.tar.gz euandre.org-570ec471d1605318aeefb030cd78682ae442235b.tar.xz |
src/content/: Update all files left to asciidoc
Diffstat (limited to 'src/content/tils/2021/01/17')
-rw-r--r-- | src/content/tils/2021/01/17/posix-shebang.adoc | 63 |
1 files changed, 33 insertions, 30 deletions
diff --git a/src/content/tils/2021/01/17/posix-shebang.adoc b/src/content/tils/2021/01/17/posix-shebang.adoc index 5f5b897..4e2fbe8 100644 --- a/src/content/tils/2021/01/17/posix-shebang.adoc +++ b/src/content/tils/2021/01/17/posix-shebang.adoc @@ -1,55 +1,58 @@ = POSIX sh and shebangs -date: 2021-01-17 +:awk-1: link:../../../2020/12/15/shellcheck-repo.html +:awk-2: link:../12/curl-awk-emails.html -layout: post +As I {awk-1}[keep moving] {awk-2}[towards POSIX], I'm on the process of +migrating all my Bash scripts to POSIX sh. -lang: en - -ref: posix-sh-and-shebangs - ---- - -As I [keep moving][posix-awk-0] [towards POSIX][posix-awk-1], I'm on the process of migrating all my Bash scripts to POSIX sh. - -As I dropped `[[`, arrays and other Bashisms, I was left staring at the first line of every script, wondering what to do: what is the POSIX sh equivalent of `#!/usr/bin/env bash`? -I already knew that POSIX says nothing about shebangs, and that the portable way to call a POSIX sh script is `sh script.sh`, but I didn't know what to do with that first line. +As I dropped `[[`, arrays and other Bashisms, I was left staring at the first +line of every script, wondering what to do: what is the POSIX sh equivalent of +`#!/usr/bin/env bash`? I already knew that POSIX says nothing about shebangs, +and that the portable way to call a POSIX sh script is `sh script.sh`, but +I didn't know what to do with that first line. What I had previously was: -```shell + +[source,shell] +---- #!/usr/bin/env bash set -Eeuo pipefail cd "$(dirname "${BASH_SOURCE[0]}")" -``` +---- -Obviously, the `$BASH_SOURCE` would be gone, and I would have to adapt some of my scripts to not rely on the script location. -The `-E` and `-o pipefail` options were also gone, and would be replaced by nothing. +Obviously, the `$BASH_SOURCE` would be gone, and I would have to adapt some of +my scripts to not rely on the script location. The `-E` and `-o pipefail` +options were also gone, and would be replaced by nothing. I converted all of them to: -```shell + +[source,shell] +---- #!/bin/sh -eu -``` +---- I moved the `-eu` options to the shebang line itself, striving for conciseness. -But as I changed callers from `./script.sh` to `sh script.sh`, things started to fail. -Some tests that should fail reported errors, but didn't return 1. +But as I changed callers from `./script.sh` to `sh script.sh`, things started to +fail. Some tests that should fail reported errors, but didn't return 1. -My first reaction was to revert back to `./script.sh`, but the POSIX bug I caught is a strong strain, and when I went back to it, I figured that the callers were missing some flags. -Specifically, `sh -eu script.sh`. +My first reaction was to revert back to `./script.sh`, but the POSIX bug I +caught is a strong strain, and when I went back to it, I figured that the +callers were missing some flags. Specifically, `sh -eu script.sh`. -Then it clicked: when running with `sh script.sh`, the shebang line with the sh options is ignored, as it is a comment! +Then it clicked: when running with `sh script.sh`, the shebang line with the sh +options is ignored, as it is a comment! Which means that the shebang most friendly with POSIX is: -```shell +[source,shell] +---- #!/bin/sh set -eu -``` +---- -1. when running via `./script.sh`, if the system has an executable at `/bin/sh`, it will be used to run the script; -2. when running via `sh script.sh`, the sh options aren't ignored as previously. +. when running via `./script.sh`, if the system has an executable at `/bin/sh`, + it will be used to run the script; +. when running via `sh script.sh`, the sh options aren't ignored as previously. TIL. - -[posix-awk-0]: {% link _tils/2020-12-15-awk-snippet-shellcheck-all-scripts-in-a-repository.md %} -[posix-awk-1]: {% link _tils/2021-01-12-awk-snippet-send-email-to-multiple-recipients-with-curl.md %} |