Run command in linux shell

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.

Читайте также:  Read configuration file linux

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

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

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

Читайте также:  Сервер централизованной защиты dr web linux

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью
Adblock
detector