Home > Enterprise >  How to use printf in bash to print on screen and output into a variable
How to use printf in bash to print on screen and output into a variable

Time:02-22

How can I printf "abc" in bash and at the same time append a variable with the text from printf?

CodePudding user response:

You can't do it in one go, but you can output the variable once it contains the output.

text=$(printf abc)
printf '%s' "$text"

CodePudding user response:

Or @choroba' example conditionally...

text=$(printf abc) && printf '%s' "$text" || printf "FAIL!\n"

Thats a shorthand for: if [ command ]; (&&) then command; (||) else command; fi

CodePudding user response:

Or, using -v:

printf -v text abc
printf '%s' "$text"
  • Related