When you do:
or
The contents of that file are read as input to the current interactive shell. Since the file is just being read, the execute permission set does not need to be set - just the read permission.
When you do:
If /path/to/file has the execute permission, Linux will try to
execute the file.
Note that ./file is a way to specify "file" in the current working directory ("." is a name for the current directory, like ".." is a name for the parent of the current working directory).
If you don't specify a path, e.g.
...the PATH environment variable is used as a list of directories (: separated) in which to search for "file". If "file" is found in one of these directories and the permissions are set such that the user may execute it, it is executed, else it is ignored and the next directory in the PATH is checked.
For Linux to execute a file, the execute permission must be set. This means the file's permission must be set so the current user can use the execute permission. Note that if the filesystem on which the file resides is mounted with the "noexec" option, it over-rides the file's permissions and you will not be able to execute it.
Assuming the file is executable by the current user, and the filesystem is not mounted with noexec option, Linux will proceed with execution. Linux supports several binary executable formats (depending on how the kernel was compiled). The most common is ELF, although many systems support the a.out format as well.
In addition to these binary formats, Linux will check to see if the file begins with a shabang, "#!". If it does the rest of the line is read and if it is the path to an executable program, that program will be executed and the rerst of the file used as input to that program.
So when you see:
At the start of a file, /bin/bash is executed, and fed the rest of the file as input. What it does with this input is bash's own business.
Similarly, files starting with
Will cause the /usr/bin/perl program to be started and fed the rest of the file as input. What Perl does with the input is Perl's business.
This mechanism is a nice way to add interpreters to the system without requiring users to know what interpreter a given program file is written in - they just execute the file, and Linux figures out what program to interpret the file with. When a script is to be processed in this way it must have the execute and read permission available to the current user.