echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to concatenate strings. The basic syntax for the command is:

echo [string or variable]

For example, to display the text "Hello, World!" on the terminal, you would use the following command:

echo "Hello, World!"

You can also use the echo command to print the value of a variable by prefixing the variable name with a dollar sign ($).

myvar="Hello, World!" echo $myvar

You can also use the echo command to concatenate strings by using the double quotes (").

string1="Hello"
string2=" World!"
echo "$string1$string2"

It will print "Hello World!"

You can also use the -n option to prevent a new line from being added after the echoed text.

echo -n "Hello, World!"

The -e option can be used to enable the interpretation of backslash escapes. This can be useful for adding special characters such as newlines, tabs, and backspaces to the text being echoed. For example:

echo -e "Hello\tworld\n"

It will print "Hello World"

Additionally, you can use the echo command to create a new file with the content you want to print, using the > symbol.

echo "Hello, World!" > file.txt

his command will create a file named file.txt with the text "Hello, World!" in it.

The "echo" command in Linux is a versatile command-line utility that can be used in a variety of ways, as described above.

One useful feature of the echo command is its ability to interpret special characters, such as newlines (\n) and tabs (\t). These special characters can be used to format the text that is displayed on the terminal.

Another feature of the echo command is its ability to create new files with the content you want to print. By using the > symbol, you can redirect the output of the echo command to a new file, like this:

echo "Hello, World!" > file.txt

This command will create a new file named "file.txt" with the text "Hello, World!" in it. If the file already exists, it will be overwritten. If you want to append the content to an existing file, you can use the >> symbol instead.

echo "Hello again" >> file.txt

The echo command also supports the use of variables within the text that is being printed. For example, you can use the echo command to print the value of a variable like this:

name=John
echo "Hello, $name"

This will print "Hello, John"

Additionally, the echo command supports the use of command substitution, which allows you to include the output of a command within the text being printed. The command is enclosed within backticks (`) or $( ).

echo "Today is $(date)"

This command will print the current date.

In summary, the echo command is a simple yet powerful command-line utility that can be used to display text on the terminal, create new files, print the value of variables, and include the output of other commands in the text being printed.