1

I have some points I want to understand: I have a script written in bash syntax and does not contain the shebang line (#!/bin/bash) and my default shell is tcsh.

  • when giving execute permission to the file and run it in the default shell (tcsh) by typing the full path of the file, it runs correctly. How the script is run correctly in tcsh and without typing the shebang line?
  • for the same file in the default shell tcsh if I use the source command to run it, it gives syntax error (even if I used the shebang bash line) why?

after some trials I found the following:Thanks, this seems correct I did some trials and reached the following: - Using source (dot operator is alias only in bash) command to run a file: - Executes it even if it has no execute permission - Runs it using the current shell irrespective of the shebang line - If the shebang line exists and the file is run by typing its name (total path) ( here need execute permission), the file is run using the shell in the shebang line. If the shebang line does not exist and run by the name, the bash is always used in my case irrespective of the current shell. I do not why, is it because the bash comes first in the /bin file??????? can you check that with me

1 Answers1

2

If an executable script does not have a shebang line, it is executed by /bin/sh. If you use source to execute it, it is run within the current shell:

$ cat s.sh
echo "shell=$shell"
echo "BASH=$BASH"
$ ./s.sh
shell=
BASH=/usr/local/bin/bash
$ tcsh
[~]% ./s.sh
shell=
BASH=/bin/sh
[~]% source s.sh
shell=/bin/tcsh
BASH: Undefined variable.
DopeGhoti
  • 76,081