1

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
  • 3
    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 at 16:11
15

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.

5
  • 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 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. – Marc Wilson Jun 15 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 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 at 7:40
  • Nice contribution! – Darwin OS X Jun 15 at 17:09

You must log in to answer this question.

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