Echo and Printf! Difference between them in displaying Output, Part 1 - AkuCode


Echo, and why should you avoid it?

When I started writing shell scripts, I immediately learned about two branches of Unix: AT & T system V and BSD. One of their differences is the result of ECHO. Created in all the modern bash shell, echo prints its arguments with one space between them into a standard output stream, followed by a new line:
Echo Quick Brown fox
The Quick Brown Fox
A new by default can be pressed into one of the events, depending on the shell;

echo -n No newline No newline$ echo “No newline\c” No newline$

The BSD variation of the The echo accepts the-e option, which presses a new line. The AT & T version uses an escape sequence, c, to do the same thing. Or vice versa? I'm having trouble remembering which is because even though I use the AT & T system (Hardware and operating system), the Echo command received both the AT & T and BSD syntax.

Of course, it is a history on the Bash shell Learning, we are dealing with bash. Bash has the-e option to enable escape sequences such as c but by default the-N usage to prevent new rows from being printed. (The escape sequence identified by "echo"-e is the same as that described in the next section with the addition of c).

The problem is that bash has the option xpg_echo (XPG stands for the X/Open portability Guide, the specifications for the Unix system) which makes the echo behave like any other version. And it can be turned on and off while it is in the bash shell (using shopt-s xpg_echo either in the command line or inside the script), or it can be activated when the shell is compiled. In other words even in bash, you can't really be sure what results you'll get.


If you restrict the use of echo for a current situation where there could not be a conflict, that is when you are sure the argument does not start with-N and does not contain escape sequences, you will be safe enough. For others or if you're unsure, use printf.

Printf: Format and print data
Derived from the C programming language function with the same name, the Shell printf command has the same purpose but differs in some detail. Like the C function, it uses a string format to show how it presents its arguments:
Printf FORMAT ARG

The FORMAT String can contain regular characters, breakout sequences, and formatting specifiers. The usual characters they represent. The format picker is replaced with an argument from the command line.
Escape Sequences

Is a single letter preceded by a backslash:
·       \a: Alert (bell)
·       \b: backspace
·       \e: escape character
·       \f: form feed
·       \n: newline
·       \r: carriage return
·       \t: horizontal tab
·       \v: vertical tab
·       \\: backslash
·       \nnn: Characters defined by one to three octal digits
·       \xHH: Characters specified by one to two digits DataReader
The backslash must be protected from the shell with quotation marks or other Backslash:
Printf “Q\t\141\n\x42\n”
Q         a
B
            Articles that learn about printf are still ongoing and will be available in the next post! 
Comments