Saving a Shell Script
Now that you've created a simple shell script, let's learn how to save it for future use.
Saving the Script
To save the shell script, you can use the nano
text editor or any other text editor of your choice. The script should be saved with a .sh
extension, which indicates that it is a shell script.
For example, to save the hello.sh
script, you can use the following command:
$ nano hello.sh
This will open the nano
editor, where you can edit and save the script.
Choosing a Location
When saving a shell script, it's important to choose a location that is accessible and easy to remember. A common practice is to save the script in the user's home directory or a dedicated directory for scripts.
For example, you can create a scripts
directory in your home directory and save the hello.sh
script there:
$ mkdir ~/scripts
$ mv hello.sh ~/scripts/
Now, the hello.sh
script is saved in the ~/scripts
directory, and you can access it from anywhere in the terminal.
Accessing the Saved Script
To access the saved script, you can navigate to the directory where it is saved and run the script using the following command:
$ cd ~/scripts
$ ./hello.sh
Hello, LabEx!
Alternatively, you can add the scripts
directory to your system's PATH
environment variable, which will allow you to run the script from anywhere in the terminal:
$ export PATH="$PATH:~/scripts"
$ hello.sh
Hello, LabEx!
In the next section, we'll learn how to run the saved shell script.