Skip to main content

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++...)

5 votes
1 answer
209 views

printf in Zsh does not shell escape exclamation mark

In Zsh 5.9, we can use printf to shell escape a string: $ printf '%q' 'One! Two' One\!\ Two This produces the correct output of escaping the ! and the space. Now let’s make it as a script: #!/bin/...
user137369's user avatar
7 votes
1 answer
587 views

printf - store formatted string output in a variable

Instead of the following printf '%s\t%s\t%s\t%s\n' 'index' 'podcast' 'website' 'YouTube' I want to store the printf output in a Results variable, how can I do this?
Porcupine's user avatar
  • 2,056
0 votes
1 answer
42 views

How do I pass hex characters to printf in a script command? [duplicate]

How do I get printf to output hex characters when run from a script? Suppose I am at the prompt and type printf "\x41\x42" the output I get AB% However if I have a script containing that ...
sgmoore's user avatar
  • 125
2 votes
1 answer
104 views

Why does printing an array with @ using printf in bash only print the first element?

I have an array snapshots=(1 2 3 4) When I run printf "${snapshots[*]}\n" It prints as expected 1 2 3 4 But when I run printf "${snapshots[@]}\n" It just prints 1 without a ...
geckels1's user avatar
  • 163
0 votes
1 answer
158 views

List all files in a directory, recursively, sorted by modification date

The main answer of Sort the files in the directory recursively based on last modified date gives a method to list all files in a directory, recursively, sorted by modification date: find -printf "...
Basj's user avatar
  • 2,549
0 votes
0 answers
16 views

How to preserve updating output in pipe, e.g. when output has `\r`

Whenever I pipe some output that is updating (say with \r), e.g. to a pager (less), or to colourise parts of it, the pipe seems to drop the lines that are updating. Any idea how to preserve the ...
suvayu's user avatar
  • 150
2 votes
1 answer
145 views

Print Variable containing backslashes

I have something like this: A=$(curl https://mysite.com) and the curl request returns the string \"Hello World\". When I now want to print A to the console using one of: echo "$A" ...
Cake's user avatar
  • 31
2 votes
1 answer
294 views

"ls" counterpart to "find" operator "-printf"?

The find command has a handy -printf operator that prints user-specified metadata for each found file/folder. Is there such an option for the ls command? As an alternative, I can feed the list of ...
user2153235's user avatar
-1 votes
1 answer
150 views

Why does printf not round numbers the way I expect in my bash? [duplicate]

prompt> printf "%.2f\n" 3.314 3.31 prompt> printf "%.2f\n" 3.315 3.32 This is fine, I got what I wanted. However if I use different numbers, I get this: prompt> printf &...
user3719454's user avatar
0 votes
2 answers
189 views

Why might I want to avoid such echo-like way of using `printf` when printing to stdout? [duplicate]

I wrote the following script when trying to understand how printf works: #!/usr/bin/bash printf "Give me your three preferences.? " read p1 read p2 read p3 printf "%s\n" "${...
John Smith's user avatar
0 votes
0 answers
46 views

How to pass a binary number to printf? [duplicate]

Such a silly question but I've been trying for at least 15 minutes already and it's late at night. How to use printf in bash to convert a binary number to hex? Here's what I've tried: $ printf %x 11 ...
Vorac's user avatar
  • 3,115
0 votes
2 answers
326 views

Bash and other shells $(...) remove the ending newline character [duplicate]

Why the shell structure $(...) removes the ending newline character? #!/bin/bash S='a b ' printf '%i [%s]\n' "${#S}" "$S" T=$(printf '%s' "$S") printf '%i [%s]\n' "...
Olivier Pirson's user avatar
0 votes
1 answer
22 views

how to write a commant for output of a particular group?

Reading file prod1.tpr, VERSION 2019.6 (single precision) Reading file prod1.tpr, VERSION 2019.6 (single precision) Select a group of reference atoms and a group of molecules to be ordered: Group ...
PRAMOD KUMAR's user avatar
0 votes
1 answer
111 views

Flush output continuously with xargs

The function output the size of the downloaded file and the speed of transfer separated with newline. Example: 20 #<< data 3.0M #<< speed 25 1.2M Now, it goes like this: output_data_func |...
decipher's user avatar
  • 312
2 votes
2 answers
309 views

Why does awk concatenate strings from different lines

Good day. I'm trying to run a ping6 on IPv6 addresses pulled from /etc/resolv.conf.dnsph. It seems though, as if awk is stringing the IPv6 addresses onto one line. $ grep ^nameserver /etc/resolv.conf....
Bjoern's user avatar
  • 31

15 30 50 per page
1
2 3 4 5
19