×

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

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

5 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 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.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

4 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.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 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.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

4 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.

man Command in Linux/Unix with Examples

The man command in Linux is used to display the manual pages for a given command or system call. These manual pages provide detailed information on the command's usage, options,...

3 minutes read.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

scp Command in Linux/Unix with Examples

The scp (secure copy) command in Linux is a command-line utility that is used to securely transfer files between two hosts over a network. It is similar to the cp...

4 minutes read.