What does echo -n do in Linux? [SOLVED]


Linux, Shell Scripting

Author: Omer Cakmak
Reviewer: Deepak Prasad

echo is a command that outputs the statements specified as arguments to the command line. The command is available in the shell of various operating systems and is used in shell scripts to display the status text.

 

What does echo -n do in Linux?

echo in standard usage a line of text is entered and displayed in standard output:

[root@fedora ~]# echo "This is echo command example"
This is echo command example

It looks like the output is shown on a bottom line.  Echoing the trailing newline is skipped if the '-n' option is used:

[root@fedora ~]# echo -n "This is echo command example"
This is echo command example[root@fedora ~]#

Using -n in terminal is as above. So how about bash script?

 

Example: Using echo -n in shell script

Let's say you have a script with hundreds of lines. When you print each input you receive from the user, too many lines will be output. For example: It may be necessary to get Y/N answers on the same line:

foc@fedora:~$ nano myscript1.sh
#!/bin/bash
echo "Do you want to quit? [Y]/N";
read input

if [ -z "$input" ]; then
   echo "[Y]/N"
elif [ "$input" = "Y" ]; then
   echo -n "Y"
elif [ "$input" = "N" ]; then
   echo -n "N"
fi

Give the script execute(x) permissions:

foc@fedora:~$ chmod +x myscript1.sh

then execute the script:

foc@fedora:~$ ./myscript1.sh
Do you want to quit? [Y]/N

[Y]/N
foc@fedora:~$ ./myscript1.sh
Do you want to quit? [Y]/N
Y
Yfoc@fedora:~$ ./myscript1.sh
Do you want to quit? [Y]/N
N
Nfoc@fedora:~$

Let's compare the -e parameter with the -n parameter:

foc@fedora:~$ nano myscript2.sh
#!/bin/bash

if [ "$1" = "-n" ]; then
   echo -n "The parameter is -n"
elif [ "$1" = "-e" ]; then
   echo -e "The parameter is -e"
else
   echo "No parameter"
fi

Give the script execute(x) permissions:

foc@fedora:~$ chmod +x myscript2.sh

Then run the script without entering any parameters:

foc@fedora:~$ ./myscript2.sh
No parameter

The output of the script is on a new line.  run the script with the -e parameter:

foc@fedora:~$ ./myscript2.sh -e
The parameter is -e

Now let's give the -n parameter and look at the output:

foc@fedora:~$ ./myscript2.sh -n
The parameter is -nfoc@fedora:~$

Both console and script output are displayed on the same line.

 

Summary

Knowing the parameters of the applications you use will of course make your job easier. In this article, we have given examples of using the -n parameter of the echo command. You can visit the manual page for more information about echo.

Each command has a help parameter and is written as --help after the command. But its usage in echo command is as follows:

foc@fedora:~$ help echo
echo: echo [-neE] [arg ...]
    Write arguments to the standard output.
    
    Display the ARGs, separated by a single space character and followed by a
    newline, on the standard output.
    
    Options:
      -n	do not append a newline
      -e	enable interpretation of the following backslash escapes
      -E	explicitly suppress interpretation of backslash escapes
    
    `echo' interprets the following backslash-escaped characters:
      \a	alert (bell)
      \b	backspace
      \c	suppress further output
...

 

References

stackoverflow.com - How to NOT print a trailing newline after echo "<something>"
www.oreilly.com - Writing Output Without the Newline

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment