Monday, April 18, 2011

bash printf to variable

If you want to use bash's printf formatting and store the value into a variable, you can use bash's print's -v flag as follows:

calc_elaspsed_time()
{
    local SECONDS=$1
    ((h=SECONDS/3600))
    ((m=SECONDS%3600/60))
    ((s=SECONDS%60))
    printf -v ELAPSED_TIME "%02d:%02d:%02d" $h $m $s
} 

1 comment: