Cybersecurity Updates & Tools

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.