Questions tagged [printf]

The shell builtin command that formats and prints data. Questions that deal with printf command or broadly using printf syntax by programming language (like awk, PHP, perl, C++...)

printf stand for print formatted. printf is shell builtin command used to print and format data and outputs to stdout.

In addition, many programming languages like awk, C++, PHP, perl use/include printf.

Useful links:

Example of usage on this site: Why printf is better than echo?

274 questions
692
votes
4 answers

Why is printf better than echo?

I have heard that printf is better than echo. I can recall only one instance from my experience where I had to use printf because echo didn't work for feeding some text into some program on RHEL 5.8 but printf did. But apparently, there are other…
amphibient
  • 12,222
  • 18
  • 62
  • 87
70
votes
4 answers

Dashes in printf

I'm trying to use printf to format some pretty output in a bash script e.g.: ----------------------- | This is some output | ----------------------- But I've stumbled over some behavior I don't understand. $ printf "--" gives me the…
Kenny Rasschaert
  • 1,183
  • 2
  • 9
  • 13
61
votes
3 answers

Why is printf "shrinking" umlaut?

If I execute the following simple script: #!/bin/bash printf "%-20s %s\n" "Früchte und Gemüse" "foo" printf "%-20s %s\n" "Milchprodukte" "bar" printf "%-20s %s\n" "12345678901234567890" "baz" It prints: Früchte und Gemüse foo Milchprodukte…
René Nyffenegger
  • 2,201
  • 2
  • 23
  • 28
39
votes
5 answers

print output to 3 separate columns

MYPATH=/var/www/html/error_logs/ TOTALFILE=$(ls $MYPATH* | wc -l) FILETIME=$(stat --format=%y $MYPATH* | head -5 | cut -d'.' -f1) FILE=$(ls -1tcr $MYPATH* | head -5 | rev | cut -d/ -f1 | rev) TOPLINE=$(head -1 $MYPATH* | grep -Po '".*?"' | head…
ADM
  • 501
  • 1
  • 4
  • 7
34
votes
3 answers

Awk printf number in width and round it up

I need to printf a number out, but with given width and rounded (with awk!) %10s I have this and somehow I need to connect the %d but everything I do, ends up with too much parametres for awk (because I have more columns).
Wanderer
  • 639
  • 3
  • 7
  • 11
31
votes
3 answers

Using `printf` to print variable containing `%` percent sign results in "bash: printf: `p': invalid format character"

I want to use printf to print a variable. It might be possible that this variable contains a % percent sign. Minimal example: $ TEST="contains % percent" $ echo "${TEST}" contains % percent $ printf "${TEST}\n" bash: printf: `p': invalid format…
finefoot
  • 2,940
  • 2
  • 21
  • 41
17
votes
2 answers

Why won't some unicode characters print to my terminal?

I'm running Arch Linux with simple terminal using the Adobe Source Code Pro font. My locale is correctly set to LANG=en_US.UTF-8. I want to print Unicode characters representing playing cards to my terminal. I'm using Wikipedia for reference. The…
Brian Fitzpatrick
  • 2,755
  • 3
  • 23
  • 43
16
votes
3 answers

How to do formatted printing with jq?

jq has built-in ability to convert numbers to string or concatenate strings. How can I format strings inside jq similar to printf like padding (%4s). For example, how can I force number to occupy 10 char spaces with left alignment? echo '{"title" :…
Zeta.Investigator
  • 880
  • 1
  • 7
  • 25
16
votes
1 answer

How do I stop Bash from interpreting octal code instead of integer?

I'm writing a a Horoscope RSS script as an intro to Unix/Linux project, and ran into an issue my Professor couldn't fix in the time I was able to stay after class. Since I couldn't figure out how to use actual dates as what to check, I thought I'd…
Irre Levant
  • 161
  • 1
  • 3
16
votes
4 answers

How do I capture stdin to a variable without stripping any trailing newlines?

In a shell script... How do I capture stdin to a variable without stripping any trailing newlines? Right now I have tried: var=`cat` var=`tee` var=$(tee) In all cases $var will not have the trailing newline of the input stream. Thanks. ALSO: If…
CommaToast
  • 375
  • 1
  • 2
  • 10
15
votes
2 answers

Are there security consequences from not giving printf a format to use?

A well-formed printf usually has a format to use: $ var="Hello" $ printf '%s\n' "$var" Hello However, what could be the security implications of not providing a format? $ printf "$var" Hello As the variable expansion is quoted, there should not be…
user232326
15
votes
6 answers

Only print output after finding pattern

There's a script (let's call it echoer) that prints to screen a bunch of information. I'd like to be able to only see lines after a pattern is found. I imagine the usage of a solution to look something like echoer | solution_command…
user23146
  • 153
  • 1
  • 5
15
votes
2 answers

Slightly confused about whether printf in the yash shell is a built-in command or not

The yash shell has a printf built-in, according to its manual. However, this is what I see in a yash shell with default configuration: $ command -v printf /usr/bin/printf $ type printf printf: a regular built-in at /usr/bin/printf Is printf a…
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
14
votes
6 answers

Padding trailing whitespaces in a string with another character

I'd like to output hello world over 20 characters. printf "%-20s :\n\n" 'hello world!!' # Actual output hello world!! : # Wanted output hello world!!========: However, I don't want to complete with spaces but with "=" instead. How do I do…
smarber
  • 1,181
  • 2
  • 12
  • 25
14
votes
5 answers

bash + using printf in order to print in special format

I just wrote the following bash script to check the ping access on list of Linux machines: for M in $list do ping -q -c 1 "$M" >/dev/null if [[ $? -eq 0 ]] then echo "($C) $MACHINE CONNECTION OK" else echo "($C)…
yael
  • 1,511
  • 11
  • 33
  • 57
1
2 3
18 19