2

Every time I need to run a shell script, I need to enter

chmod +x <filename>

For example, if I want to run this sample script named "run",

#!/bin/bash

javac --version

I have to go into my terminal

chmod +x run
./run

Is there a way to allow Z shell (zsh) always to run these scripts without having to enter chmod +x <filename>?

1
  • 4
    Note that zsh does not run the script unless you explicitly tell it to with zsh ./run. If you just do ./run, the shell mentioned in the shebang (here, #!/bin.bash) is the shell that will run the script.
    – terdon
    Jun 15, 2021 at 16:11

1 Answer 1

17

You only need to run chmod once for a script, not each time you run it. If you don‘t want to do this, you can also use bash ./run to execute it.

You can also create a text file with the executable bit set once, and then use it as a template in your editor of choice to create new scripts based on it.

4
  • I think what he means is that he wants all shell scripts to be executable by default, and not have to add the attribute manually.
    – sfxedit
    Jun 14, 2021 at 23:38
  • Well, that's going to depend, ideally, on the tool being used to create them. TextMate, for example, will mark them executable the first time you save the file. Jun 15, 2021 at 1:48
  • 5
    @sfxedit He should not want to want that. I know it's only a small protection against hackers or viruses executing code on your machine but protection is by layers. Do not weaken any of the layers of protection your machine has.
    – slebetman
    Jun 15, 2021 at 7:22
  • @slebetman One way or the other, the x bit needs to get set, otherwise a file is not executable. Now that I think of it there may be a way in zsh with auto-loaded functions but that can't be used to run bash scripts (and has other downsides as well).
    – nohillside
    Jun 15, 2021 at 7:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .