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

learn more… | top users | synonyms

314
votes
5answers
120k 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 ...
6
votes
5answers
367 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) $...
0
votes
1answer
24 views

How to print ${STR1} and $STR2 - not their contents - into a file?

I need to print expressions ${STR1} and $STR2 into a file -- not the content of the variables. How can I accomplish this using echo or printf?
0
votes
1answer
241 views

print characters repetitively using asterisk symbol in bash [duplicate]

print '*' * 80 This python snippet prints 80 asterisks, which I use often as delimiters in log files. How can I do it in bash script using operator overloading?
1
vote
2answers
47 views

print multiple words seperated by space

I have a text file and the data inside has format like (name age country): michael jordans 25 US adam smith 30 UK chris wood ABC 22 Aus if I use command: cat text.txt | awk {'print $1'} --> it ...
4
votes
2answers
2k views

bash script , echo output in box

I have created this Function , which generate below output on terminal , but this function seems complex, So I posted here for any improvement or for same alternate solution. #!/bin/bash function ...
0
votes
1answer
35 views

What is special about printf over echo

I was finding length of a variable as below: `set num=123456` `echo $num|wc -c` it returned output as 7 (suppose to return 6) I did same with printf `printf "%s" $num|wc -c` it returned output ...
2
votes
0answers
39 views

Portable color codes with printf? [duplicate]

There is a color codes script on GitHub suitable for sourcing, which I am modifying for my own purposes. Background I have an odd mix of environments to work in, from a Mac OS terminal, to various ...
3
votes
2answers
139 views

posting data using cURL in a script

I'm trying to write a simple alternative script for uploading files to the transfer.sh service. One of the examples on the website mentions a way of uploading multiple files in a single "session": $ ...
4
votes
5answers
4k 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" ...
1
vote
2answers
37 views

Get the output in a formatted way

In a for loop, I am using the command: echo " Job_time is $i : Stopping the job " Sample output: Job_time is 6 : Stopping the job Job_time is 6.50 : Stopping the job Desired output: Job_time is ...
0
votes
0answers
13 views

running command in Mobax Terminal are there plugins that are needed

while read CTFlist; do #next we need to create ctfs padded with zeros as a variable ctfPadded=$(printf ${ctflist}00000000) #then call rc2 key as variable rc2Key="TemporaryRC2Key1" #next create a ...
5
votes
2answers
938 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 ...
1
vote
2answers
98 views

format command output with variable and fixed space

How can I achieve this best maintaining the hierarchy on the left and aligning the size value on the right? Also, at the same time I'd like to round the size to one or two digits. I tried with printf ...
3
votes
2answers
66 views

use printf to format output of an array

I have an array that contains details about each NIC. Each array index consists of three space-separated values. I would like to have a nice table as output. Is there a way to assign formatting ...
8
votes
3answers
30k 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) ...
0
votes
1answer
32 views

Printing Space Between Strings

#!/bin/bash ARRAY="185.18.207.66 31.18.212.113" result="" for i in $ARRAY do result=$(printf '%s %s' "$result" "$i" "checked") done paste <(printf "%s\n" $result) I am trying to print ...
-1
votes
3answers
111 views

Why am I getting an error “print_unicode: line 9: printf: missing unicode digit for \u Unicode 0187” with this script

I have written them following shell script to view what the Unicode characters look like on my terminal. #!/bin/bash X=0 while [ $X -lt 65536 ]; do HEX=`bc <<< "obase=16; $X"` HEX="...
3
votes
2answers
423 views

Printf printing only a single line

I'm trying to get the output of env in a shell variable and print it. #!/bin/sh ENV_BEFORE=$(env) printf $ENV_BEFORE As a result a single variable from the env output gets printed. When using echo ...
1
vote
1answer
89 views

Bash script how to accept single or double digit number and make it 2 digit

I have a following simple extract from my script which is strangely stuck up for a particular inputs. echo -e "Enter the Day of the Month : \c " read input_day printf -v input_day '%02d' "$...
3
votes
1answer
51 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?
0
votes
0answers
34 views

Include text from other file in screen printf

The original code of main.sh: screen -S myscreenname -p 0 -X stuff "`printf "myabcdefg"`" I want to put only abcdefg (without my) in other file. Below codes works just fine but I need the text.txt ...
9
votes
3answers
2k 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?
3
votes
2answers
51 views

using printf function for repeated patterns

