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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

57 minutes ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

2 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

6 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

7 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

8 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

9 hours ago