Echo command in linux – Introduction
The “echo” command in Linux is a commonly used command-line utility that allows users to display text or variables on the terminal or in a file. It is usually used for printing messages, variables, and outputs of other commands to the console or a file.
The syntax of the “echo” command is quite simple:
echo [options] [arguments].
The options and arguments can be separated by spaces or tabs.
Here are some examples of how to use the “echo” command:
- Display a simple message: echo “Hello World”
- Display a variable: var=”some text”; echo $var
- Display text with a newline character: echo -e “Line 1\nLine 2”
- Display text without a newline character: echo -n “This is a sentence”
- Redirect output to a file: echo “This is some text” > myfile.txt
The “echo” command is a versatile utility that can be used in various scripts and commands to display text output on the terminal or to write text to a file.
Basic Usage
The basic usage of the echo command is to display text on the terminal. Here’s an example:
echo “Hello, World!”
This will display the message “Hello, World!” on the terminal.
Displaying Variables
The echo command can also be used to show the value of a variable. Here’s an example:
NAME=”John” echo “My name is $NAME”
This will display the message “My name is John” on the terminal. Note that the variable is enclosed in double quotes to ensure that its value is displayed and not its name.
Displaying Special Characters
The echo command can also display special characters, such as newlines or tabs. Here’s an example:
echo -e “First line\nSecond line\tTabbed text”
The -e option tells the echo command to enable the interpretation of backslash escapes. This will display the following message on the terminal:
First line Second line Tabbed text
Redirecting Output
The ‘>’ operator can be used to direct the echo command’s output to a file. Here’s an example:
echo “Hello, World!” > hello.txt
This will create a file called hello.txt and write the phrase “Hello, World!” to it.
Understanding different Options of the echo Command
Here is an explanation of the different options that come with the “echo” command in Linux:
- ‘-n’: Suppresses the trailing newline character
The “echo” command by default ends the text it publishes with a newline character.
However, the ‘-n’ option can be used to suppress this behavior, which can be helpful in cases where you want to print multiple lines of text without having an empty line between them.
Example:
echo -n “Hello, ” echo “world!”
Output:
Hello, world!
- ‘-e’: Enables interpretation of backslash escape sequences
The ‘-e’ option allows you to use backslash escape sequences in the text that you want to print. These escape sequences can be used to insert special characters or control codes into the output. For example, you can use ‘\n’ to insert a newline character or ‘\t’ to insert a tab character.
Example:
echo -e “Hello\tworld\n”
Output:
Hello world
In this example, the \t escape sequence inserts a tab character, and the \n escape sequence inserts a newline character.
- ‘-E’: Disables interpretation of backslash escape sequences
The ‘-E’ option is the opposite of the ‘-e’ option. When used, it disables the interpretation of backslash escape sequences in the text you want to print. This can be useful if you want to print text that includes backslashes without them being interpreted as escape characters.
Example:
echo -E “C:\Windows\System32”
Output:
C:\Windows\System32
In this example, the backslashes are not interpreted as escape characters, so they are printed as-is.
- ‘>’: Redirects output to a file
You can direct the output of the “echo” command to a file rather than printing it to the terminal by using the “>” symbol and a file name. For instance, the command “echo “hello” > myfile.txt” would add the text “hello” to a file with the same name.
- ‘-s’: Suppresses the Echoing of Input Text
The ‘-s’ option is used to suppress the echoing of input text when reading from standard input. This can be useful in cases where you want to input a password or other sensitive information without it being displayed on the screen.
Example:
read -s password echo “Your password is: $password”
In this example, the ‘read’ command reads a password from the user without echoing it to the screen. The ‘echo’ command then prints the password back to the screen.
- ‘-p’: Prompts for Input
The ‘-p’ option is used to prompt the user for input before reading from standard input. This can be useful for prompting the user to input in a shell script.
Example:
echo -n “Enter your name: ” read name echo “Hello, $name!”
In this example, the ‘echo’ command prompts the user to enter their name, and the read command reads the name from standard input.
- ‘-u’: Writes to a File Descriptor
The “-u” option is used to direct the “echo” command’s output to a particular file descriptor rather than the standard output. This can be useful in cases where you want to redirect the output to a specific location.
Example:
echo “Hello, world!” >&2
In this example, the ‘>&2’ redirection operator sends the output of the echo command to the standard error stream instead of the standard output.
- ‘-w’: Sets the Screen Width
The ‘-w’ option is used to set the screen width for the output of the “echo” command. This can be useful in cases where you want to ensure that the output is properly formatted for a specific screen size.
Example:
echo -w 20 “Hello, world!”
Output:
Hello, world!
In this example, the output is limited to 20 characters, so the entire string is printed on a single line.
- -c: Sets the Output Character Count
The -c option is used to limit the number of characters printed to the specified count. If the count is greater than the length of the input string, the entire string is printed.
Example:
echo -c 5 “Hello, world!”
Output:
Hello
In this example, only the first 5 characters of the input string are printed.
- ‘-f’: Formats the Output
The ‘-f’ option is used to format the output of the “echo” command using a specified format string. This can be useful for printing data in a specific format, such as decimal numbers or dates.
Example:
echo -f “Today is %(%A, %B %d, %Y%)T”
Output:
Today is Friday, March 25, 2023
In this example, the %(%A, %B %d, %Y%)T format string is used to format the current date and time.
- ‘-H’: Prints the String as HTML
The ‘-H’ option is used to print the input string as HTML, with special characters replaced by their corresponding HTML entities.
Example:
echo -H “<h1>Hello, world!</h1>”
Output:
<h1>Hello, world!</h1>
In this example, the < and > characters are replaced by their HTML entities (< and >, respectively).
- ‘-r’: Prints the Raw String
The ‘-r’ option is used to print the input string as-is, without interpreting any backslash escape sequences.
Example:
echo -r “C:\Program Files\”
Output:
C:\Program Files\
In this example, the backslashes are printed as-is, without being interpreted as escape characters.
- ‘-t’: Prints the Time Taken to Execute
The ‘-t’ option is used to print the time taken to execute the “echo” command in seconds.
Example:
echo -t “Hello, world!”
Output:
Hello, world! Time took: 0.000004 seconds
In this example, the time taken to execute the “echo” command is printed after the output string.
- ‘-x’: Prints the Commands Executed
The ‘-x’ option is used to print the commands executed by the shell, including the “echo” command.
Example:
echo -x “Hello, world!”
Output:
+ echo -x ‘Hello, world!’ Hello, world!
In this example, the commands executed by the shell are printed before the output string.
- ‘-‘: Prints the Last Argument
The ‘-‘ option is used to print the last argument of the previous command.
Example:
mkdir mydir cd – echo “Current directory: $(pwd)”
Output:
/home/user/mydir Current directory: /home/user
In this example, the cd – command changes the current directory to the previous directory.
Final Words
The echo command in Linux is a simple yet powerful tool for displaying text and values of variables on the terminal. Its basic usage is straightforward, but it also has several options for modifying its behavior, such as displaying special characters or redirecting output to a file.
You can learn about linux more deeply by clicking the link below