I have codes which append the patterns into text file w.r.t. the user's input as follows; echo -n "WHICH STATIONS?" read station awk -v input="$station" ' BEGIN { n = split(tolower(input), ...
3
votes
3answers
740 views

How do I use a multi-character delimiter for array expansion in bash?

I'm reducing the question to (I believe) the simplest case. Let's say I have a script myscript.sh with the following contents: #!/bin/bash IFS='%20' echo "$*" If I run the command as follows, the ...
4
votes
2answers
648 views

Thousands separator in printf in zsh

I have been trying to define a thousands separator in printf for a while now and I have discovered that zsh has some problems with it. In bash I can use something like: $ printf "%'d\n" 1234567890 ...
0
votes
1answer
42 views

Display non-printable parameters in running processes

ps aux displays commands running on the server, but it does not display non-printable parameters in any sort of way that is useful. mycmd.sh: #!/bin/bash while true do sleep 5 done ...
6
votes
3answers
617 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 ...
2
votes
4answers
394 views

How do I append multiple lines involving variables to the end of a bash script?

I would like to add: function ps_mem { python /home/vagrant/ps_mem/ps_mem.py -p $@ } To the end of ~/.bashrc from the command-line. I have tried using: printf "function ps_mem {\n python /home/...
6
votes
1answer
1k 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 ...
2
votes
4answers
487 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 ...
0
votes
0answers
76 views

Logging to a file in tabular format

I am trying to print something into a file through the script which will be sent as an attachment to the mail. Now what i am willing is to preserve a proper formatting within the file. Can i print ...
2
votes
3answers
62 views

Print columns of data that wrap internally

I am printing some monitoring data from a log as such: printf " %10s %5s %25s %15s %15s %s${txtrst}\n" $date $time $metric $status $current_criticality "$failure" I would like the last ...
4
votes
3answers
558 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 ...
0
votes
1answer
62 views

Need help understanding this cpu utilization script

Need help in understanding the below Script : HEADER='CPU pctUser pctNice pctSystem pctIowait pctIdle' HEADERIZE="BEGIN {print \"$HEADER\"}" PRINTF='{printf "%-3s %9s %9s %9s %9s %...
3
votes
1answer
2k views

convert timestamp to epoch with bash builtin printf

While researching a different approach to convert a timestamp like 2015-09-08 11:07:35 to 1441703255 I found a lot of pretty commonly found solutions use e.g. date +%s --date "2015-09-...
4
votes
1answer
154 views

bash prevent printf from interrupting another printf

I have the following test script. #!/bin/bash function foo { printf "Test line break: $1\nafter line break\n\n" } for VARIABLE in {1..30} do foo $VARIABLE & done wait Now I executed the ...
-1
votes
1answer
213 views

How to rename or delete an alias permanently from system?

I have created one alias in my system, printf "%s\n" "alias shh='sqlplus hfdora/hfdora@hfd2" >> ~/.kshrc Now, I want to change the alias name from shh2 to shh Also, how to delete other ...
1
vote
2answers
192 views

Unable to store ascii value in a variable

I am trying to convert hex to decimal to ascii and store it in a variable. I am using the following code. HEX=30 DEC=`printf "%d\n" 0x${HEX}` echo "$DEC" ASC=`printf \\$(printf '%03o' $DEC)` echo "$...
3
votes
3answers
55 views

printing a series of characters

My ultimate goal here is to generate a block of text that can be used to test out various fonts at a terminal. I want to generate the file as basically an ascii chart. A series of characters from 1 ...
1
vote
2answers
73 views

Alignment when printing a series of records

How can I resolve the problem of the tables going to the right. I just want it to be shown under 1. Here's my script with START added. The alignment has gone wonky now: while IFS="," read f1 f2 f3 ...
4
votes
3answers
3k 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 ...
1
vote
2answers
337 views

bash - Why does type -a execute “$(printf 'a')” and is it possible to get back the original alias? [duplicate]

After i ask this question 2 days ago, i decided to make this alias in my ~/.bashrc: alias catvu="LC_ALL=C sed \"$(printf 's/[^\t -\176\200-\377]/^&/g')\"|LC_ALL=C tr '\0-\10\13-\37\177' '@-...
7
votes
3answers
16k 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 (...
1
vote
2answers
210 views

Format Column Width with Printf

I have searched online for 10 hours now and tried various codes, but still need help. I am attempting to merge three files using 'paste' and 'awk'. However, the columns are not adjusting to the ...
3
votes
1answer
3k 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" ...
3
votes
0answers
105 views

find printf switch POSIX alternative

I'd like to rewrite these 2 command so they will use only POSIX-complaint switches: find "$TARGET_DIR" -maxdepth 1 -type d -printf '(DIR) %f\n' find "$TARGET_DIR" -maxdepth 1 -type f -printf '%s %f ...
2
votes
1answer
325 views

How do I print hyphens if a variable is empty?

I am collecting windows system information using wmic on Linux system. For that purpose, I make various wmic calls and their output is stored in some variables. After required data is collected, I ...
0
votes
1answer
659 views

How do I escape quotes for json conversion in Bash

I have a file which I need to convert to a json string and it needs to be done via bash. My test file looks like 1 2 ' 3 4 Here is what's happening $ LOG=$(cat file) $ printf '{"log": "%q"}\n' "$...
3
votes
1answer
2k views

How to use printf to show the results of a calculation?

My program is #!/bin/sh printf 'Please enter salary amount: ' read salary TAX="echo $salary * 0.15 | bc" NET="echo $salary + $TAX | bc" printf "Net Salary= %.2f" $NET printf "\n" But whatever I ...