Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printf example in bash does not create a newline

Tags:

bash

printf

Working with printf in a bash script, adding no spaces after "\n" does not create a newline, whereas adding a space creates a newline, e. g.:

  1. No space after "\n"

    NewLine=`printf "\n"` echo -e "Firstline${NewLine}Lastline" 

    Result:

    FirstlineLastline 
  2. Space after "\n "

    NewLine=`printf "\n "` echo -e "Firstline${NewLine}Lastline" 

    Result:

    Firstline  Lastline 

Question: Why doesn't 1. create the following result:

Firstline  Lastline 

I know that this specific issue could have been worked around using other techniques, but I want to focus on why 1. does not work.

Edited: When using echo instead of printf, I get the expected result, but why does printf work differently?

    NewLine=`echo "\n"`     echo -e "Firstline${NewLine}Lastline" 

Result:

    Firstline     Lastline 
like image 454
WolfHumble Avatar asked Apr 20 '10 15:04

WolfHumble


People also ask

Does printf print on a newline?

The printf statement does not automatically append a newline to its output. It outputs only what the format string specifies. So if a newline is needed, you must include one in the format string.

How do I not print a new line in bash?

Bash is the command console in Linux and Mac OS, which also recognizes the 'echo' command. In the case of Bash, echo also creates a new line in the output, but you can use different steps to stop it. The best way to remove the new line is to add '-n'. This signals not to add a new line.

How do I print a new line in bash?

Printing Newline in Bash The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.

How do I create a new line in printf?

The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen.


2 Answers

The backtick operator removes trailing new lines. See 3.4.5. Command substitution at http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html

Note on edited question

Compare:

[alvaro@localhost ~]$ printf "\n"  [alvaro@localhost ~]$ echo "\n" \n [alvaro@localhost ~]$ echo -e "\n"   [alvaro@localhost ~]$ 

The echo command doesn't treat \n as a newline unless you tell him to do so:

NAME        echo - display a line of text [...]        -e     enable interpretation of backslash escapes 

POSIX 7 specifies this behaviour here:

[...] with the standard output of the command, removing sequences of one or more characters at the end of the substitution

like image 75
Álvaro González Avatar answered Oct 02 '22 11:10

Álvaro González


Maybe people will come here with the same problem I had: echoing \n inside a code wrapped in backsticks. A little tip:

printf "astring\n" # and  printf "%s\n" "astring"  # both have the same effect. # So... I prefer the less typing one 

The short answer is:

# Escape \n correctly !  # Using just: printf "$myvar\n" causes this effect inside the backsticks: printf "banana "  # So... you must try \\n  that will give you the desired  printf "banana\n"  # Or even \\\\n if this string is being send to another place  # before echoing,  buffer="${buffer}\\\\n printf \"$othervar\\\\n\"" 

One common problem is that if you do inside the code:

echo 'Tomato is nice' 

when surrounded with backsticks will produce the error

command Tomato not found. 

The workaround is to add another echo -e or printf

printed=0  function mecho(){   #First time you need an "echo" in order bash relaxes.   if [[ $printed == 0 ]]; then     printf "echo -e $1\\\\n"     printed=1   else     echo -e "\r\n\r$1\\\\n"   fi } 

Now you can debug your code doing in prompt just:

(prompt)$  `mySuperFunction "arg1" "etc"` 

The output will be nicely

 mydebug: a value  otherdebug: whathever appended using myecho  a third string 

and debuging internally with

mecho "a string to be hacktyped" 
like image 30
Sergio Abreu Avatar answered Oct 02 '22 11:10

Sergio Abreu