×

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

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

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

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.

sed Command in Linux/Unix with Examples

The 'sed' 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...

3 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

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.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 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 mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute 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.

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.

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.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute 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.

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.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

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

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.

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.