Difference between printf and echo in Bash

后端 未结 4 1220
悲哀的现实
悲哀的现实 2021-01-21 08:45
$ printf \'apple\' | wc -m
       5
$ echo \'apple\' | wc -m
       6

Why printf prints 5 and echo prints 6 characters?

4条回答
  •  伪装坚强ぢ
    2021-01-21 09:49

    Printf does not give new line after execution of command but echo gives new linw after the execution of command. Please see the bewlow code output.

    Use of Printf:

    !/bin/sh

    a=0

    while [ $a -lt 10 ] do printf $a a=expr $a + 1 done

    o/p: 0123456789

    Use of Echo:

    !/bin/sh

    a=0

    while [ $a -lt 10 ] do echo $a a=expr $a + 1 done

    o/p:It will print in a column wise as this window not taking as a column wise please try to execute this code it will give better understanding.

    0 1 2 3 4 5 6 7 8 9

    you can remove the new line in echo by giving the bewlow command echo -n $a

提交回复
热议问题