How to create and run shell scripts in Ubuntu 22.04
A script is used in Linux and commands are written into it according to job and task characteristics. When executing such a script, each command in it will be executed one by one in order. Shell is a user-written command interpreter. Script shell helps users write and execute multiple commands at the same time.
In this article, readers will learn how to execute shell scripts through command line input. This article is using Ubuntu 22.04 and Ubuntu 20.04. Before demonstrating how to execute a shell script via CLI, let's first look at how to create a shell script.
Prerequisites
- Ubuntu system, any recent version will work.
- The user has sudo privileges
Create a file with the extension ".sh"
To create a '.sh' file, follow these steps:
1. Open the default editor via the menu bar
You can use the default text editor in Ubuntu or if there is any other editor installed in your system such as vim editor. This particular tutorial is using the default text editor.
Type 'editor' and click 'Text Editor'.
The default editor will be opened.
2. Write the command in an untitled document and save it with ".sh" extension
You can write any command according to job requirements or assigned tasks. This particular article is writing the following echo commands/code in an untitled document.
echo "Hello World" echo "Ubuntu 20.04 LTS tutorial" echo "Today's task"
Now, save the file with the ".sh" extension to the folder of your choice. This example saves it to the default directory - the home directory - and the file is named "tutorial.sh".
Now the "tutorial.sh" script will look like this in the text editor after being saved.
3. Execute shell script 'guide.sh' via CLI
You must execute the shell script via command line input.
You must first 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 tutorial, the script name is 'tutorial.sh'.
Run the script with the following command:
$ ./scriptname
The script name is 'tutorial.sh' as mentioned above.
When you run the above command, it will execute the script line by line and display the following output:
You should read it
- How to change user accounts in Ubuntu Bash Shell Windows 10
- How to fix the error is not logged in Ubuntu
- How to Change Themes on Ubuntu with Gnome Tweak Tool
- What's New in Ubuntu 21.10?
- How to change the default shell in Windows Terminal
- Everything you can do with the new Windows 10 Bash Shell
- What is Google Cloud Shell? What's remarkable about Cloud Shell?
- What is a shell?
May be interested
- What is Google Cloud Shell? What's remarkable about Cloud Shell?google cloud shell is an online development and operations environment that you can access anywhere via a web browser. cloud shell is a free service provided by google.
- Why is Ubuntu LTS preferred over regular distros?ubuntu is one of the most widely used linux distributions among software developers and other content creators. ubuntu is also used for many servers around the globe.
- How to automate Photoshop with Photoshop Scriptstoday, this article will show you how to use photoshop scripts. it gives you the ability to change the behavior of a running script - something that photoshop actions can't do!
- How to use Open-Shell in Windows 11windows 11 has great out-of-the-box features and what makes it even better is that you can customize this operating system to your liking. however, there are limits to how much you can adjust.
- How to Customize Ubuntu 23.04 to Look Like macOSubuntu 23.04 lunar lobster, released in april 2023, brings many interesting features. despite stiff competition from operating systems like macos and windows, ubuntu still manages to create a niche for itself in the market.
- What's new in Ubuntu 18.04 LTS just released?ubuntu 18.04 lts code-named bionic beaver has just been officially launched on april 26th. this version is canonical's up to 5 year support period.
- Steps to install OpenStack on Ubuntu 18.04 using DevStackdevstack is a set of extensible scripts that facilitate openstack deployments. in this tutorial you will learn how to deploy openstack on ubuntu 18.04 using devstack.
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 10in part 10, i will show you how to use powershell scripts in conjunction with smo and parameters to create sql server scripts. creating sql server scripts is an important task for administrators and sql server database development professionals.
- 12 scripts for AutoHotKey make life easierautohotkey is one of the best windows automation programs that can perform from the simplest actions to the most difficult tasks.
- How to Create DEB Packages for Debian and Ubuntuthe deb file in debian-based operating systems like ubuntu and kali linux is equivalent to the exe file found in windows.