Skip to content

Use /usr/bin/env sh instead of direct path of sh#1051

Merged
typicode merged 1 commit intotypicode:mainfrom
skhaz:shell
Feb 17, 2022
Merged

Use /usr/bin/env sh instead of direct path of sh#1051
typicode merged 1 commit intotypicode:mainfrom
skhaz:shell

Conversation

@skhaz
Copy link
Copy Markdown
Contributor

@skhaz skhaz commented Sep 24, 2021

According with the best practices of shell scripting, the right way to invoke the shebang is #!/usr/bin/env shell

Reference: https://www.cyberciti.biz/tips/finding-bash-perl-python-portably-using-env.html

@Maxim-Mazurok
Copy link
Copy Markdown

This doesn't work in my Ubuntu 20 (WSL):

$ /usr/bin/env shell
/usr/bin/env: ‘shell’: No such file or directory

@Maxim-Mazurok
Copy link
Copy Markdown

I wanted to use bash, this worked for me:

# Bash sets the BASH environment variable, so if it is not set, then we
# are running in a different shell, so manually run ourselves in BASH.
# source: https://github.com/typicode/husky/issues/971#issuecomment-846431179
if [ -z "${BASH:-}" ]; then
  exec bash "$0" "$@"
fi

Credit: #971 (comment)

@nathandao
Copy link
Copy Markdown

nathandao commented Sep 27, 2021

This doesn't work in my Ubuntu 20 (WSL):

$ /usr/bin/env shell
/usr/bin/env: ‘shell’: No such file or directory

hmmm, does using /usr/bin/env sh instead of shell work for you? 🤔

@nathandao
Copy link
Copy Markdown

@skhaz since you're already on it, would it be possible to update other parts of the code that are using #!/bin/sh as well? The files I found are:

.husky/commit-msg
.husky/pre-commit
docs/README.md
./husky.sh

@Maxim-Mazurok
Copy link
Copy Markdown

This doesn't work in my Ubuntu 20 (WSL):

$ /usr/bin/env shell
/usr/bin/env: ‘shell’: No such file or directory

hmmm, does using /usr/bin/env sh instead of shell work for you? 🤔

Yes, it does. Opens up sh for me. Same with /usr/bin/env bash - opens up bash for me.

@Maxim-Mazurok
Copy link
Copy Markdown

Also, I can see that in the linked article they mention #!/usr/bin/env bash not #!/usr/bin/env shell:

Let us find out why is it a good idea to use #!/usr/bin/env bash instead of #!/bin/bash as shebang?
(source)

So, I guess that it was a typo, @skhaz?

@skhaz
Copy link
Copy Markdown
Contributor Author

skhaz commented Sep 27, 2021

@Maxim-Mazurok do you want bash instead of sh? I am not sure if macOS cames with bash, sh is a more secure option

@skhaz
Copy link
Copy Markdown
Contributor Author

skhaz commented Sep 27, 2021

@nathandao done, changed all references of /bin/sh

@Maxim-Mazurok
Copy link
Copy Markdown

@Maxim-Mazurok do you want bash instead of sh? I am not sure if macOS cames with bash, sh is more secure option

Yes, I want bash because I am letting developers configure if they want to lint and unit test their code in pre-commit hook. I'm storing scrips in array, and then passing this array of scripts to npm-run-all. Didn't find an easy way to do this with sh. Also, I'm only concerned about Ubuntu and Windows. And windows git client comes with git bash, so I guess it's pretty safe for me. Mac may think differently ;)

@Maxim-Mazurok
Copy link
Copy Markdown

Maxim-Mazurok commented Sep 27, 2021

This is my code:

scripts=("npm:sort -- --check" "npm:spell-check" "npm:prettier:check")
[[ "$PRE_COMMIT_LINT" == true ]] && scripts+=("npm:test:lint")
[[ "$PRE_COMMIT_TEST_UNIT" == true ]] && scripts+=("npm:test:unit")
npx -y concurrently --kill-others-on-fail "${scripts[@]}"

But sh/bash is not related to this issue PR, so never mind :)

@skhaz
Copy link
Copy Markdown
Contributor Author

skhaz commented Sep 27, 2021

@Maxim-Mazurok you can use bash by invoking the script inside the hook, for example:

Your hook

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

your_script.sh || exit

And in your_script.sh you have bash specific things:

#!/bin/bash

your code for bash

Or... Manually change to bash your hook

@Maxim-Mazurok
Copy link
Copy Markdown

Or... Manually change to bash your hook

This doesn't work (changing shebang in hook), because husky.sh resets shell to be sh.

@Maxim-Mazurok you can use bash by invoking the script inside the hook, for example:

Your hook

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

your_script.sh || exit

And in your_script.sh you have bash specific things:

#!/bin/bash

your code for bash

This is a good idea, thanks

@stale
Copy link
Copy Markdown

stale Bot commented Nov 28, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the wontfix label Nov 28, 2021
@Maxim-Mazurok
Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Occuring an activity

@stale stale Bot removed the wontfix label Nov 28, 2021
@skhaz
Copy link
Copy Markdown
Contributor Author

skhaz commented Nov 29, 2021

Bump

@stale
Copy link
Copy Markdown

stale Bot commented Jan 28, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the wontfix label Jan 28, 2022
@Maxim-Mazurok
Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Occuring an activity

@stale stale Bot removed the wontfix label Jan 28, 2022
@typicode typicode merged commit 81b2e2e into typicode:main Feb 17, 2022
@typicode
Copy link
Copy Markdown
Owner

Thank you for the PR 👍

