0

When trying to run a script file. What i saw was

When running ./a_script this will look for the script in the current directory. Will run it only if execute permission is given.

When running .a_script it will search for the hidden script file in $PATH and run if found.

When running a_script it will search for the script in $PATH and run if found.

When running . a_script this will search for the script and run it even if execute permission is not given to the script. Why so?

Also, .a_script and ./a_script runs the command in the current shell.

. a_script runs it is a different shell?

I may be wrong. Can anybody explain regarding this issue?

The link that i had referred: https://ss64.com/bash/source.html

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • A period `.` is shell builtin command synonym of `source` – oliv Oct 30 '18 at 06:45
  • You have it backwards `. – David C. Rankin Oct 30 '18 at 06:54
  • In case you hear the term around, some folks say things like *"... then dot in your functions"* when they have a script full of helper functions, like a shell library. The old System V startup scripts used to *"dot in common functions"* all over the place. – Mark Setchell Oct 30 '18 at 07:28

1 Answers1

3

When running ./a_script this will look for the script in the current directory. Will run it only if execute permission is given.

When running .a_script it will search for the hidden script file in $PATH and run if found.

When running a_script it will search for the script in $PATH and run if found.

Yes. Note that the "script" need not be an actual shell script in these cases; any executable will do.

Also, . is just part of the name of the command here. In ./a_script it represents the current directory. This works in any path: Try e.g. ls /./home/. (same as ls /home).

In a_script and .a_script, the command name doesn't contain /, so a PATH search is performed. . isn't special here.

When running . a_script this will search for the script and run it even if execute permission is not given to the script. Why so?

Because in that case the command is . and a_script is just an argument to the . command. . will search for the given file and execute the commands in it (in the current shell). a_script cannot be a general executable; it must be a shell script.

Also, .a_script and ./a_script runs the command in the current shell.

No, if they are indeed shell scripts, they will spawn a new shell to run them.

. a_script runs it is a different shell?

No, it's the opposite: the built-in . command specifically runs commands from a file in the current shell (as if you had typed them).

Another name for . is source, which works the same way, but is easier to search for. See also https://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins.

Community
  • 1
  • 1
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 2
    I think it is worth mentioning in your answer that `.` is a synonym for `source`. The later being a bit easier to search for. – Bernhard Oct 30 '18 at 07:16