If I have turned off execute permissions on hello.sh and try to execute it

~$ sudo chmod 0666 hello.sh
~$ ./hello.sh
-bash: ./hello.sh: Permission denied

I get the permission error. But I can easily override that by using the . or source builtin.

$ . hello.sh
hello everyone

What's the purpose of setting the execute permission on a file if someone can override it using the source builtin?


echo 'hello everyone'

There is a difference: if you execute the script like ./script.sh, you are creating a new process, a new instance of the bash shell. This is denied.

However, since the permissions allow you to read the script, what's there to stop you from copy and paste all it contents? The source build-in does only that: it executes the commands one by one by reading them from a text file.


When you run ./hello.sh, you tell the kernel to execute the program hello.sh. If you have execution permission, then the kernel reads the first few bytes of the file, sees the #! line so it knows that this is a script, and runs the interpreter (/bin/bash), passing it the script name as its first argument. Then bash treats the file as a series of instructions. If you don't have execute permission, the kernel aborts the execution at the first step.

When you run . hello.sh or source hello.sh, you aren't asking the kernel to execute hello.sh. You're asking bash to read the file hello.sh and interpret it as a series of instructions.

It's the same thing if you run bash hello.sh, by the way: you aren't executing hello.sh, you're having it interpreted by bash (which is being executed). See Executing a script in zsh - file permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.