How To

Echo Command In Linux With Bash Examples

Introduction

The echo command is one of the most commonly used commands in Linux and Bash scripting. It is used to print text, variables, command output, and messages in the terminal. If you are learning Bash scripting, the echo command is one of the first commands you should understand.

In Bash scripts, echo is often used to display information, show progress messages, print variable values, create simple output, and write text into files. It is useful for beginners, Linux users, system administrators, and cybersecurity learners who want to create simple and readable scripts.

What Is The Echo Command In Linux?

The echo command displays text or values on the terminal screen.

Basic syntax:

echo "Your message here"

Example:

echo "Welcome to Bash scripting"

Output:

Welcome to Bash scripting

This command simply prints the given message.

Basic Echo Command Example

Create a Bash script:

nano echo-example.sh

Add the following code:

#!/bin/bashecho "Hello Linux user"echo "This is a Bash script"echo "Learning Bash scripting is useful"

Save and run the script:

chmod +x echo-example.sh./echo-example.sh

Output:

Hello Linux userThis is a Bash scriptLearning Bash scripting is useful

Echo With Variables

The echo command is commonly used to print variable values.

#!/bin/bashname="Kali Linux"topic="Bash Scripting"echo "Name: $name"echo "Topic: $topic"

Output:

Name: Kali LinuxTopic: Bash Scripting

This is useful when displaying usernames, file paths, system details, or script results.

Echo With Command Output

You can use command substitution with echo.

#!/bin/bashecho "Current user: $(whoami)"echo "Current directory: $(pwd)"echo "Today date: $(date)"echo "Kernel version: $(uname -r)"

This script prints useful system information using Linux commands.

Echo With New Line And Escape Characters

Use the -e option to enable escape characters.

echo -e "Line 1\nLine 2\nLine 3"

Output:

Line 1Line 2Line 3

Use \t to add tab spacing:

echo -e "Name:\tKali Linux"

Write Text To A File Using Echo

You can save text into a file using redirection.

echo "This is a log message" > log.txt

To append text without deleting old content:

echo "Another log message" >> log.txt

This is useful for logs, reports, and automation scripts.

Cybersecurity Example: Create A Simple Security Report

#!/bin/bashecho "Security Report" > security-report.txtecho "User: $(whoami)" >> security-report.txtecho "Hostname: $(hostname)" >> security-report.txtecho "Date: $(date)" >> security-report.txtecho "Open network connections:" >> security-report.txtss -tuln >> security-report.txt

This script creates a simple report with system and network information.

Conclusion

The echo command in Linux is simple but very useful in Bash scripting. It helps print messages, show variable values, display command output, and write text to files.

For beginners, learning echo is an important step because almost every Bash script uses it in some way. It is useful for Linux automation, debugging, reporting, logging, and cybersecurity scripting.

Cyber Defence

Recent Posts

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

7 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

DockerĀ is an open-source platform that lets you package and run applications inside containers. Each container…

17 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

18 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

18 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

19 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

20 hours ago