{"id":14156600,"url":"https://github.com/spack/sbang","last_synced_at":"2025-05-06T06:30:48.661Z","repository":{"id":46071047,"uuid":"307862620","full_name":"spack/sbang","owner":"spack","description":"Run scripts with very long shebang (#!) lines","archived":false,"fork":false,"pushed_at":"2021-11-16T15:40:34.000Z","size":31,"stargazers_count":68,"open_issues_count":1,"forks_count":5,"subscribers_count":36,"default_branch":"main","last_synced_at":"2024-08-17T08:07:35.320Z","etag":null,"topics":["script","shebang","shell"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spack.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-10-28T00:26:00.000Z","updated_at":"2024-06-03T14:21:10.000Z","dependencies_parsed_at":"2022-08-20T17:20:35.658Z","dependency_job_id":null,"html_url":"https://github.com/spack/sbang","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spack%2Fsbang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spack%2Fsbang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spack%2Fsbang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spack%2Fsbang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spack","download_url":"https://codeload.github.com/spack/sbang/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224490710,"owners_count":17319983,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["script","shebang","shell"],"created_at":"2024-08-17T08:06:57.993Z","updated_at":"2024-11-13T17:07:50.252Z","avatar_url":"https://github.com/spack.png","language":"Shell","funding_links":[],"categories":["Shell","shell"],"sub_categories":[],"readme":"# sbang\n![linux](https://github.com/spack/sbang/workflows/linux/badge.svg)\n![macos](https://github.com/spack/sbang/workflows/macos/badge.svg)\n![shellcheck](https://github.com/spack/sbang/workflows/shellcheck/badge.svg)\n[![codecov](https://codecov.io/gh/spack/sbang/branch/main/graph/badge.svg?token=IKH7mB5qq7)](https://codecov.io/gh/spack/sbang)\n\n`sbang` lets you run scripts with very long shebang (`#!`) lines.\n\nMany operating systems limit the length and number of possible arguments\nin shebang lines, making it hard to use interpreters that are deep in the\ndirectory hierarchy or require special arguments.\n\nTo use, put the long shebang on the second line of your script, and\nmake sbang the interpreter, like this:\n\n```sh\n#!/bin/sh /path/to/sbang\n#!/long/path/to/real/interpreter with many arguments\n```\n\n`sbang` will run the real interpreter with the script as its argument.\n\n## Why?\n\nMost people don't have long shebang problems. They can come up if you\ninstall software in deeply nested directories. e.g., in your home\ndirectory (with something like [Spack](https://github.com/spack/spack)),\nor in a shared project directory on an NFS volume. It also comes up in\ndeeply nested [virtual environments](https://github.com/pypa/virtualenv),\nwhere the `python` interpreter is copied into a deep path.\n\nGenerally, `sbang` is useful for user-installed code. Admins who have\nroot and can put software wherever they want will likely not need it.\n\n### Long shebangs\n\nSuppose you have a script, `long-shebang.sh`, like this:\n\n```sh\n#!/very/very/long/path/to/some/interp\n\necho \"success!\"\n```\n\nIf `very/long/path` is actually very long, running this script will\nresult in an error on some OS's. On Linux, you get an error this:\n\n```console\n$ ./long-shebang.sh\n-bash: ./long=shebang.sh: /very/very/long/path/to/some/interp: bad interpreter:\n       No such file or directory\n```\n\nOn macOS, things are worse. The system doesn't consider the long\ninterpreter path, and just tries to run the script with the shell. This\nis not likely to be what you intended.\n\n### Shebangs with arguments\n\nPassing arguments on the shebang line is an issue.  Consider:\n\n```sh\n#!/path/to/interp -a -b -c\n\n...\n```\n\nDepending on your OS, `interp` may end up receiving a single argument\nlike `\"-a -b -c\"` instead of three separate arguments (`\"-a\"`, `\"-b\"`,\n`\"-c\"`). `sbang` will delegate shebang arguments separately, as you would\nexpect, so you can do this:\n\n```sh\n#!/bin/sh /path/to/sbang\n#!/path/to/interp -a -b -c\n\n...\n```\n\n### Further reading\n\nThere's a really comprehensive writeup on the history and limitations of\nthe shebang mechanism at\nhttps://www.in-ulm.de/~mascheck/various/shebang/.\n\n\n## Using `sbang`\n\nYou can use `sbang` in several ways.\n\n### `sbang` on the command line\n\nYou can use `sbang` in two ways. You can use it directly, from the\ncommand line, like this:\n\n```console\n$ sbang ./long-shebang.sh\nsuccess!\n```\n\n### `sbang` as the interpreter\n\nYou can also use `sbang` *as* the interpreter for your script. Put\n`#!/bin/sh /path/to/sbang` on line 1, and move the original shebang to\nline 2 of the script:\n\n```sh\n#!/bin/sh /path/to/sbang\n#!/long/path/to/real/interpreter with arguments\n\necho \"success!\"\n```\n\n```console\n$ ./long-shebang.sh\nsuccess!\n```\n\nOn Linux, you could shorten line 1 to `#!/path/to/sbang`, but other\noperating systems like Mac OS X require the interpreter to be a binary,\nso it's best to use `sbang` as an argument to `/bin/sh`. Obviously, for\nthis to work, `sbang` needs to have a short enough path that *it* will\nrun without hitting OS limits.\n\n### Other comment syntaxes\n\nFor Lua, node, and php scripts, the second line can't start with `#!`, as\n`#` is not the comment character in these languages (though they all\nignore `#!` on the *first* line of a script). Instrument such scripts\nlike this, using `--`, `//`, or `\u003c?php ... ?\u003e` instead of `#` on the\nsecond line, e.g.:\n\n```sh\n#!/bin/sh /path/to/sbang\n--!/long/path/to/lua with arguments\nprint \"success!\"\n```\n\n```sh\n#!/bin/sh /path/to/sbang\n//!/long/path/to/node with arguments\nprint \"success!\"\n```\n\n```sh\n#!/bin/sh /path/to/sbang\n\u003c?php #/long/path/to/php with arguments ?\u003e\n\u003c?php echo \"success!\\n\"; ?\u003e\n```\n\n## How it works\n\n`sbang` is a very simple POSIX shell script. It looks at the first two\nlines of a script argument and runs the last line starting with `#!`,\nwith the script as an argument. It also forwards arguments. Because it's\nsimple POSIX, you can use it almost anywhere.\n\n\n## Authors\n\n`sbang` was created by Todd Gamblin, tgamblin@llnl.gov, as part of\n[Spack](https://github.com/spack/spack).\n\n## Related projects\n\nThe [long-shebang](https://github.com/shlevy/long-shebang) project is\nlike `sbang` but written in C instead of POSIX `sh`. It grew out of\n[Nix](https://github.com/nixos/nix) a few months after `sbang` grew out\nof Spack, for similar reasons.\n\n## License\n\n`sbang` is distributed under the terms of both the MIT license and the\nApache License (Version 2.0). Users may choose either license, at their\noption.\n\nAll new contributions must be made under both the MIT and Apache-2.0\nlicenses.\n\nSee [LICENSE-MIT](https://github.com/spack/sbang/blob/develop/LICENSE-MIT),\n[LICENSE-APACHE](https://github.com/spack/sbang/blob/develop/LICENSE-APACHE),\n[COPYRIGHT](https://github.com/spack/sbang/blob/develop/COPYRIGHT), and\n[NOTICE](https://github.com/spack/sbang/blob/develop/NOTICE) for details.\n\nSPDX-License-Identifier: (Apache-2.0 OR MIT)\n\nLLNL-CODE-816912\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspack%2Fsbang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspack%2Fsbang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspack%2Fsbang/lists"}