• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

print new line character in Unix / Linux

July 9, 2015 by Sharad Chhetri

This is quick post on print new line character in Unix / Linux. Here, we will share two methods of print / echo new line character. Generally , when we do bash/shell scripting most of the time we require this.

Let’s start this essential basic of bash/shell scripting.

echo command : new line character

In this section , we are using echo command to print new line character. Echo command can be used in various ways as per the requirement. Through echo command, you can also find out success status of last command run.

To print new line character by echo command use the given below format

The backslash character n should be used with -e option.
-e = For enabling interpretation of backslash escape
-n = Do not output the trailing newline
n = For new line [This is backslash character which print to new line]

Example 1 , with -e and n .

echo -e "Write some wordnNow printed new line"

Example 2, with -e , -n and n . You may have notice -en is written in this example. The -e and -n together can be written as -en .

echo -en "Write some wordnNow printed new line"

You may be thinking, what is the difference in these two examples. In Example 1 there is trailing new line whereas in Example 2 you will not find trailing new line due to use of -n with echo command . See the below given screenshot for clear understanding.

echo command

printf command : new line character

This is another alternate method to print new line character by using printf command. By default, on using only backslash character n , it will not print trailing new line (Just as we have seen in echo command Example 2)

To print new line character by printf command use the given below format

Printf command is used for format and print data. your shell may have its own version of printf .
Use the n option along with printf command.

n = For new line [This is backslash character which print to new line]

printf "Write some wordnNow printed new line"

Given below is screenshot of above example.

printf command

Note : It is good to know both method, because you may experience undesirable output depending upon the bash / command version. I personally prefer printf command.

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. print working directory ( pwd , PWD , OLDPWD ) in Linux / Unix
  2. Unix / Linux : How to print duplicate lines from file
  3. setfacl: Option -m: Invalid argument near character 3
  4. How to print particular line number by using sed command
  5. awk command to print columns from file
  6. Print double hyphen sign simultaneously in post of Octopress
  7. Bash : print variable value inside single and double quotes
  8. Print grep command output without seperator
  9. How To Find Absolute Full Path Of Command On Linux / Unix : which command
  10. rsync over ssh port number on Linux/Unix system

Filed Under: Linux Tagged With: linux command

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

How to see line numbers in file through cat command

The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form

How to install pam_mysql in CentOS or Red Hat

How to protect from port scanning and smurf attack in Linux Server by iptables

Show visitor ip address php code

master admin password openerp 7.x

Protect from w00tw00t.at.blackhats.romanian.anti-sec

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy