How do I execute a bash script in Terminal?

后端 未结 9 2090
無奈伤痛
無奈伤痛 2020-12-12 11:50

I have a bash script like:

#!/bin/bash

echo Hello world!

How do I execute this in Terminal?

9条回答
  •  忘掉有多难
    2020-12-12 12:19

    This is an old thread, but I happened across it and I'm surprised nobody has put up a complete answer yet. So here goes...

    The Executing a Command Line Script Tutorial!

    Q: How do I execute this in Terminal?

    Confusions and Conflicts:

    • You do not need an 'extension' (like .sh or .py or anything else), but it helps to keep track of things. It won't hurt. If the script name contains an extension, however, you must use it.
    • You do not need to be in any certain directory at all for any reason.
    • You do not need to type out the name of the program that runs the file (BASH or Python or whatever) unless you want to. It won't hurt.
    • You do not need sudo to do any of this. This command is reserved for running commands as another user or a 'root' (administrator) user. Great post here.

    (A person who is just learning how to execute scripts should not be using this command unless there is a real need, like installing a new program. A good place to put your scripts is in your ~/bin folder. You can get there by typing cd ~/bin or cd $HOME/bin from the terminal prompt. You will have full permissions in that folder.)

    To "execute this script" from the terminal on a Unix/Linux type system, you have to do three things:

    1. Tell the system the location of the script. (pick one)

      • Type the full path with the script name (e.g. /path/to/script.sh). You can verify the full path by typing pwd or echo $PWD in the terminal.
      • Execute from the same directory and use ./ for the path (e.g. ./script.sh). Easy.
      • Place the script in a directory that is on the system PATH and just type the name (e.g. script.sh). You can verify the system PATH by typing echo $PATH or echo -e ${PATH//:/\\n} if you want a neater list.
    2. Tell the system that the script has permission to execute. (pick one)

      • Set the "execute bit" by typing chmod +x /path/to/script.sh in the terminal.
      • You can also use chmod 755 /path/to/script.sh if you prefer numbers. There is a great discussion with a cool chart here.
    3. Tell the system the type of script. (pick one)

      • Type the name of the program before the script. (e.g. BASH /path/to/script.sh or PHP /path/to/script.php) If the script has an extension, such as .php or .py, it is part of the script name and you must include it.
      • Use a shebang, which I see you have (#!/bin/bash) in your example. If you have that as the first line of your script, the system will use that program to execute the script. No need for typing programs or using extensions.
      • Use a "portable" shebang. You can also have the system choose the version of the program that is first in the PATH by using #!/usr/bin/env followed by the program name (e.g. #!/usr/bin/env bash or #!/usr/bin/env python3). There are pros and cons as thoroughly discussed here.

提交回复
热议问题