实际上所有bash 脚本的运行都是
由交互式shell创建一个新的非交互式shell的子进程然后运行的。
Let's start with a simple Bash script and work up to more complex ones later. Just about the simplest program you can write in any programming language is a Hello World program, which is a program that simply outputs the text "Hello world". Example 1 shows a Bash script to do that.
Example 1 shows a very simple two-line Bash script. Create this script by typing those two lines into a file using your favorite text editor. The name of the file can be anything you choose, but you should not name the file test, because there is a built-in test command in almost every shell, and it's too easy to accidentally run the built-in test command instead of your own script. I suggest that you name this script hello.
Be careful to type the script exactly as you see it in Example 1. If you misspell a word or mistype the first line, the script may not work. One common mistake is to put a space somewhere on the first line. There should be no spaces anywhere on the first line. Another common mistake is to put one or more blank lines above the line containing "#!/bin/bash". That line must be the very first line of the script.
2.4. Running the Hello World Script
Once you've created your Hello World script, you can execute it in a variety of ways. Let's assume that you named the script hello, and it exists in your current working directory. Example 2 shows how to execute the script. In this example, the interactive shell's prompt is shown as "bash$". Your shell's prompt might look different, but that's OK.
When you type "bash hello" as shown in Example 2, if you get an error message that says "command not found" (or something similar) then either you don't have Bash installed on your system or it is installed in a directory that is not listed in the value of your PATH
environment variable. If you get an error message that says "No such file or directory", then your current working directory is probably not the one containing the script you just created.
The command "bash hello" starts a non-interactive Bash shell and passes it one argument: the name of the file containing the script to execute. While the script is running, there are actually two shells running! One is the interactive Bash shell which displays the "bash$" prompt and executes the command "bash hello". The other is the non-interactive Bash shell that you manually started to execute the script. The interactive shell isn't doing anything while the script is running — it's merely waiting for the non-interactive shell to terminate. In Example 2, the interactive shell is Bash, but you don't have to use Bash as your interactive shell to run a Bash script. The command shown in Example 2 will work no matter which interactive shell you use.
It's a bit of a hassle to run Bash scripts this way. It would be simpler if you could just type the name of your script without the leading "bash" to make the operating system automatically start a new Bash shell to execute your script. That way, the script can be run just like any other program. To allow your script to be run without having to type the leading "bash", you must do two things:
-
Change the permissions on the script to allow you to execute it. This can be done with the command "chmod u+x hello". You don't have to grant execute permission just to yourself. The command "chmod ugo+x hello" allows everyone to execute your script. See the man page for the chmod command for more information.
-
Make sure that the directory containing the script is one of the directories listed in the value of your
PATH
environment variable.
A good way to satisfy the requirement in item #2 above is to do this:
-
Create a directory named "bin" under your home directory. You can do this with the command "mkdir $HOME/bin".
-
Move the script to that directory. You can do this with the command "mv hello $HOME/bin".
-
List the full pathname of that directory in the value of your
PATH
environment variable (We cover variables in more detail in Section 4). You can do this by putting the following line into your interactive Bash startup script (which is the file .bashrc in your home directory):PATH="$HOME/bin:$PATH"
-
Terminate your shell, and start a new one. You have to do this because the changes to your .bashrc file only take effect in shells started after you save the changes.
Lastly, if you don't want to permanently alter your PATH
environment variable (as shown above), you can run a Bash script in your current working directory by typing "./scriptname". If you want to run your scripts in this fashion, you still have to make the script executable using the chmod command shown above. Example 3 shows how you can run your hello script if you have made the script executable.