If you are unaware of how the script file is written and run on a system, follow this article’s guidelines, where we will show you can write a shell script file and run it on your Raspberry Pi system.
How to Write and Run a Shell Script on Raspberry Pi
The script file is an executable file with an extension .sh and the code inside the script file is written in Shell language, which is considered time-saving.
You don’t need to write an extended code to execute the file; only a few lines will do the task for you. Follow the methods below to know about writing and running a shell script on Raspberry Pi.
Writing a Shell Script on Raspberry Pi
Creating a shell script file on Raspberry Pi is extremely simple and to add further advantage, we are providing you with a simple way to write a shell script file on Raspberry Pi. However, before that, you should need to create a file with the name of your choice and save it in a directory. For generalization, we are creating a simple “welcome.sh” file using the following command:
After opening the file, you will need to paste the following code inside the file:
echo "Welcome to LinuxHint!"
In the above code, the first line tells the bash shell to execute commands used inside the script file. Whenever you create a script file, you should need to add this line at the start of a code. Once the above code is pasted in the file, you can save it using “CTRL+X”, add “Y” and enter.
Running Shell Script File on Raspberry Pi
In order to run a shell script file on your Raspberry Pi system, you should first need to make the file executable using the following command:
The above command will add the execution privilege to the current Raspberry Pi user.
Now, there are two methods to run a script file on the Raspberry Pi system. The first one uses the sh command to run the file on the Raspberry Pi terminal.
The second way is to run the sh file through the following command:
In both cases, you will get the same output on your Raspberry Pi terminal and it’s entirely up to your choice which method you like for running the script file on your Raspberry Pi system.
Conclusion
Shell Script is an executable file that contains a series of shell commands executed through a single command line. If you want to write your script file on your Raspberry Pi system, you need to create a file and add shell language code inside the file. Afterward, you will need to make the file executable through the chmod command and execute the script through the sh or using dot slash with the shell script file name.