Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash echo uso: Displaying text in the terminal
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash echo uso: Displaying text in the terminal

Overview

The echo command in Bash is used to display a line of text or a variable value in the terminal. It is one of the most commonly used commands for outputting information, making it essential for scripting and command-line operations.

Usage

The basic syntax of the echo command is as follows:

echo [options] [arguments]

Common Options

  • -n: Suppresses the trailing newline, allowing the next output to appear on the same line.
  • -e: Enables interpretation of backslash escapes (e.g., \n for a new line, \t for a tab).
  • -E: Disables interpretation of backslash escapes (this is the default behavior).

Common Examples

Here are some practical examples of using the echo command:

  1. Basic text output:
    echo "Hello, World!"
    
  2. Suppressing the newline:
    echo -n "This is on the same line."
    echo " And this continues."
    
  3. Using backslash escapes:
    echo -e "First line\nSecond line"
    
  4. Displaying the value of a variable:
    name="Alice"
    echo "Hello, $name!"
    
  5. Outputting special characters:
    echo -e "Tab character:\tTabbed text"
    

Tips

  • Use echo -n when you want to print multiple outputs on the same line without line breaks.
  • To include special characters in your output, remember to use the -e option.
  • Be cautious when using variables with echo, as unquoted variables can lead to word splitting or globbing issues. Always quote your variables to avoid unexpected behavior.