
The echo command in Linux is used to display text or variables to the standard output (STDOUT). It's commonly used in shell scripts, command pipelines, and interactive shell sessions to print messages, display variables, or generate output for further processing.
This article explains how to use the echo command in Linux to display and manipulate text.
echo CommandBelow is the basic echo command syntax:
Within the above command, [options] includes optional flags that modify the command's behavior, and [text...] sets the text or variables to display. Multiple text arguments are separated with spaces.
echo Command Optionsecho CommandDisplay a sample message using the echo command.
The above command outputs the message Hello, World!.
Output:
Create a new variable and display it's value using the echo command.
The above command outputs the message Welcome, John by substituting the value of the variable $name into the output.
Output:
Display a new message with the system date.
The above command outputs the message Today is and the output of the date command which displays the system date and time.
Output:
Display special characters.
The above command outputs three lines with the newline character \n sequence.
Output:
Display text without a trailing newline.
The above command outputs Hello, World! without a newline character after Hello,.
Output:
Display content without spaces between text.
The above command outputs the backspace escape character \b to remove spaces between words.
Output:
Display tabs between text.
The above command output the tab escape character \t to insert tabs between words.
Output:
Display all files and folders in the working directory.
The above command outputs all files and directories in the active working directory similar to the ls command.
Output:
Combine echo with command substitution.
The above command outputs the message The current directory is: and the output of the pwd command which displays the active working directory.
Output:
Redirect the echo command output to a file.
The above command outputs the command output Hello, World! to a new file output.txt.
Output:
Display environment variables.
The above command outputs the value of the PATH environment variable.
Output:
Display a colored output.
The above command outputs the text in red using escape sequences to enabled a colored output.
Output:
Using echo in a script:
Create a new sample script file my_script.sh using a text editor such as Nano
Add the following contents to the file.
Enable execute privileges on the file.
Run the script
The above script uses the echo command to display messages at different intervals.
Output:
You have used the echo command in Linux to efficiently display text, variables, and messages. For more command options, run the man echo command to view the echo manual pages.
0 Comments
Be the first to comment and share your perspective with the community.