How to create and run shell script in Ubuntu

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.

text editor

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"

shell script in text editor

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.”

Also Read:  How to Restore or Set Ubuntu's Default Boot Screen Animation

save sh file

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

execute shell script

Run the script using the following command:

./shellscript.sh

run shell script

Executing the above command will run each line of the script sequentially and display the corresponding output.

If you liked this article, please subscribe to our YouTube Channel. You can also stay connected with us on X (Twitter) and Facebook.



Leave a Reply