[ACCEPTED]-format bash variable for command-shell

Accepted answer
Score: 20

In Bash, printf is provided by the shell (see 13 the bash(1) manpage, search for "printf"), so you 12 don't even have the (minimal) overhead of 11 fork() and exec() to execute a separate command -- unless 10 you run it from within backticks. Bash's 9 built-in printf lets you assign the output 8 to a given variable with the -v argument; your 7 script fragment could be rewritten as:

for ((i=23; i<42;i++)); do 
    printf -v ii "%02i\n" $i
    sh ../myprogram $ii

I'm 6 not sure I'd consider that more readable 5 than the original.

The most resource-intensive 4 part of your script fragment is calling 3 your other shell script; I wouldn't worry 2 about the overhead of calling printf unless 1 further evidence indicates otherwise.

edited to add the difference between backticks and direct execution

Score: 5

Your number already has 2 decimal places. Why 20 do you need to use printf then? If i remember 19 correctly (haven't got a shell for testing 18 here), it just pads the number up to 2 decimals 17 when used with those flags. Personally, i 16 like xargs:

seq 23 42 | xargs -n1 sh ../myprogram 

You can use the -w argument for 15 seq, which pads the numbers with zeros if necessary, so 14 they have all the same width.

It turns out 13 seq is linux specific. Thanks for Dave in the 12 comments for figuring it out (his answer). Use 11 printf directly, without a loop:

printf '%02i\n' {23..42} | xargs -n1 sh ../myprogram 

I like to use 10 xargs, because it allows easily running your 9 commands in parallel up to some limit, can 8 pass more than one number at once and allows 7 other flexible options. Like Dave, i recommend 6 you to drop the sh from it, and place it into 5 your shell script, as first line instead:


Then 4 just execute your stuff as

printf '%02i\n' {23..42} | xargs -n1 ../myprogram

This is more 3 generic, and allows your script also to 2 be called by the exec C library calls (at least 1 in Linux, that's the case).

Score: 1

FYI: A completely different solution:

jot -w "%02i" - 23 42 | xargs -n 1 ../myprogram

This 12 has the performance downside of calling 11 jot (standard since 4.2BSD so all the BSD derivatives 10 have it, but a quick look shows that this 9 basic wonderful tool appears to be lacking 8 from the Linux distributions I looked at). Edit: Linux 7 (at least Red Hat) appears to have a subset 6 of jot's features in a command called seq (thanks 5 to litb's answer for this info).

This has the benefit 4 of working in non-bash shells as well (unlike 3 what some people think, there is more than 2 one shell in active use and shell-agnostic 1 solutions are generally a good idea).

Score: 0

How about

for i in {23..42} ; do
    sh ../myprogram $i

Numbers between 23 and 42 are always 5 going to be in %02i format.

If you absolutely 4 must format, then

for i in {23..42} ; do
    printf "%02i\n" | xargs -n1 sh ../myprogram $i

Substitutes the overhead 3 of spawning xargs for the overhead of spawning 2 the subshell for the backticks. I have 1 no idea which is more efficient.

More Related questions