why do we use shebang in the beginning of a shell script file. does the script will run without it.

I tried running it without shebang in shell script but it didn't run.

marked as duplicate by sudodus, Fabby, heemayl command-line May 18 '18 at 12:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • the #! helps the interpreter recognize the code; be it sh, bash, tsch, csh, ksh, .. perl, python .... (which at times can look alike for short scripts) [and thus know which interpreter is required to run it] – guiverc May 18 '18 at 10:48

When a file script starts with the shebang directive

#!langpath args

and has execution permissions set, Unix will "replace it" by

exec langpath args path-to-the-script.

This ways, typically:

  • langpath defines the language to be used, ans should be the path of an executable interpreter of the (programming) language (ex: '/usr/bin/python')
  • the language should accept a script file as an argument
  • the language should ignore the #!... directive as a comment
  • Thank you so much, its really helped me to understand the concept of using shebang. – Ashish Kumar Thakur Jun 22 '18 at 8:03
  • @AshishKumarThakur, I am glad it help. – JJoao Jun 22 '18 at 23:06

Different shells support different features. To give effective advice, ShellCheck needs to know which shell your script is going to run on. You will get a different numbers of warnings about different things depending on your target shell.

ShellCheck normally determines your target shell from the shebang (having e.g. #!/bin/sh as the first line). The shell can also be specified from the CLI with -s, e.g. shellcheck -s sh file.

If you don't specify shebang nor -s, ShellCheck gives this message and proceeds with some default (bash).

Note that this error can not be ignored with a directive. It is not a suggestion to improve your script, but a warning that ShellCheck lacks information it needs to be helpful.

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