×

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.


Related Topics

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

12 minutes read.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files...

4 minutes read.