Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On my Linux box, echo $SHELL results in /bin/csh. So I assume my default shell is c-shell. I am trying to understand the behavior of a shell script.
scenario 1 - script contains -

echo $1 $2 $3
echo $*
echo $argv[1] $argv[2]
$argv[3]
echo $argv[*]
echo $#argv

output -

arg1 arg2 arg3
arg1 arg2 arg3 arg4
[1] [2]
./test.sh: line 4: [3]: command not found
[*]
4argv
  • So clearly the c-shell is not able to execute the last 4 lines in the script which should be executed by csh.

However - when I add the shebang line #!/bin/csh at the top of the script, it prints all the output correctly.

Question - Why is the csh not executing the last 4 lines correctly in first scenario and why do I explicitly have to include the shebang line?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

When the file starts with #!/bin/csh, you explicitly tell the kernel to run the script with that program. But if there's no #! at the start of your file, execution fails – the exec() syscall fails – and your shell attempts to run the script using an even older method, by passing the file directly to /bin/sh.

The Bourne shell, sh, implemented this behavior way before #! appeared, and so scripts written this way expect to be run under sh or at least a compatible shell. csh is far from compatible, so it runs such scripts under sh. Your $SHELL is irrelevant.

tcsh:

Because many systems use either the standard version 6 or ver- sion 7 shells whose shell scripts are not compatible with this shell, the shell uses such a 'standard' shell to execute a script whose first character is not a '#', i.e., that does not start with a comment.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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