Skip to content

What is echo Linux command?

The Linux echo command is a versatile tool used for printing text or variables to the terminal. It is commonly used in shell scripts and command-line operations to display messages or manipulate output. The command can also be used to redirect text to files or as part of a pipeline to pass information between commands.

echo Syntax:

Terminal window
echo [option] [string]

echo Options:

OptionDescription
-nDo not output the trailing newline
-eEnable interpretation of backslash escapes
-EDisable interpretation of backslash escapes

Parameters:

ParametersDescription
stringThe text or string to be displayed on the standard output

echo Command Usage Examples:

Terminal window
echo "Hello, World!"

Prints the message “Hello, World!” to the console.

Redirect and Append Output to a File

Terminal window
echo "This is a new line" >> file.txt

Appends the text “This is a new line” to the end of the file “file.txt”.

Use Escape Characters

Terminal window
echo "I love Linux\nLinux loves me"

Prints the message “I love Linux” on one line, followed by “Linux loves me” on the next line.

Terminal window
echo "Current directory: $(pwd)"

Prints the current working directory by using command substitution with $(pwd).

Check if a Variable is Set

Terminal window
if [ -z "$VAR" ]; then echo "Variable VAR is not set"; fi

Checks if the variable VAR is empty and outputs a message accordingly.

How do I use echo in Linux?

To use the echo command in bash, execute the following command:

Terminal window
echo "Hello, World!"

What are some options available with the echo command?

The echo command in Linux has various options such as -n to omit the trailing newline and -e to enable interpretation of backslash escapes. Here is an example:

Terminal window
echo -n "Hello, World!"

How can I redirect the output of the echo command to a file?

You can redirect the output of the echo command to a file using the following syntax:

Terminal window
echo "Hello, Echo" > output.txt

How to print a blank line with the echo command?

To print a blank line using the echo command in Linux, you can simply run:

Terminal window
echo

How do I display the contents of a variable using echo?

You can display the contents of a variable with the echo command by referencing the variable using the dollar sign ($) symbol. Here’s an example:

Terminal window
name="Linux"
echo "Hello, $name"

How can I append text to a file using the echo command?

To append text to a file with the echo command in Linux, you can use double greater than symbols (>>) for redirection. For example:

Terminal window
echo "New text line" >> file.txt

How to display text in different colors using echo?

You can display text in different colors using ANSI escape codes with the echo command. Here is an example to print text in red:

Terminal window
echo -e "\033[31mHello, World!\033[0m"

Applications of the echo command

  • Printing text to the terminal
  • Displaying variable values
  • Redirecting output to a file
  • Writing shell scripts
  • Displaying messages in scripting
  • Debugging scripts
  • Environment variable expansions
  • Displaying newline characters in strings
  • Creating and editing configuration files