A script is used in Linux and contains commands written according to work specifications and assignments. When executed, each command in the script executes in order. The shell is the user-written command interpreter. A Shell script helps a user write and execute multiple commands at the same time.
This article will show how to execute shell scripts through command line input. Concerning this particular article, I've tested it on Ubuntu 24.04, Ubuntu 22.04 and Ubuntu 20.04. Before demonstrating how to execute a shell script through CLI, we will first see how to create a shell script.
Prerequisites
- Ubuntu system, any recent version will work.
- User with sudo privileges
Create a file with ".sh" extension
To create a “.sh” file, follow the following steps:
1. Open default “Text Editor” through the menu bar
You can either use the default text editor in Ubuntu or if there is any other editor installed in your system for example “vim text editor”. For this particular guide, I am using the default text editor.
Type “editor” and click on the ”Text Editor”.

The default text editor will be open.

2. Write commands in the untitled document and save it with the extension ".sh"
You can write any commands as per your work requirements or assigned tasks. I am writing the following echo commands/code in the untitled document for this particular article.
echo "Hello World" echo "Ubuntu 20.04 LTS tutorial" echo "Today’s task"

Now save the file with the ".sh" extension in the directory of your own choice. For this particular article, I am saving it in the default folder - the home folder - and the file is named "tutorial.sh".

Now, the script "tutorial.sh" will look something like this in the text editor after being saved.

3. Execute the shell script “tutorial.sh” through CLI
You have to execute the shell script through command line input.
First, you have to make the shell script executable by running the following command:
$ chmod +x scriptname
Write your shell script name in place of “scriptname” in the above command. For this particular guide, the script name is “tutorial.sh”.

Run the script using the following command:
$ ./scriptname
The script name is “tutorial.sh” as mentioned above.
![]()
On running the above command, it will execute each line of script one by one and display the following output:

Frequently Asked Questions
What is a shell script?
A shell script in Ubuntu is a text file containing a series of commands that the shell can execute. It's a way to run multiple commands automatically, saving time and effort.
What is the basic structure of a shell script?
A basic shell script starts with #!/bin/bash on the first line, which tells Ubuntu to use the Bash shell to interpret the script. Following lines contain the commands you want to execute.
How do I make my shell script executable?
In the terminal, use the chmod command: chmod +x myscript.sh. This changes the script’s permissions, allowing it to be executed.
How do I run a shell script?
After making it executable, you can run the script by typing ./myscript.sh in the terminal from the directory where the script is located.
Can I pass arguments to my shell script?
Yes, you can pass arguments to your script. Inside the script, arguments are accessed using `$1`, `$2`, etc., where `$1` is the first argument, `$2` the second, and so on.
How do I add comments to my script?
Use the # symbol to start a comment. Everything on the line after # will be ignored by the shell.
What is the difference between sh and bash in script writing?
bash (Bourne Again SHell) is an enhanced version of sh (Bourne Shell). Bash includes additional features like command line editing. If your script relies on Bash-specific features, use #!/bin/bash; otherwise, #!/bin/sh is sufficient.
How can I debug my shell script?
Run your script with the -x option: bash -x myscript.sh` This prints each command and its arguments as they are executed, which is useful for debugging.
Is there a way to run a script at specific times or intervals?
Yes, you can use cron, a time-based job scheduler in Unix-like systems. To add a script to cron, edit the crontab file with crontab -e and specify the time and script path.
Can shell scripts include loops and conditional statements?
Shell scripts can have `for`, `while`, and `until` loops, as well as `if-else` and `case` statements, making them quite powerful for automating complex tasks.
How do I prompt users for input in a shell script?
Use the read command. For example, read -p "Enter your name: " name will prompt the user to enter their name and store it in the variable name.
What are some best practices for writing shell scripts?
Some best practices include: adding comments for clarity, checking for errors after each command, using clear and descriptive variable names, and avoiding the use of hard-coded values where possible.
How can I ensure my shell script is portable?
To make your script portable, avoid using Bash-specific features if not necessary, and instead use standard POSIX-compliant syntax. Also, explicitly define paths to binaries instead of assuming their location.
Can I run my Ubuntu shell script on other Unix-like systems?
In many cases, yes, especially if you follow standard practices and avoid system-specific features. However, always test your script on the target system to ensure compatibility.
Conclusion
This article demonstrates the basic initials of shell script and how to create and execute a shell script in a very simple and easy-to-understand manner. The user can perform each step by looking through the attached screenshots of each input command and what will be the correct output of that command. The article will help both professional and naive users equally.