kodiakhq Bot referenced this pull request in ToppleTheNun/npm-lockfile-version May 8, 2022
[![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [husky](https://typicode.github.io/husky) ([source](https://togithub.com/typicode/husky)) | [`7.0.4` -> `8.0.0`](https://renovatebot.com/diffs/npm/husky/7.0.4/8.0.0) | [![age](https://badges.renovateapi.com/packages/npm/husky/8.0.0/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/npm/husky/8.0.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/npm/husky/8.0.0/compatibility-slim/7.0.4)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/npm/husky/8.0.0/confidence-slim/7.0.4)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>typicode/husky</summary>

### [`v8.0.0`](https://togithub.com/typicode/husky/releases/v8.0.0)

[Compare Source](https://togithub.com/typicode/husky/compare/v7.0.4...v8.0.0)

#### What's Changed

##### Feats

-   feat: add `husky -` prefix to logged global error messages by [@&#8203;joshbalfour](https://togithub.com/joshbalfour) in [https://github.com/typicode/husky/pull/1092](https://togithub.com/typicode/husky/pull/1092)
-   feat: show `PATH` when command not found to improve debuggability
-   feat: drop Node 12 support
-   feat: skip install if `$HUSKY=0`

##### Fixes

-   fix: hook script use `/usr/bin/env sh` instead of direct path of `sh` by [@&#8203;skhaz](https://togithub.com/skhaz) in [https://github.com/typicode/husky/pull/1051](https://togithub.com/typicode/husky/pull/1051)
-   fix: actually set 'husky_skip_init' as readonly in `./husky.sh` by [@&#8203;hyperupcall](https://togithub.com/hyperupcall) in [https://github.com/typicode/husky/pull/1104](https://togithub.com/typicode/husky/pull/1104)
-   fix: force `basename`/`dirname` to treat `$0` as an argument by [@&#8203;mataha](https://togithub.com/mataha) in [https://github.com/typicode/husky/pull/1132](https://togithub.com/typicode/husky/pull/1132)
-   fix: remove `git.io` links by [@&#8203;renbaoshuo](https://togithub.com/renbaoshuo) in [https://github.com/typicode/husky/pull/1136](https://togithub.com/typicode/husky/pull/1136)

##### Docs

-   docs: fix uninstall via npm by [@&#8203;pddpd](https://togithub.com/pddpd) in [https://github.com/typicode/husky/pull/1033](https://togithub.com/typicode/husky/pull/1033)
-   docs: add dog emoji as favicon by [@&#8203;jamiehaywood](https://togithub.com/jamiehaywood) in [https://github.com/typicode/husky/pull/1095](https://togithub.com/typicode/husky/pull/1095)
-   docs: replace deprecated `npx --no-install` option with `npx --no` by [@&#8203;sibiraj-s](https://togithub.com/sibiraj-s) in [https://github.com/typicode/husky/pull/1070](https://togithub.com/typicode/husky/pull/1070)
-   docs: add `pnpm` installation by [@&#8203;MohamadKh75](https://togithub.com/MohamadKh75) in [https://github.com/typicode/husky/pull/1139](https://togithub.com/typicode/husky/pull/1139)

##### Chore

-   chore: update workflows by [@&#8203;tiziodcaio](https://togithub.com/tiziodcaio) in [https://github.com/typicode/husky/pull/1125](https://togithub.com/typicode/husky/pull/1125)

</details>

---

### Configuration

📅 **Schedule**: At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/ToppleTheNun/npm-lockfile-version).
hoonoh referenced this pull request in hoonoh/aws-spot-price May 9, 2022
[![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [husky](https://typicode.github.io/husky) ([source](https://togithub.com/typicode/husky)) | [`7.0.4` -> `8.0.0`](https://renovatebot.com/diffs/npm/husky/7.0.4/8.0.0) | [![age](https://badges.renovateapi.com/packages/npm/husky/8.0.0/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/npm/husky/8.0.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/npm/husky/8.0.0/compatibility-slim/7.0.4)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/npm/husky/8.0.0/confidence-slim/7.0.4)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>typicode/husky</summary>

### [`v8.0.0`](https://togithub.com/typicode/husky/releases/v8.0.0)

[Compare Source](https://togithub.com/typicode/husky/compare/v7.0.4...v8.0.0)

#### What's Changed

##### Feats

-   feat: add `husky -` prefix to logged global error messages by [@&#8203;joshbalfour](https://togithub.com/joshbalfour) in [https://github.com/typicode/husky/pull/1092](https://togithub.com/typicode/husky/pull/1092)
-   feat: show `PATH` when command not found to improve debuggability
-   feat: drop Node 12 support
-   feat: skip install if `$HUSKY=0`

##### Fixes

-   fix: hook script use `/usr/bin/env sh` instead of direct path of `sh` by [@&#8203;skhaz](https://togithub.com/skhaz) in [https://github.com/typicode/husky/pull/1051](https://togithub.com/typicode/husky/pull/1051)
-   fix: actually set 'husky_skip_init' as readonly in `./husky.sh` by [@&#8203;hyperupcall](https://togithub.com/hyperupcall) in [https://github.com/typicode/husky/pull/1104](https://togithub.com/typicode/husky/pull/1104)
-   fix: force `basename`/`dirname` to treat `$0` as an argument by [@&#8203;mataha](https://togithub.com/mataha) in [https://github.com/typicode/husky/pull/1132](https://togithub.com/typicode/husky/pull/1132)
-   fix: remove `git.io` links by [@&#8203;renbaoshuo](https://togithub.com/renbaoshuo) in [https://github.com/typicode/husky/pull/1136](https://togithub.com/typicode/husky/pull/1136)

##### Docs

-   docs: fix uninstall via npm by [@&#8203;pddpd](https://togithub.com/pddpd) in [https://github.com/typicode/husky/pull/1033](https://togithub.com/typicode/husky/pull/1033)
-   docs: add dog emoji as favicon by [@&#8203;jamiehaywood](https://togithub.com/jamiehaywood) in [https://github.com/typicode/husky/pull/1095](https://togithub.com/typicode/husky/pull/1095)
-   docs: replace deprecated `npx --no-install` option with `npx --no` by [@&#8203;sibiraj-s](https://togithub.com/sibiraj-s) in [https://github.com/typicode/husky/pull/1070](https://togithub.com/typicode/husky/pull/1070)
-   docs: add `pnpm` installation by [@&#8203;MohamadKh75](https://togithub.com/MohamadKh75) in [https://github.com/typicode/husky/pull/1139](https://togithub.com/typicode/husky/pull/1139)

##### Chore

-   chore: update workflows by [@&#8203;tiziodcaio](https://togithub.com/tiziodcaio) in [https://github.com/typicode/husky/pull/1125](https://togithub.com/typicode/husky/pull/1125)

</details>

---

### Configuration

📅 **Schedule**: "after 5pm on Friday,every weekend" (UTC).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/hoonoh/aws-spot-price).

Co-authored-by: Renovate Bot <bot@renovateapp.com>
kodiakhq Bot referenced this pull request in mheob/vscode-snippets May 14, 2022
[![WhiteSource Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [husky](https://typicode.github.io/husky) ([source](https://togithub.com/typicode/husky)) | [`^7.0.4` -> `^8.0.0`](https://renovatebot.com/diffs/npm/husky/7.0.4/8.0.1) | [![age](https://badges.renovateapi.com/packages/npm/husky/8.0.1/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/npm/husky/8.0.1/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/npm/husky/8.0.1/compatibility-slim/7.0.4)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/npm/husky/8.0.1/confidence-slim/7.0.4)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>typicode/husky</summary>

### [`v8.0.1`](https://togithub.com/typicode/husky/releases/v8.0.1)

[Compare Source](https://togithub.com/typicode/husky/compare/v8.0.0...v8.0.1)

-   fix: use POSIX equality operator

### [`v8.0.0`](https://togithub.com/typicode/husky/releases/v8.0.0)

[Compare Source](https://togithub.com/typicode/husky/compare/v7.0.4...v8.0.0)

#### What's Changed

##### Feats

-   feat: add `husky -` prefix to logged global error messages by [@&#8203;joshbalfour](https://togithub.com/joshbalfour) in [https://github.com/typicode/husky/pull/1092](https://togithub.com/typicode/husky/pull/1092)
-   feat: show `PATH` when command not found to improve debuggability
-   feat: drop Node 12 support
-   feat: skip install if `$HUSKY=0`

##### Fixes

-   fix: hook script use `/usr/bin/env sh` instead of direct path of `sh` by [@&#8203;skhaz](https://togithub.com/skhaz) in [https://github.com/typicode/husky/pull/1051](https://togithub.com/typicode/husky/pull/1051)
-   fix: actually set 'husky_skip_init' as readonly in `./husky.sh` by [@&#8203;hyperupcall](https://togithub.com/hyperupcall) in [https://github.com/typicode/husky/pull/1104](https://togithub.com/typicode/husky/pull/1104)
-   fix: force `basename`/`dirname` to treat `$0` as an argument by [@&#8203;mataha](https://togithub.com/mataha) in [https://github.com/typicode/husky/pull/1132](https://togithub.com/typicode/husky/pull/1132)
-   fix: remove `git.io` links by [@&#8203;renbaoshuo](https://togithub.com/renbaoshuo) in [https://github.com/typicode/husky/pull/1136](https://togithub.com/typicode/husky/pull/1136)

##### Docs

-   docs: fix uninstall via npm by [@&#8203;pddpd](https://togithub.com/pddpd) in [https://github.com/typicode/husky/pull/1033](https://togithub.com/typicode/husky/pull/1033)
-   docs: add dog emoji as favicon by [@&#8203;jamiehaywood](https://togithub.com/jamiehaywood) in [https://github.com/typicode/husky/pull/1095](https://togithub.com/typicode/husky/pull/1095)
-   docs: replace deprecated `npx --no-install` option with `npx --no` by [@&#8203;sibiraj-s](https://togithub.com/sibiraj-s) in [https://github.com/typicode/husky/pull/1070](https://togithub.com/typicode/husky/pull/1070)
-   docs: add `pnpm` installation by [@&#8203;MohamadKh75](https://togithub.com/MohamadKh75) in [https://github.com/typicode/husky/pull/1139](https://togithub.com/typicode/husky/pull/1139)

##### Chore

-   chore: update workflows by [@&#8203;tiziodcaio](https://togithub.com/tiziodcaio) in [https://github.com/typicode/husky/pull/1125](https://togithub.com/typicode/husky/pull/1125)

</details>

---

### Configuration

📅 **Schedule**: "before 3am on Friday" (UTC).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#github/mheob/vscode-snippets).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants