1

I am learning the basics of bash and linux. To execute a script, I could type...

bash script1

or

source script1

or

./script1

The first two will run without chmod u+x and the last one requires it.

From my understanding, bash tries to run things in a subshell so it doesn't mess things up. When I add bash before the filename, it's executed in a subshell. source is just a way of telling the computer to run it in the current shell. I'm not sure why these don't require the execute permission though.

./ is pretty straightforward. However, I've seen people run scripts without the ./. One told me I could do this by doing something with PATH. I completely don't understand this PATH thing.

Can someone explain in the easiest way possible?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Andy
  • 167
  • 9
  • See: [What is the difference between executing a Bash script vs sourcing it?](https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-vs-sourcing-it) – John Kugelman Sep 26 '19 at 19:22
  • See [Why do you need ./ (dot-slash) ... in bash](https://stackoverflow.com/questions/6331075/why-do-you-need-dot-slash-before-executable-or-script-name-to-run-it-in-bas) to see what the deal is with `./` and how it relates to `PATH` – that other guy Sep 26 '19 at 20:12

2 Answers2

1

On

bash script1

you are executing bash (the one that needs execution permission) to read and process script1 (which needs read permission).

On

source script1

you are telling the current bash to read the file and process it as if it were typed on the current shell, so the current bash reads the script (read permission) and executes every line.

Finally, on

./script1

you are telling bash to try to run a file called ./script1, so it checks if it is executable (execute permission on ./script1) and passes this file to the kernel to be executed. The kernel opens the file and acts as needed (if it have a shebang line, it uses whatever is given, if it finds it is an ELF object, it prepares the binary in memory...).

Regarding PATH, check some documentation and come back with specific doubts, if any.

Poshi
  • 5,332
  • 3
  • 15
  • 32
  • I guess this brings another question. Can anyone (not the user) without the execute permission still run my files by simply putting bash or source? If so, it kind of defeats the purpose of permission. – Andy Sep 26 '19 at 20:53
  • Yes, because with `bash` or `source` you are not executing the script. You are reading it through `bash`, which is the one that is being executed. If you want to avoid a script being "executed", remove the read permission (to avoid `bash` reading and interpreting it) and the execute permission (to avoid the kernel to execute it). – Poshi Sep 26 '19 at 21:33
1

PATH is environmental variable. It is a list containing all directories that will be searched when you issue a command. So if your PATH is defined as PATH=/bin:/usr/bin these two dirs will be searched, if you redefine it as export PATH=./:$PATH it will also add current directory to search list.

jaroslawj
  • 462
  • 2
  • 12