Skip to main content

Command Palette

Search for a command to run...

Cheat Sheet #day52 - echo

Published
2 min read
Cheat Sheet #day52 - echo

echo Command Cheatsheet

The echo command in Unix/Linux is used to display a line of text or a string that is passed as an argument. It is commonly used in shell scripts and command-line operations to output text to the terminal or to a file.

Syntax

echo [OPTION]... [STRING]...

Common Options

  • -n: Do not output the trailing newline.

  • -e: Enable interpretation of backslash escapes.

  • -E: Disable interpretation of backslash escapes (default).

Backslash Escapes (used with -e)

  • \a: Alert (bell)

  • \b: Backspace

  • \c: Suppress trailing newline

  • \e: Escape character

  • \f: Form feed

  • \n: New line

  • \r: Carriage return

  • \t: Horizontal tab

  • \v: Vertical tab

  • \\: Backslash

  • \0nnn: Byte with octal value nnn (1 to 3 digits)

  • \xHH: Byte with hexadecimal value HH (1 to 2 digits)

Examples and Use Cases

Basic Usage

echo "Hello, World!"
  • Outputs: Hello, World!

Suppress Newline

echo -n "Hello, World!"
  • Outputs: Hello, World! without trailing newline.

Enable Backslash Escapes

echo -e "Line 1\nLine 2\nLine 3"
  • Outputs:

      Line 1
      Line 2
      Line 3
    

Include a Tab

echo -e "Column 1\tColumn 2"
  • Outputs: Column 1 Column 2 with a tab space between the columns.

Carriage Return

echo -e "Hello\rWorld"
  • Outputs: World (the \r causes Hello to be overwritten by World).

Alert/Bell

echo -e "\a"
  • Produces an alert sound if the terminal supports it.

Output to a File

echo "This is a test" > file.txt
  • Writes This is a test to file.txt, overwriting the file if it exists.

Append to a File

echo "This is an additional line" >> file.txt
  • Appends This is an additional line to file.txt.

Using Variables

name="Alice"
echo "Hello, $name!"
  • Outputs: Hello, Alice!

Combining Text and Command Output

echo "Current directory is $(pwd)"
  • Outputs: Current directory is /path/to/current/directory

This cheatsheet provides a quick reference to the most common usages and options for the echo command. For more detailed information, you can always refer to the echo man page by typing man echo in your terminal.

Cheat Sheets

Part 1 of 50

These cheat sheets are designed to be quick reference materials, offering condensed information, tips, and best practices in a easy to understand format.

More from this blog

Cloud Tuned

627 posts

Your starting point for anything cloud: AWS, Azure, GCP, Serverless, Architecture, Hybrid Cloud, Systems Design and other Information Technology topics.