shell-script headers (#!/bin/sh vs #!/bin/csh)

前端 未结 3 522
萌比男神i
萌比男神i 2020-12-23 11:00

Why do all script files start with

#!/bin/sh

or with

#!/bin/csh

Is that required? What\'s the purpose o

3条回答
  •  执笔经年
    2020-12-23 11:37

    This defines what shell (command interpreter) you are using for interpreting/running your script. Each shell is slightly different in the way it interacts with the user and executes scripts (programs).

    When you type in a command at the Unix prompt, you are interacting with the shell.

    E.g., #!/bin/csh refers to the C-shell, /bin/tcsh the t-shell, /bin/bash the bash shell, etc.

    You can tell which interactive shell you are using the

     echo $SHELL
    

    command, or alternatively

     env | grep -i shell
    

    You can change your command shell with the chsh command.

    Each has a slightly different command set and way of assigning variables and its own set of programming constructs. For instance the if-else statement with bash looks different that the one in the C-shell.

    This page might be of interest as it "translates" between bash and tcsh commands/syntax.

    Using the directive in the shell script allows you to run programs using a different shell. For instance I use the tcsh shell interactively, but often run bash scripts using /bin/bash in the script file.

    Aside:

    This concept extends to other scripts too. For instance if you program in Python you'd put

     #!/usr/bin/python
    

    at the top of your Python program

提交回复
热议问题