- How to Execute Commands from Within a Shell Script
- Executing Commands From a Shell Script
- Creating a Shell Script
- Executing Commands Within a Shell Script
- Executing a Shell Script
- Working With Variables
- Working With Interactive Shell Commands
- Defining Functions
- Conclusion
- About the author
- Denis Kariuki
- Run command in linux shell
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
How to Execute Commands from Within a Shell Script
“Working with shell script is something any Linux user should be at home with. However, how you learn to execute commands plays a big role in your understanding and working with shell scripts. This guide explains all details about executing commands within a shell script. We will cover everything from creating a shell script to defining variables to executing the shell script. Let’s get started!”
Executing Commands From a Shell Script
The shell is an interpreter that allows users to work and interact with the Linux system. The shell works as a command-line interpreter, taking inputs and giving output. A shell script can contain loops, functions, variables, and commands. The best thing with shell scripts is how they make automating tasks easy. Let’s get into the details of executing commands in a shell script.
Creating a Shell Script
A shell script has the .sh extension, and you can create a shell script using an editor, provided you save the file using the .sh extension. In this case, we will create a linuxhint.sh script using the nano editor.
Once the file opens, the line below is the first thing you must write for the system to know that you are working with a shell script.
With your shell script constructed, we can proceed to see how to execute commands inside the script.
Executing Commands Within a Shell Script
A shell script supports the built-in Linux commands. It works similarly to how you would execute Linux commands on your terminal. For instance, if you want to execute commands to get the current date and time and display the logged-in user, you could use the date and who commands by typing them as shown.
Executing a Shell Script
Once you’ve defined the commands you want to execute, save the bash script. Before you execute the script, we must first make it executable using the syntax below.
Once it is executable, you can execute the script using the syntax below.
Working With Variables
Shell scripts allow the creation of user-defined variables to hold the output of given commands. To hold the command’s output inside a variable, enclose the command using $(). For instance, if we needed to get the current date using the date command and store it in a variable, we could define our variable as shown below.
Note that there shouldn’t be any spacing between the words in the line.
If you want to create user-defined variables, write the variable name, and the value should be enclosed in double quotes. For instance, check the variable below.
Once the variable is created, you can call it using the $ followed by the variable name. You can then execute the command using the echo command.
The output would execute as expected based on the commands we added.
Working With Interactive Shell Commands
With a shell script, you can work with commands and variables that interact with the user through inputs to determine the output. As shown below, we use the read command to get the user input and echo the output.
Defining Functions
You can also execute commands through functions in the shell script. A function can take any number of arguments, and you can create a function to execute any command. Below is a shell script with a function that creates a folder and five files inside it.
Here’s the output after executing the script.
Conclusion
Shell scripts are not that challenging to work and create. With a few tricks, you can easily use different commands in your shell scripts to achieve much functionality. In this guide, we’ve seen the various ways you can execute commands within a shell script. Hopefully, you now have a foundation for creating shell script commands.
About the author
Denis Kariuki
Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.
Run command in linux shell
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter