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

Filter by
Sorted by
Tagged with
621
votes
4answers
265k views

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 ...
58
votes
3answers
7k views

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"...
57
votes
3answers
31k views

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'...
36
votes
5answers
115k views

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) ...
27
votes
3answers
65k views

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 (...
16
votes
2answers
4k views

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 ...
15
votes
3answers
15k views

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 ...
15
votes
2answers
1k views

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 ...
14
votes
6answers
5k views

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 ...
14
votes
5answers
55k views

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) $...
13
votes
3answers
13k views

Writing a character N times using the printf command

I found the following command to repeat a character in Linux: printf 'H%.0s' {1..5000} > H.txt I want, for example, H to repeat 5000 times. What does %.0s mean here?
13
votes
6answers
3k views

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 ...
13
votes
1answer
4k views

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 ...
11
votes
1answer
948 views

How does printf round halves to the first decimal place?

I am testing two different implementations of printf on my system: printf (GNU coreutils) 8.26, and the version bundled with zsh 5.3.1. I am testing how half numbers are rounded, i.e. for 1.5, 2.5, 3....
11
votes
4answers
9k views

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 ...
10
votes
4answers
29k views

Pad a number with a zero

I'm trying to pad zero to a number a=010 printf "%04d" $a 0008 I need the "output" as "0010" it's converting the value. and even tried with typeset a=010 typeset -RZ2 a echo $a when I use the ...
10
votes
4answers
483 views

Modify width of first column in file with a variable number of fields, using awk

I understand how to use awk's printf function, but I don't want to specify every field. For example, assume this is my file: c1|c2|c3|c4|c5 c6|c7|c8|c9|c10 c11|c12|c13|c14|c15 I want to format it ...
10
votes
2answers
3k views

Why does `printf “%s”` concatenate two following strings together? [duplicate]

$ printf "%s" a b ab$ printf "%s%s" a b ab I have some problem understand the format specifier for printf. If I am correct it is mostly the same as those for strings in the C programming language. ...
9
votes
4answers
13k views

bash script , echo output in box

I have created this function, which prints the output as seen in the example image. But, the implementation of this function seems too complex. Is there a way that I can improve it or implement an ...
9
votes
2answers
1k views

Explanation needed on how I can repeat a character in POSIX shell

The following answer on Stack Overflow, How can I repeat a character in bash? imposes one plausible way of POSIX-ly repeating a single character, as follows. In this example let's use the equal sign ...
9
votes
3answers
8k views

How to print control characters with escape sequences?

I have defined the color red using tput red=$(tput setaf 1) to colorize warnings in my program. This works fine: printf '%sfail\n' "$red" # prints 'fail' in red But on one occasion I would like to ...
8
votes
6answers
1k views

repeating a character using printf and appending a newline at the end

In order to repeat a character N times, we could use printf. E.g to repeat @ 20 times, we could use something like this: N=20 printf '@%.0s' $(seq 1 $N) output: @@@@@@@@@@@@@@@@@@@@ However, there ...
8
votes
4answers
5k views

Weird float rounding behavior with printf

I've read some answers on this site and found the printf rounding desirable. However when I used it in practice, a subtle bug led me to the following behavior: $ echo 197.5 | xargs printf '%.0f' 198 ...
8
votes
3answers
372 views

Using `printf` to output chars given some ASCII numbers

I've been trying to make printf output some chars, given their ASCII numbers (in hex)... something like this: #!/bin/bash hexchars () { printf '\x%s' $@ ;} hexchars 48 65 6c 6c 6f Expected output: ...
8
votes
4answers
9k views

Human-readable filesize with find's printf

I am writing a simple helper function to show a specific folder in the format I want: find . -maxdepth 1 -not -name "." -printf '[%TY-%Tm-%Td]\t%s\t%f\n' | sort -n However I want to show the ...
8
votes
1answer
5k views

Where is `scanf` implementation source code?

I want to find C source code for scanf implementation on Linux machine. Googling to find scanf implementation does not tell me the way to find it. I tried to find that source code from gcc source tree ...
7
votes
6answers
11k views

Bash: How to generate random float number using $RANDOM

Is it possible to generate real random numbers with a specific precision and in a specific range using the Integer Random Generator $RANDOM? For example how we can generate real number with 4 ...
7
votes
3answers
8k views

How to prepend a line to all files in a directory?

I have a directory called backup with file extensions *.sql. The thing I want to do is to prepend a line to all sql files in the directory backup. I did echo 'use my_db;' >> backup/*.sql, which ...
7
votes
2answers
2k views

Why does Bash history not record this command?

The command printf "#!/bin/bash\n" > t generates an error(bash: !/bin/bash\n": event not found), but hitting the up key does not recall the command so that I can modify it. Why is that? (What I ...
7
votes
3answers
756 views

BASH print question (printf \\$(printf '%03o' $1))

I used the following to convert from int to char and char to int in bash. But I do not understand how printf \\$(printf '%03o' $1) or printf '%d' "'$1" work. Please explain how printf \\$(printf '%...
7
votes
5answers
19k views

Add leading characters in front of string using printf or echo

How can I add leading characters to fill a string to a certain length? Assume I want to add zeroes in front of a string, if said string is shorter than 20 characters: printf '%020s\n' "$line" ...
7
votes
1answer
1k views

Is it a bug that zsh print the full string with printf '%.s'?

The command printf '%.0s-' `seq 1 30`; echo works fine in all shells tested: /bin/jsh : ------------------------------ /bin/attsh : ------------------------------ /bin/y2sh : -------...
7
votes
1answer
410 views

POSIX alternative to GNU find's -printf predicate

I'd like to rewrite these 2 commands so they will use only POSIX-compliant switches: find "$TARGET_DIR" -maxdepth 1 -type d -printf '(DIR) %f\n' find "$TARGET_DIR" -maxdepth 1 -type f -printf '%s %...
6
votes
3answers
3k views

Why doesn't printf escape newlines?

$ printf "hi" hi$ printf "hi\n" hi $ printf "hi\\n" hi Why doesn't the last line print hi\n?
6
votes
3answers
22k views

Formatted shell script output

How can I fix the output width using printf? This is an example script: #!/bin/bash OK=$(printf '\t%+50s\n' OK) FAIL=$(printf '\t%+50s\n' FAIL) for i in a aa aaa aaaa aaaaaa aaaaaaaaaaaaaaaaa ...
6
votes
6answers
2k views

printf, awk … How to format a number with space to the thousands

Is there a simple way to separate a very large number per thousands with printf, awk, sed ? So 10000000000000 become 10 000 000 000 000 Thanks
6
votes
3answers
679 views

Different format in terminal vs in a .txt document

I have a script that tracks time. When I print my output in my console I get this: 0 Days, 00:00:33 When I save it to a txt file I get this: ^[[2K 0 Days, 00:00:33 Code: now=$(date +%s) diff=$...
6
votes
3answers
3k views

Prevent watch breaking colors

I'm altering scripts at work that monitor log files to single out certain items and colorize them. The final output is a list of 6-digit numbers in several columns. I've been able to add a ~ to the ...
6
votes
3answers
868 views

Shell builtin `printf` line limit?

The /usr/bin/printf util argument list length is limited to the shell's maximum command line length, (i.e. getconf ARG_MAX, on my system that'd be 2097152); example: # try using a list that's way too ...
6
votes
2answers
1k views

Repeat a variable with printf

if I use the following command: printf "%.0s┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃\n" {1..3} I get an output like this: ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃...
6
votes
4answers
2k views

printf: multibyte characters

When trying to format printf output involving strings containing multi-byte characters, it became clear that printf does not count literal characters but the number of bytes, which makes formatting ...
6
votes
2answers
8k views

How can I print a variable with padded center alignment?

How can I print $myvar padded so that it is in the center of the terminal, and to either side are = to the edge of the screen?
6
votes
4answers
10k views

How to use printf and %s when there are color codes?

I had these lines: if [[ $# -eq 0 ]]; then printf "$fail_color Error - Function: $function, Line: $line_number \n" printf "do_test: Third parameter missing - expected result\n" exit 1 fi This ...
6
votes
2answers
7k views

What characters need to be escaped when using the printf command?

I want to clarify that I am not talking about how to escape characters on the shell level of interpretation. As far as I can tell, only two character need to be escaped: % and \ To print a literal %,...
6
votes
1answer
3k views

Does printf have an “engineering” notation format for numbers?

Say I have this number: 105000000, in this form I can't easily see how big it is, so using printf at a prompt I try using the "scientific" notation: % printf "%.3E" 105000000 1.050E+08 This is ...
5
votes
2answers
257 views

Replacing date with EPOCH in column output

I have a set of data like so: user1 1234 01/01/2020 user2 2345 02/02/2020 I need to convert the third column (date) to EPOCH while retaining the username and id number. user1 1234 1577862000 user2 ...
5
votes
1answer
18k views

Printf formatting with variable format - what does this var reference?

I am experimenting with printf formatting in bash and I ran across this piece of code: #/bin/bash divider=============================== divider=$divider$divider header="\n %-15s %8s %10s %11s\n" ...
5
votes
1answer
3k views

printf and how to add dots “…” between variables and their values

We have the following output cat gh.log machine status ============================================================= connection_ght 400 pach_po 72 timeout 311 ...
5
votes
1answer
99 views

%c formatted string shows different behaviour in the shell and in C

If i write a very simple line of code in C: printf("Ascii char for %d is %c\n",65,65); It simply prints A, as ascii value of 65 corresponds to character A. But if I use the same code in shell and ...
5
votes
1answer
1k views

One liner for padding columns (similar to printf) of output from the previous pipe command?

I'd normally use printf to pad each cell with enough whitespace to maintain header position over its respective column. Ultimately I can just iterate over each line of the output and do enough cut ...

1
2 3 4 5