echo - Command in Linux – Practical Guide with Redirection, Checksums & Pipes

The echo command prints text to the terminal or sends it through pipes and redirection to other commands or files. It is a basic yet powerful tool for scripting, debugging, and file generation.


Syntax

echo [OPTIONS] [STRING]
  • STRING: The text or variable you want to print.
  • -n: Do not print a newline at the end.
  • -e: Enable interpretation of special characters like \n, \t.

Practical Examples

echo "Hello, world!"
echo -n "Processing..."
echo -e "Line1\nLine2\tTabbed"

Redirection with echo

Redirection lets you control where the output of echo goes.

> – Write to file (overwrite)

echo "My note" > note.txt

Creates or overwrites note.txt.


>> – Append to file

echo "Another line" >> note.txt

Adds to the end of the file.


< – Input file for other commands (not used directly with echo)

sort < note.txt

Sorts content of file using stdin.


<< – Here Document (inline multi-line input)

cat << EOF
Line 1
Line 2
EOF

Used to simulate file input directly in terminal or scripts.


<<< – Here String (one-line input to stdin)

sha256sum <<< "my string"

Feeds a single string directly as input.


Piping echo to Other Commands

Send to grep to filter text

echo "Linux is fast" | grep "fast"

Send to tr to change case

echo "hello" | tr a-z A-Z

Count characters with wc

echo "12345" | wc -c

Hash text with sha256sum

echo -n "secret" | sha256sum

-n avoids adding a newline, which would change the hash.


Encode string with base64

echo -n "mypassword" | base64

Save piped text to file with tee

echo "Log line" | tee -a logfile.txt

Practical Checksum Use Cases

Create a checksum file

echo -n "hello world" | sha256sum > hash.txt

Append multiple checksums

echo -n "data1" | sha256sum >> checks.txt
echo -n "data2" | sha256sum >> checks.txt

Verify checksum

Make sure hash.txt contains:

a591a6d40bf420404a011733cfb7b190d62c65bf0abc…  -

Then run:

sha256sum -c hash.txt

Combine echo with Here Documents & tee

cat << EOF | tee myfile.txt | sha256sum > file.hash
Hello!
This is a multi-line message.
EOF

Writes a message to file and generates its hash in one go.


🧹 Advanced Redirection: stdout & stderr

Redirect stdout only

echo "OK" > output.txt

Redirect stderr only

somecommand 2> error.txt

Redirect both stdout and stderr

somecommand &> all_output.txt

Discard all output (quiet mode)

somecommand > /dev/null 2>&1

Scripting Use Case: Logging with Timestamps

echo "$(date): Script started" >> script.log

Summary Table

Use Case Command Example
Print text to screen echo "Hello"
Overwrite file echo "data" > file.txt
Append to file echo "more" >> file.txt
Pipe to another command echo "hi" \| tr a-z A-Z
Hash a string echo -n "secret" \| sha256sum
Encode with base64 echo -n "text" \| base64
Multi-line input with << cat << EOF ... EOF
One-line stdin with <<< sha256sum <<< "mydata"
Save output + generate hash echo ... \| tee file \| sha256sum > hash.txt
Redirect both stdout + stderr command &> out.txt
Suppress all output command > /dev/null 2>&1

Learn More:

man echo
man bash
man sha256sum
man base64
man tee