A Linux script is a set of written commands and the shell, serving as the user-written command interpreter, plays a key role in executing these scripts. A Shell script simplifies the process of writing and executing multiple commands simultaneously, providing users with a convenient way to automate tasks.
Create shell script with .sh extension
To create a shell script with a “.sh” extension, follow these steps:
Open the default “Text Editor” through the Show Apps menu bar. You can use the default text editor in Ubuntu or any other installed editor like gedit.
Alternatively, use the terminal (Ctrl+Alt+T) to open the default text editor:
gnome-text-editor
For gedit use the below command to open it
gedit
Write commands based on your work requirements or assigned tasks. In this article, the example includes echo commands. A basic shell script starts with #!/bin/bash on the first line, indicating the use of the Bash shell for interpretation. The subsequent lines contain the desired commands.
#!/bin/bash echo "CONNECTwww.com" echo "How to create and run a shell script in Ubuntu" echo "Manikandan"
Save the file with the “.sh” extension in the directory of your choice. In this example, it is saved in the default home folder with the filename “shellscript.sh.”
Run the shell script
To run the shell script “shellscript.sh” through the command line interface (CLI), make the script executable with the following command:
sudo chmod +x ~ shellscript.sh
Run the script using the following command:
./shellscript.sh
Executing the above command will run each line of the script sequentially and display the corresponding output.