Saturday, January 27, 2024

[SOLVED] Why use printf '%s\n' "message"?

Issue

if [[ -z "$usr_name" ]]; then
   printf '%s\n' "No input entered"
   exit 1

What is the meaning of '%s\n' and why would you add it instead of just using:

printf "No input entered"

Solution

printf is used for format and print data. In the case of:

printf '%s\n' "No input entered"

%s represents a string place holder for the space separated string that follows and "\n" represents a line feed.

%s will then be substituted for "No input entered", followed by a line feed.



Answered By - Raman Sailopal
Answer Checked By - David Goodson (WPSolving Volunteer)