Mastering Linux Basics: Understanding the Differences and Applications of echo, cat, and tee Commands

This document explains the usage of echo, cat, and tee.1.Introduction to Basic Syntax

  1. Clarify the difference between>and>>.
    1. > is overwrite redirection
    2. >> is append redirection
  2. Clarify the usage of EOF as an end-of-file marker
    1. Here Document is a form of redirection that allows multiple lines of text to be used as input for a command.
    2. The syntax is as follows
command << [OPTIONS] EOF
Multi-line text content
EOF

Basic usage (variables will be replaced)
cat > file.txt << EOF
Hello, $USER!
Current directory: $PWD
EOF
Result: Variables (like $USER, $PWD) will be replaced in file.txt

Disable variable replacement (enclose EOF in single quotes)
cat > file.txt << 'EOF'
Hello, $USER!
Current directory: $PWD
EOF
Result: file.txt retains the literal $USER, $PWD.
  1. Common usage of tee, echo, cat, etc.
    1. Common usage of tee
tee -a /home/shl/portainer_ui/compose.yaml << 'EOF'
version: "3"
services:
  portainer:
    image: portainer/portainer:latest
    container_name: portainer
    ports:
    - "9000:9000"
    volumes:
    - /home/shl/portainer_ui/data:/data
    - /var/run/docker.sock:/var/run/docker.sock
EOF
  1. Common usage of cat
cat >> file.txt << EOF
This is appended content
EOF
  1. Common usage of echo
echo "Hello" >> test.txt    # Create test.txt and write "Hello"
echo "World" >> test.txt    # Append "World" to test.txt, content becomes "Hello\nWorld"

2.Differences between echo, cat, and teeecho,cat,tee are three basic commands in Linux/Unix systems for handling text, each with distinct core functions and application scenarios:1. echo: Outputs text to standard output (stdout)

  • Core function: Print strings or variable values to the terminal, or save to a file via redirection.
  • Typical usage:
  • bash
echo "Hello World"          # Output to terminal
echo $HOME                  # Output environment variable
echo "Content" > file.txt      # Overwrite file
echo "Content" >> file.txt     # Append to file
  • Characteristics: Simple and direct, suitable for single-line text output or variable value display.

2. cat: Concatenates files and outputs to standard output

  • Core function:
    • View file content:cat file.txt
    • Merge files:cat file1.txt file2.txt > merged.txt
    • Create files:cat > new_file.txt (Press Ctrl+D to end)
  • Typical usage:
cat file.txt               # Display file content
cat -n file.txt            # Display content with line numbers
cat file1.txt file2.txt    # Merge and output to terminal
  • Characteristics: Focused on reading and merging file content, does not generate new content.

3. tee: Outputs to both standard output and a file simultaneously

  • Core function: Copies input to multiple destinations (screen + file), supports append mode.
  • Typical usage:
echo "Content" | tee file.txt     # Overwrite file and display to terminal
echo "Content" | tee -a file.txt  # Append to file and display to terminal
ls -l | tee output.log         # Save command output to file and display
  • Characteristics: Suitable for scenarios where real-time viewing of processing results and log saving is required.

Comparison Summary

Command Main Purpose Input Source Output Target Typical Scenario
echo Generate text None / Variable Screen or file Print information, write configuration files
cat Read / Merge files File Screen or file View files, merge files
tee Split output (display and save simultaneously) Pipe / Command output Screen + file Real-time logging

Common Combination Examples

  1. Create configuration file:
echo "server: 127.0.0.1" > config.ini
  1. Real-time monitor command output and log:
tail -f logs/app.log | tee -a analysis.log

These commands are often used in conjunction with pipes (|) and redirection (>,>>) to build complex text processing workflows.

Leave a Comment