Tutorials  /  Linux Basics

How To Execute a Command with a Shell Script in Linux

Ccentron Redaktion · January 2025 ·4 min read ·Linux Basics, Tutorial

Introduction to Execute a Command

Shell is a command-line interpreter that allows the user to interact with the system. It is responsible for taking inputs from the user and displaying the output.

Shell scripts are a series of commands written in order of execution. These scripts can contain functions, loops, commands, and variables. Scripts are useful for simplifying a complex series of commands and repetitive tasks.

In this article, you will learn how to create and execute shell scripts for the command line in Linux.

VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Prerequisites to Execute a Command

To complete this tutorial, you will need:

  • Familiarity with using the terminal.
  • Familiarity with a text editor.
  • Familiarity with commands like chmod, mkdir, and cd.

Getting Started

A shell script needs to be saved with the extension .sh.

The file needs to begin with the shebang line (#!) to let the Linux system know which interpreter to use for the shell script.

For environments that support bash, use:

Code
#!/bin/bash

For environments that support shell, use:

Code
#!/bin/sh

This tutorial assumes that your environment supports bash.

Shell scripts can also have comments to increase readability. A good script always contains comments that help a reader understand exactly what the script is doing and the reasoning behind a design choice.

Creating and Running a Basic Shell Script

You can create a shell script using the vi editor, a cat command, or a text editor.

For this tutorial, you will learn about creating a shell script with vi:

Code
vi basic_script.sh

This starts the vi editor and creates a basic_script.sh file.

Then, press i on the keyboard to start INSERT MODE. Add the following lines:

Code
#!/bin/bash
whoami
date

To save and exit the vi editor:

  1. Press ESC
  2. Type : (colon character)
  3. Type wq
  4. Press ENTER

Finally, you can run the script with the following command to Execute a Command:

Code
bash basic_script.sh

You may get output that resembles the following:

Code
root
Fri Jun 19 16:59:48 UTC 2020

Using Variables in Shell Scripts

Scripts can include user-defined variables. Add the following lines to the script:

Code
#!/bin/bash
# This is a comment

# defining a variable
GREETINGS="Hello! How are you"
echo $GREETINGS

Run the script:

Code
bash basic_script.sh

This prints out the value assigned to the variable:

Code
Hello! How are you

Reading Input from the Command Line

Shell scripts can accept input from the command line using the read command. Add the following lines:

Code
#!/bin/bash
echo "What is your name?"
read NAME
GREETINGS="Hello! How are you"
echo $NAME $GREETINGS

Output:

Code
What is your name?
Sammy
Sammy Hello! How are you

Defining Functions to Execute a Command

Users can define their own functions in a script. Add the following lines:

Code
#!/bin/bash
echo "What is the name of the directory you want to create?"
read NAME

echo "Creating $NAME ..."
mkcd ()
{
  mkdir "$NAME" 
  cd "$NAME"
}

mkcd
echo "You are now in $NAME"

Output:

Code
What is the name of the directory you want to create?
test_dir
Creating test_dir ...
You are now in test_dir

Conclusion

In this article, you learned how to create and Execute a Command for the line in Linux.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren