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

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

9 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

9 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

9 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

9 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

1 day ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

1 day ago