Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
19
votes
2
answers
22354
views
What does the `-eu` mean in `#!/bin/bash -eu` at the top of a bash script? (or any of `-abefhkmnptuvxBCHP`)
What does the `eu` mean after `#!/bin/bash -eu` at the top of a bash script? Normally I begin my bash scripts with this [hashbang/shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)): #!/bin/bash but I just came across one with #!/bin/bash -eu and I have no idea why there is a `-eu` there. Reading...
What does the
eu
mean after #!/bin/bash -eu
at the top of a bash script?
Normally I begin my bash scripts with this [hashbang/shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) :
#!/bin/bash
but I just came across one with
#!/bin/bash -eu
and I have no idea why there is a -eu
there. Reading the man bash
pages doesn't seem to help me, but maybe I'm overlooking something.
---
**Not a duplicate:**
This is not a duplicate of https://unix.stackexchange.com/questions/208112/correct-behavior-of-exit-and-err-traps-when-using-set-eu .
[Quoting @ilkkachu in the comments below this question](https://unix.stackexchange.com/questions/595249/what-does-the-eu-mean-in-bin-bash-eu-at-the-top-of-a-bash-script-or-a#comment1110949_595249) , directly addressing this:
> ...how -e and -u work with regard to traps or anything else is completely unrelated to how they and other single-character options can be given on the command line.
I agree with that. These are separate questions and answers with differing motivations behind them. That question is so different I would never even think to click on it by looking at its title OR its description when trying to understand the answer to my own question here, and the answers are vastly different too.
Gabriel Staples
(2972 rep)
Jun 26, 2020, 05:42 PM
• Last activity: Jun 24, 2025, 11:33 PM
13
votes
1
answers
67238
views
No such file or directory but I can see it!
I'm trying to run a python script, on a headless Raspberry PI using winSCP and get the following error message: Command '"./areadetect_movie_21.py"' failed with return code 127 and error message /usr/bin/env: python : No such file or directory. When I try and run from terminal, I get: : No such file...
I'm trying to run a python script, on a headless Raspberry PI using winSCP and get the following error message:
Command '"./areadetect_movie_21.py"'
failed with return code 127 and error message
/usr/bin/env: python
: No such file or directory.
When I try and run from terminal, I get:
: No such file or directory.
I try a similar python script, in the same directory, with the same python shebang, the same permissions and using the same user pi, and it works.
I also do a
ls
and I can see the file, so I don't know why it will not run.
reggie
(533 rep)
Mar 10, 2015, 12:34 PM
• Last activity: Jun 12, 2025, 04:42 PM
-3
votes
2
answers
226
views
Is Bourne shell shebang #!/bin/sh expecting BASH POSIX-correct?
I've recently been playing with the legacy Bourne Shell from the GitHub repo [heirloom-sh](https://github.com/grml/heirloom-sh). It's installed at `/usr/local/bin/sh` with no symlinks. One script is invoking it via `#!/bin/env sh`. *I'm running Manjaro with system and packages fully up to date as of...
I've recently been playing with the legacy Bourne Shell from the GitHub repo [heirloom-sh](https://github.com/grml/heirloom-sh) . It's installed at
/usr/local/bin/sh
with no symlinks. One script is invoking it via #!/bin/env sh
.
*I'm running Manjaro with system and packages fully up to date as of this post (vscodium-bin
1.99.02289-1, Manjaro 6.12.20-2-MANJARO). I posted concisely on their forum , but this issue goes way beyond even Arch stacks, affecting all Linux development.*
## Background
Most of our Linux scripts are written for BASH (/bin/bash
) and cannot work for Bourne Shell (implied by /bin/sh
, but not reliable ). But, many coders still put #!/bin/sh
for the shebang (#!/...
) anyway, then code as if for the BASH #!/bin/bash
.
For most systems, /bin/sh
is a link to /bin/bash
, letting everything for BASH work in a mis-labaled Bourne Shell script. This has resulted in some sloppy programming practices and breaks the rule from Chemistry class: "Never make labels lie."
For Debian, /bin/sh
is a link to /bin/dash
(Debian Almquist shell, POSIX minimal), carrying many Bourne Shell limits. So, Debian/Ubuntu devs might not run into the same problems of BASH scripts still working with #!/bin/sh
. Thus, this may mostly be a non-Debian dev issue.
...mostly.
# Examples of resulting errors
## Eg. VSCodium
I first noticed this as a non-system issue with the AUR packages vscodium and vscodium-bin (I'm using vscodium-bin
).
/opt/vscodium-bin/bin/codium
(where VSCodium resides) had this as line 1:
#!/usr/bin/env sh
...which would not start from the GUI, and then from the CLI threw this error:
/opt/vscodium-bin/bin/codium: syntax error at line 20: `YN=$' unexpected
I had two solutions:
1. Comment and changed the shebang line to this:
#!/usr/bin/bash
...then it worked.
2. Remove /usr/local/bin/sh
(my legacy sandbox authentic Bourne shell, see above)...
I reversed line 1 that so that /opt/vscodium-bin/bin/codium
once again had this as line 1:
#!/usr/bin/env sh
...as it does OOB, then I removed /usr/local/bin/sh
.
Then it also worked.
***Technically, #!/usr/bin/env sh
might not be POSIX compliant if it invokes BASH-only code.***
So, that would be an example of this coding issue happening with an actual project, VSCodium specifically.
## Eg. Manjaro Boot
I mentioned this in my Manjaro Forum post , but basically having /bin/sh
as an actual, true legacy /bin/sh
Bourne shell broke my boot sequence.
That tells me that many of my distro's most-current startup boot processes and scripts are actually using BASH code with a Bourne shell shebang.
On Arch, not only Manjaro, /bin/sh
is a symlink to bash
, presuming the same PWD; it isn't even an absolute link, but a relative link!
Arch:
# ls -l /bin/sh
lrwxrwxrwx 1 root root 4 May 5 2025 /bin/sh -> bash
The boot issue on Manjaro is unlikely from developers changing an Arch file with #!/bin/bash
into #!/bin/sh
. Many of our implied, forgotten, invoked system startup scripts are probably inherited from the runlevel era. This is a legacy issue, after all.
I'm scratching my head on how in the Linux-world that was even able to come from mainstream distro-level developers.
# Summary
We still have Bourne Shell artifacts in the form of #!/bin/sh
floating around out there, even in our most current Linux distros, still being created in the developer world. If ever /bin/sh
actually is /bin/sh
, many Linux systems won't boot! This makes big problems for any portability.
In fact, this might even be a security issue: Make /bin/sh
actually become /bin/sh
and every Linux server can't reboot.
___
These surrounding questions come to mind. How far does it reach into our Unix/Linux world to have /bin/sh --> bash
?
Is it POSIX compliant to use #!/usr/bin/env sh
, then invoke code that only works with #!/bin/bash
*and does not work* with #!/bin/sh
? If POSIX compliant, why and would it break any other coding guidelines?
Has the Linux world addressed BOLO to make sure we aren't using Bourne Shell (#!/bin/sh
) in our scripts unless we are writing for the legacy Bourne Shell last updated for capabilities in 1989? Should we all be looking through our code and removing any #!/bin/sh
meant to run on modern systems that probably have a link to /bin/bash
anyway? Even consider removing the /bin/sh
link to /bin/bash
altogether?
In light of not relying on sh at /bin/sh , if we thus should not use #!/bin/sh
in referencing BASH code (that is if), then *theoretically* should Debian scripts use #!/bin/dash
instead of #!/bin/sh
? Or, is Debian's dash
close enough to sh
that it fits current POSIX expectations?
How much has this already been discussed and what are the current resolutions across the Linux world?
Is there another reason we need "labels to lie" and that using #!/bin/sh
-labeled code (that can't run on actual 1989 Bourne Shell's /bin/sh
) is a good idea?
Does this issue only affect Linux or also Unix? Do Apple scripts use #!/bin/sh
with symlink?
Jesse
(355 rep)
Apr 5, 2025, 08:17 PM
• Last activity: Apr 12, 2025, 04:30 PM
0
votes
0
answers
30
views
What are the rules behind finding executables in shebang statements?
I want to create a Docker image that has the Python3 runtime, a Python script of my creation, update-alternatives configured to run `python3` when `python` is invoked, and I want to make the Python script "executable" by adding a shebang line at the top. Putting that all together looks like this: ``...
I want to create a Docker image that has the Python3 runtime, a Python script of my creation, update-alternatives configured to run
python3
when python
is invoked, and I want to make the Python script "executable" by adding a shebang line at the top.
Putting that all together looks like this:
# dockerfile
from ubuntu:24.04
run apt-get update \
&& apt-get install -y python3 \
&& update-alternatives --install /usr/bin/python python /usr/bin/python3 1
copy my_python_exe.py /usr/local/bin
#! python
# my_python_exe.py
def main():
print("Hello, world!")
if __name__ == "__main__":
main()
$ chmod +x ./my_python_exe.py
$
How I build the docker image:
$ docker build -t tmp .
[+] Building 3.9s (8/8) FINISHED docker:default
=> [internal] load build definition from dockerfile 0.1s
=> => transferring dockerfile: 242B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:24.04 0.1s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/3] FROM docker.io/library/ubuntu:24.04@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782 2.5s
=> => resolve docker.io/library/ubuntu:24.04@sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782 2.5s
=> [internal] load build context 0.0s
=> => transferring context: 149B 0.0s
=> CACHED [2/3] RUN apt-get update && apt-get install -y python3 && update-alternatives --install /usr/bin/python python /usr/bin/python3 1 0.0s
=> [3/3] COPY my_python_exe.py /usr/local/bin 0.1s
=> exporting to image 0.6s
=> => exporting layers 0.3s
=> => exporting manifest sha256:6ba674667609ecb5557b74c26c1575de071aa7df447700347d10d8f01001b112 0.0s
=> => exporting config sha256:31817234dbeddca20209469f49e623143733bc2353e2630c500de16a51356ea6 0.0s
=> => exporting attestation manifest sha256:39c41491e0ad0229401709a3b32c411cd3bd6d552c59c7aec2afc2d12deea269 0.1s
=> => exporting manifest list sha256:849fd19a3455b4d7c4dae89f469680545e51e46156978a21222bdf6e30505e01 0.0s
=> => naming to docker.io/library/tmp:latest 0.0s
=> => unpacking to docker.io/library/tmp:latest 0.0s
How I run the docker container:
$ docker run --rm -it --user $(id -u):$(id -g) -v $HOME:$HOME -w $HOME -v /etc/passwd:/etc/passwd:ro tmp
stonethrow@a59e51ccedf0:~$
The path within the running docker container context:
stonethrow@0b6d6819f651:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
How I run my Python script and **the error** that I'm asking about:
stonethrow@0b6d6819f651:~$ my_python_exe.py
bash: /usr/local/bin/my_python_exe.py: cannot execute: required file not found
What is that "cannot execute: required file not found
" error? Naively, I feel like a reasonable trail of breadcrumbs has been left for the command python
to resolve to /usr/bin/python3
:
- Python3 has been installed
- I've configured update-alternatives to establish the symlink /usr/bin/python
to point to /usr/bin/python3
(which itself points to the executable /usr/bin/python3.12
itself)
- My Python script uses the shebang convention -- which, per my, understanding tells the invoking shell what executable to use to run the script -- to specify that python
should be used to execute the script.
- $PATH
includes /usr/bin/
, where the python
symlink exists.
**Why does that "cannot execute: required file not found
" error occur?**
---
What I have tried:
1. Explicitly running python my_python_exe.py
in the docker container context works:
stonethrow@0b6d6819f651:~$ python /usr/local/bin/my_python_exe.py
Hello, world!
2. Modifying the shebang statement from #! python
to #! /usr/bin/python3
works (for the following, all code/dockerfiles are the same as provided above, *except for* the shebang statement in the Python script:
#! /usr/bin/python3
# my_python_exe.py
...
stonethrow@25ac7da86227:~$ my_python_exe.py
Hello, world!
3. Modifying the shebang statement from #! python
to #! /usr/bin/python
works:
#! /usr/bin/python
# my_python_exe.py
...
stonethrow@25ac7da86227:~$ my_python_exe.py
Hello, world!
4. Modifying the shebang statement to #! /usr/bin/env python
works:
#! /usr/bin/env python
# my_python_exe.py
...
stonethrow@25ac7da86227:~$ my_python_exe.py
Hello, world!
5. Modifying the shebang statement to #! python3
enables the error:
#! python3
# my_python_exe.py
...
stonethrow@0fdb8af3f7d6:~$ my_python_exe.py
bash: /usr/local/bin/my_python_exe.py: cannot execute: required file not found
6. Modifying the shebang statement to #! python3.12
enables the error:
#! python3.12
# my_python_exe.py
...
stonethrow@0fdb8af3f7d6:~$ my_python_exe.py
bash: /usr/local/bin/my_python_exe.py: cannot execute: required file not found
7. Modifying the shebang statement to #! env python
enables the error (and so does #! env python3
and #! env python3.12
):
#! /usr/bin/env python
# my_python_exe.py
...
stonethrow@0fdb8af3f7d6:~$ my_python_exe.py
bash: /usr/local/bin/my_python_exe.py: cannot execute: required file not found
---
The pattern from what I've tried implies that $PATH
isn't used while trying to run the executable referenced in shebang statements and/or you need to use the executable env
for $PATH
to be used, but of course, since $PATH
itself isn't in effect, in order to use env
you have to specify its full path. But that's conjecture; I'd be grateful if someone could set me straight on the underlying rules at play here.
StoneThrow
(1937 rep)
Mar 30, 2025, 09:26 PM
2
votes
2
answers
1404
views
bash shebang on macOS
On macOS, `#!/bin/bash` means a script will use the system bash, which is very dated (3.2, 2006 year) and (for myself, at least) mostly useless. Then you can install modern bash using Homebrew or MacPorts, and use something like this: ``` #!/usr/bin/env bash ``` [But the `env` approach is not reliab...
On macOS,
#!/bin/bash
means a script will use the system bash, which is very dated (3.2, 2006 year) and (for myself, at least) mostly useless.
Then you can install modern bash using Homebrew or MacPorts, and use something like this:
#!/usr/bin/env bash
[But the env
approach is not reliable: what if you run this script on a Mac where only the system bash is installed? Then, env
will use bash 3.2 instead (I suppose...), and this will make the script to misbehave and therefore can make harm.](https://unix.stackexchange.com/questions/29608/why-is-it-better-to-use-usr-bin-env-name-instead-of-path-to-name-as-my)
Then, the third approach is
#!/opt/homebrew/bin/bash
But what if you run this script on a Mac where modern bash is installed using MacPorts instead of Homebrew?
What is the best practice here? (Maybe create a symlink in /usr/local/bin
that points to /opt/homebrew/bin/bash
, and then #!/usr/local/bin
?)
jsx97
(1347 rep)
Mar 10, 2025, 06:01 PM
• Last activity: Mar 11, 2025, 06:11 PM
52
votes
3
answers
67725
views
Use .sh or .bash extension for Bash scripts?
(See https://unix.stackexchange.com/q/182869/10043) If I want my scripts to use the Bash shell, does using the `.bash` extension actually invoke Bash or does it depend on system config or the first shebang line. If both were in effect but different, which would have precedence? I'm not sure whether...
(See https://unix.stackexchange.com/q/182869/10043)
If I want my scripts to use the Bash shell, does using the
.bash
extension actually invoke Bash or does it depend on system config or the first shebang line. If both were in effect but different, which would have precedence?
I'm not sure whether to end my scripts with .sh
to just indicate "shell script" and then have the first line select the Bash shell (e.g. #!/usr/bin/env bash
) or whether to just end them with .bash
(as well as the setting in the first line).
I want Bash to be invoked.
Michael Durrant
(43563 rep)
Feb 4, 2015, 01:37 PM
• Last activity: Feb 10, 2025, 07:53 PM
92
votes
1
answers
12045
views
Why does the following bash script delete itself?
If you create an executable file with the following contents, and run it, it will delete itself. How does this work? #!/bin/rm
If you create an executable file with the following contents, and run it, it will delete itself.
How does this work?
#!/bin/rm
Stack Exchange Broke The Law
(2294 rep)
May 3, 2016, 11:38 AM
• Last activity: Oct 2, 2024, 10:22 AM
1
votes
1
answers
118
views
shell-expand-line and shebangs
In bash, the very useful shell-expand-line (`M-C-e`) expands command substitutions to their contents, on the current line. However, this works very strangely for commands without shebangs. One may verify this by the following. 1. Create a simple executable file with and without shebang. ``` echo ech...
In bash, the very useful shell-expand-line (
M-C-e
) expands command substitutions to their contents, on the current line. However, this works very strangely for commands without shebangs.
One may verify this by the following.
1. Create a simple executable file with and without shebang.
echo echo hi >> test-no-shebang
chmod +x test-no-shebang
echo '#!/bin/sh' >> test-shebang
echo echo hi >> test-shebang
chmod +x test-shebang
2. Enter `
./test-shebang
, then
M-C-e. This works as expected, expanding the line to
hi.
C-_` works normally, undoing the expansion.
3. Enter `
./test-no-shebang
, then
M-C-e. This works strangely, indeed expanding the line to
hi, but removing the prompt in front of the line.
C-_ does not work, printing the character literally instead, and nor do most bindings, but
C-u kind of works, successfully clearing the line (but not clearing the
hi`).
Why is this the case? Can I make shell-expand-line work for scripts without shebang?
EDIT:
1. Some comments thought that it would be wise for me to mention that the mentioned key-bindings refer to the emacs line-editing bindings (which are the default bash key-bindings).
2. With more testing, I observe that I can reproduce the problem on the mac Terminal and on kitty on bash version 5.2.15(1)-release
, but cannot reproduce the problem on bash 3.2.57(1)-release
.
user22476690
(143 rep)
Aug 4, 2024, 08:35 AM
• Last activity: Aug 5, 2024, 07:38 PM
680
votes
11
answers
304953
views
Why is it better to use "#!/usr/bin/env NAME" instead of "#!/path/to/NAME" as my shebang?
I notice that some scripts which I have acquired from others have the shebang `#!/path/to/NAME` while others (using the same tool, NAME) have the shebang `#!/usr/bin/env NAME`. Both seem to work properly. In tutorials (on Python, for example), there seems to be a suggestion that the latter shebang i...
I notice that some scripts which I have acquired from others have the shebang
#!/path/to/NAME
while others (using the same tool, NAME) have the shebang #!/usr/bin/env NAME
.
Both seem to work properly. In tutorials (on Python, for example), there seems to be a suggestion that the latter shebang is better. But, I don't quite understand why this is so.
I realize that, in order to use the latter shebang, NAME must be in the PATH whereas the first shebang does not have this restriction.
Also, it appears (to me) that the first would be the better shebang, since it specifies precisely where NAME is located. So, in this case, if there are multiple versions of NAME (e.g., /usr/bin/NAME, /usr/local/bin/NAME), the first case specifies which to use.
My question is why is the first shebang preferred to the second one?
TheGeeko61
(7181 rep)
Jan 21, 2012, 01:06 AM
• Last activity: Jul 2, 2024, 06:24 AM
14
votes
1
answers
1662
views
Shebang can reference a script in Linux
I've always heard that the target of a shebang line (e.g. `#!/bin/bash`) must be a binary executable, not a script. And this is still true for many OSes (e.g. MacOS). But I was surprised to see that this is not true on Linux, where up to 4 levels of scripts can be used, where the fourth script refer...
I've always heard that the target of a shebang line (e.g.
#!/bin/bash
) must be a binary executable, not a script. And this is still true for many OSes (e.g. MacOS). But I was surprised to see that this is not true on Linux, where up to 4 levels of scripts can be used, where the fourth script references a binary executable in its shebang line. However, if 5 levels of scripts are used, then the program will fail with the error Too many levels of symbolic links
.
See the LWN article "How programs get run" and the following code which was not shown in that article.
$ cat wrapper2
#!./wrapper
When did this change occur (assuming that at some point it was not allowed)?
jrw32982
(1089 rep)
Apr 26, 2024, 04:23 PM
• Last activity: Apr 26, 2024, 05:02 PM
9
votes
1
answers
1825
views
Why does env -S with quoted strings in the shebang line work fine in Ubuntu but not in macOS?
I have the following script in an executable file `test-shebang.mjs` and I wanted to use `zx` to run my script but have my `~/.zshrc` be sourced before that: ```js #!/usr/bin/env -S zsh -c 'source ~/.zshrc; zx --install $@' -- console.log("work pls") ``` `./test-shebang.mjs` works fine in Ubuntu: ``...
I have the following script in an executable file
test-shebang.mjs
and I wanted to use zx
to run my script but have my ~/.zshrc
be sourced before that:
#!/usr/bin/env -S zsh -c 'source ~/.zshrc; zx --install $@' --
console.log("work pls")
./test-shebang.mjs
works fine in Ubuntu:
❯ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/ "
SUPPORT_URL="https://help.ubuntu.com/ "
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/ "
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy "
UBUNTU_CODENAME=jammy
❯ zsh --version
zsh 5.8.1 (x86_64-ubuntu-linux-gnu)
But when I copy over the same script in macOS, I get this error:
❯ ./test-shebang.mjs
zsh:1: unmatched '
❯ zsh --version
zsh 5.9 (x86_64-apple-darwin23.0)
❯ sw_vers
ProductName: macOS
ProductVersion: 14.4
BuildVersion: 23E214
Why does this happen?
I tried with bash
as well but running into errors there as well. FWIW, I'm doing this roundabout way of doing things because I've installed zx
via pnpm
which itself is installed via brew
and I would prefer not to set PATH
which would just make the script longer.
runofthemillgeek
(193 rep)
Apr 8, 2024, 05:06 PM
• Last activity: Apr 8, 2024, 09:06 PM
113
votes
8
answers
424143
views
#!/bin/bash - no such file or directory
I've created a bash script but when I try to execute it, I get #!/bin/bash no such file or directory I need to run the command: `bash script.sh` for it to work. How can I fix this?
I've created a bash script but when I try to execute it, I get
#!/bin/bash no such file or directory
I need to run the command:
bash script.sh
for it to work.
How can I fix this?
Nicolas de Fontenay
(4483 rep)
Dec 17, 2011, 03:25 PM
• Last activity: Apr 4, 2024, 01:20 PM
6
votes
5
answers
2399
views
Find all scripts with a given shebang line with find & sed
I want to find out all scripts with a specific shebang line. Specifically, I want all files that match the following criteria: * It's mostly a plain text file (stuffs created by `gzexe` don't look very friendly) * The 1st line contains solely `#!/bin/sh` or `#! /bin/sh` (with a space) I would like t...
I want to find out all scripts with a specific shebang line. Specifically, I want all files that match the following criteria:
* It's mostly a plain text file (stuffs created by
gzexe
don't look very friendly)
* The 1st line contains solely #!/bin/sh
or #! /bin/sh
(with a space)
I would like to do this with find
, sed
and grep
(file
available).
File names are useless, because some scripts don't have extensions or even have wrong extensions. Also a something.sh
may have a shebang line of #!/bin/bash
which is also not what I wanted.
Besides, sometimes I would come across a file like this:
#!/bin/sh blah.blah.blah...The 1st line is empty and the shebang is located at the 2nd line, which **is not what I wanted**. I am able to find shebang lines with
find|grep
but I don't know how to find lines **specifically on the 1st line** of a file.
Thanks for any help in advance.
iBug
(3638 rep)
Apr 2, 2017, 04:45 AM
• Last activity: Mar 18, 2024, 02:24 PM
0
votes
0
answers
68
views
Unable to mount from a script using shebang (kernel hardening related ?)
I stumbled across a really weird issue with `mount`. I'm unable to make sense out of it. For some context, I wrote a Python script that aims to automount hot-pluggable USB devices, but this is not relevant. Here's the mount error that occurs: ``` mount: /media/usb/backup: filesystem was mounted, but...
I stumbled across a really weird issue with
mount
. I'm unable to make sense out of it.
For some context, I wrote a Python script that aims to automount hot-pluggable USB devices, but this is not relevant.
Here's the mount error that occurs:
mount: /media/usb/backup: filesystem was mounted, but any subsequent operation failed: Operation not permitted.
I have a LUKS partition already unlocked and I'm trying to mount it using my script.
$ lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sdb 8:16 0 1.8T 0 disk
├─sdb1 8:17 0 64M 0 part
├─sdb2 8:18 0 128M 0 part
├─sdb3 8:19 0 10G 0 part
└─sdb4 8:20 0 1.8T 0 part
└─cfe1c7ab-36cc-4419-9373-764aaae1033f 254:1 0 1.8T 0 crypt
I created a minimal reproducible example which is as follow:
#!/usr/bin/bash
mount /dev/mapper/cfe1c7ab-36cc-4419-9373-764aaae1033f
The x
bit is set and when I run this script using the shebang, I get the same error as above:
$ sudo /tmp/test
mount: /media/usb/backup: filesystem was mounted, but any subsequent operation failed: Operation not permitted.
The weird thing is that if I run this exact same command in my shell, it does work:
$ sudo mount /dev/mapper/cfe1c7ab-36cc-4419-9373-764aaae1033f
$ echo $?
0
When I run my script not using the shebang but by using the bash explicitly, it works as well:
$ sudo bash /tmp/test
$ echo $?
0
So apparently there's something happening with the shebang which triggers the Operation not permitted
error. If I'm not mistaken the shebang is handled directly by the kernel, therefore I'm starting to wonder if it has anything to do with the fact I'm using ArchLinux's [linux-hardened
](https://github.com/anthraxx/linux-hardened) (which is a fork of GraphenOS kernel).
Are you aware of any Linux mechanism (mount namespace?) that could trigger that error when running a script with a shebang ? Thanks for reading me.
ShellCode
(235 rep)
Sep 3, 2023, 12:17 AM
2
votes
1
answers
12652
views
When to use #!/bin/ksh and #!/bin/sh? Need example
Also what are the main differences between these two. To execute a script is it necessary to write this at the beginning of script?
Also what are the main differences between these two. To execute a script is it necessary to write this at the beginning of script?
Nainita
(2972 rep)
Jun 21, 2015, 05:02 PM
• Last activity: May 31, 2023, 02:29 PM
1
votes
0
answers
2089
views
How can I run the file currently open in the kate editor as a python script?
I am writing Python scripts and using a shebang line to specify the location of the Python interpreter in my system.: #!/usr/bin/env python So, in principle, my system should be able to run the script correctly, using the right interpreter. This should also be true for other scripting languages, suc...
I am writing Python scripts and using a shebang line to specify the location of the Python interpreter in my system.:
#!/usr/bin/env python
So, in principle, my system should be able to run the script correctly, using the right interpreter.
This should also be true for other scripting languages, such as shell scripts, provided that I give the right path to the interpreter in the shebang line.
What I do now to run it is to open a terminal, and type:
python /path/to/my/script.py
but I am looking for a more efficient way.
So, my question is: is there a command, within Kate, which allows me to run the current file as a script?
I am trying the command "Run Current Document", under the "Tools" menu, I get a message asking if I really want to run it, but then nothing happens.
Fabio
(535 rep)
May 24, 2023, 09:16 AM
• Last activity: May 25, 2023, 12:39 PM
1
votes
2
answers
743
views
Is there a Guix equivalent of nix-shell shebangs?
I want to write a script which executes within a specific `guix shell` environment. I'm hoping there's an equivalent version of the [`nix-shell` shebang][0]. For example, it would be cool to write something similar to the following: ```bash #!guix shell #!--manifest=manifest.scm bash # ... commands...
I want to write a script which executes within a specific
guix shell
environment. I'm hoping there's an equivalent version of the nix-shell
shebang . For example, it would be cool to write something similar to the following:
#!guix shell
#!--manifest=manifest.scm bash
# ... commands ...
As a workaround, it may be sufficient to "source" the search paths of evaluating guix shell
, e.g.:
#!/usr/bin/env bash
eval "$(guix shell --manifest=manifest.scm --search-paths)"
# ... commands ...
kballou
(332 rep)
Jan 24, 2023, 10:07 PM
• Last activity: May 18, 2023, 09:54 PM
21
votes
2
answers
10291
views
POSIX shell scripts shebang #!/bin/sh vs #!/usr/bin/env sh, any difference?
I recently noticed that many scripts are using `/usr/bin/env` in their [shebang][1]. I have seen that mainly using [Bash][2] and [Python][3], but thus far never in conjunction with [POSIX][4] `sh` (`ash`, `dash`,...). I wonder why, and if my, meant-to-be highly portable, [POSIX][4] shell scripts mig...
I recently noticed that many scripts are using
/usr/bin/env
in their shebang . I have seen that mainly using Bash and Python , but thus far never in conjunction with POSIX sh
(ash
, dash
,...).
I wonder why, and if my, meant-to-be highly portable, POSIX shell scripts might benefit from the env
approach?
***
Is there a general concensus on whether to use:
- standard:
#!/bin/sh
- environment:
#!/usr/bin/env sh
***
Let me stress this enough:
**I never have seen this with sh
.**
Vlastimil Burián
(30505 rep)
Feb 27, 2020, 02:08 AM
• Last activity: May 3, 2023, 11:09 PM
29
votes
5
answers
17041
views
Why is "shebang" called "shebang"?
Does "shebang" mean "bang she"? Why not "hebang" as "bang he"?
Does "shebang" mean "bang she"?
Why not "hebang" as "bang he"?
Tim
(106420 rep)
Aug 7, 2014, 04:39 PM
• Last activity: Mar 27, 2023, 11:36 AM
1
votes
0
answers
151
views
How does the system decide which shell runs a script without a shebang line?
I have this "script" on my Arch Linux system: ``` $ cat ~/scripts/foo.sh ps -hp $$ ``` It will simply run `ps` on its own PID. But the script has no shebang line. I was expecting that in the absence of a specific shebang, the system would run this either using whatever interactive shell I used to la...
I have this "script" on my Arch Linux system:
$ cat ~/scripts/foo.sh
ps -hp $$
It will simply run ps
on its own PID. But the script has no shebang line. I was expecting that in the absence of a specific shebang, the system would run this either using whatever interactive shell I used to launch it, or using whatever I have set as $SHELL
. However, I see that it does change depending on which shell I am running but it isn't consistent:
1. If I launch it from a bash session, it is run by bash:
$ ps -hp $$
1267979 pts/15 Ss 0:00 /bin/bash
$ foo.sh
1276346 pts/15 00:00:00 bash
2. If I launch it from a zsh session, it is run by sh
(which is bash
on my system):
% ps -hp $$
1279855 pts/15 S 0:00 zsh
% foo.sh
1280006 pts/15 S+ 0:00 sh /home/terdon/scripts/foo.sh
3. If I launch it from an sh
session, it is run by sh
again:
$ ps -hp $$
1281052 pts/15 S 0:00 sh
$ foo.sh
1281296 pts/15 S+ 0:00 sh
4. If I launch it from an tcsh
session, it is run by sh
again:
> ps -hp $$
1281968 pts/15 S 0:00 -csh
> foo.sh
1282148 pts/15 S+ 0:00 /bin/sh /home/terdon/scripts/foo.sh
5. If I launch it from a dash
session, it is run by sh
yet again
$ ps -hp $$
1282803 pts/15 S 0:00 dash
$ foo.sh
1283404 pts/15 S+ 0:00 /bin/sh /home/terdon/scripts/foo.sh
So it looks like bash is the only shell that will run a script using itself when the script is launched from a bash session and all the others I tested will use sh
instead. This isn't related to whether /bin/sh
is bash
or another shell. I also tested on an Ubuntu system whose /bin/sh
is dash
and got the same behavior: bash would run the script using itself, dash
and zsh
would run it using sh
.
On both my Arch and the Ubuntu system mentioned above, my SHELL
variable is set to /bin/bash
and my user's entry in /etc/passwd
has /bin/bash
as my default shell.
Questions:
* Who controls this? Is it the kernel or the shell? Is this a bash feature (i.e. bash will use itself in the absence of a shebang)? Something else?
* Why do the shells behave differently? Why do I have so many different formats in the output of ps
? I see sh /home/terdon/scripts/foo.sh
and sh
alone, and /bin/sh /home/terdon/scripts/foo.sh
. What's going on here? I realize these are all equivalent, but it's surprising to see so many different ways of launching the same essential job.
terdon
(251585 rep)
Mar 17, 2023, 12:47 PM
• Last activity: Mar 17, 2023, 01:24 PM
Showing page 1 of 20 total questions