{"id":3371,"date":"2014-07-22T02:15:06","date_gmt":"2014-07-21T18:15:06","guid":{"rendered":"http:\/\/rmohan.com\/?p=3371"},"modified":"2014-07-22T02:19:03","modified_gmt":"2014-07-21T18:19:03","slug":"bash-printf-to-variable","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3371","title":{"rendered":"bash printf to variable"},"content":{"rendered":"<p>If you want to use bash&#8217;s printf formatting and store the value into a variable, you can use bash&#8217;s print&#8217;s -v flag as follows:<\/p>\n<p>calc_elaspsed_time()<br \/>\n{<br \/>\n    local SECONDS=$1<br \/>\n    ((h=SECONDS\/3600))<br \/>\n    ((m=SECONDS%3600\/60))<br \/>\n    ((s=SECONDS%60))<br \/>\n    printf -v ELAPSED_TIME &#8220;%02d:%02d:%02d&#8221; $h $m $s<br \/>\n} <\/p>\n<p>printf Command<br \/>\nThe built-in printf (print formatted) command prints a message to the screen. You will use this command a lot in shell scripts. printf is very similar to the C standard I\/O printf() function, but they are not identical. In particular, single- and double-quoted strings are treated differently in shell scripts than in C programs.<\/p>\n<p>The first parameter is a format string describing how the items being printed will be represented. For example, the special formatting code \u201c%d\u201d represents an integer number, and the code \u201c%f\u201d represents a floating-point number.<\/p>\n<p>$ printf \u201c%d\\n\u201d 5<br \/>\n5<br \/>\n$ printf \u201c%f\\n\u201d 5<br \/>\n5.000000<br \/>\nInclude a format code for each item you want to print. Each format code is replaced with the appropriate value when printed. Any characters in the format string that are not part of a formatting instruction are treated as printable characters.<\/p>\n<p> $ printf \u201cThere are %d customers with purchases over %d.\\n\u201d 50 20000<br \/>\nThere are 50 customers with purchases over 20000.<br \/>\nprintf is sometimes used to redirect a variable or some unchanging input to a command. For example, suppose all you want to do is pipe a variable to a command. Instead of using printf, Bash provides a shortcut <<< redirection operator. <<< redirects a string into a command as if it were piped using printf.\n\nThe tr command can convert text to uppercase. This example shows an error message being converted to uppercase with both printf and <<<.\n\n $ printf \u201c%s\\n\u201d \u201c$ERRMSG\u201d | tr [:lower:] [:upper:]\nWARNING: THE FILES FROM THE OHIO OFFICE HAVEN\u2019T ARRIVED.\n$ tr [:lower:] [:upper:] <<< \u201c$ERRMSG\u201d\nWARNING: THE FILES FROM THE OHIO OFFICE HAVEN\u2019T ARRIVED.\nThe format codes include the following.\n%a\u2014Represent a floating-point number in hexadecimal format, using lowercase\nletters\n%A\u2014Represent a floating point number in hexadecimal format, using uppercase\nletters\n%b\u2014Expand backslash sequences\n%c\u2014Represent a single character\n%d\u2014Display a signed number\n%e\u2014Display a floating-point number, shown in exponential (also called \u201cscientific\u201d)\nnotation\n%f (or %F)\u2014Display a floating-point number without exponential notation\n%g\u2014(General) Let Bash choose %e or %f, depending on the value\n%i\u2014Same as %d\n%0\u2014Display an octal number\n%q\u2014Quote a string so it can be read properly by a shell script\n30 Chapter 3 Files, Users, and Shell Customization\n%s\u2014Display an unquoted string\n%u\u2014Display an unsigned number\n%x\u2014Display an unsigned hexadecimal number, using lowercase letters\n%X\u2014Display an unsigned hexadecimal number, using uppercase letters\n%%\u2014Display a percent sign\n\nIf a number is too large, Bash reports an out-of-range error.\n\n $ printf \u201c%d\\n\u201d 123456789123456789012\nbash: printf: warning: 123456789123456789012: Numerical result out of range\nFor compatibility with C\u2019s printf, Bash also recognizes the following flags, but treats\nthem the same as %d:\n%j\u2014A C intmax_t or uintmax_t integer\n%t\u2014A C ptrdiff_t integer\n%z\u2014A C size_t or ssize_t integer\nAlso for C compatibility, you can preface the format codes with a l or L to indicate a long number.\n\nThe %q format is important in shell script programming and it is discussed in the  quoting section, in the Chapter 5,\u201cVariables.\u201d\n\nTo create reports with neat columns, numbers can proceed many of the formatting codes to indicate the width of a column. For example, \u201c%10d\u201d prints a signed number in a column 10 characters wide.\n\n $ printf \u201c%10d\\n\u201d 11\n11\nLikewise, a negative number left-justifies the columns.\n\n$ printf \u201c%-10d %-10d\\n\u201d 11 12\n11 12\nA number with a decimal point represents a column width and a minimum number of digits (or decimal places with floating-point values). For example, \u201c%10.5f\u201d indicates a floating-point number in a 10-character column with a minimum of five decimal places.\n\n$ printf \u201c%10.5f\\n\u201d 17.2\n17.20000\nFinally, an apostrophe (\u2018)displays the number with thousands groupings based on the current country locale.\nThe \\n in the format string is an example of a backslash code for representing unprintable characters. \\n indicates a new line should be started. There are special backslash formatting codes for the representation of unprintable characters.\n\n\\b\u2014Backspace\n\\f\u2014Form feed (that is, eject a page on a printer)\n\\n\u2014Start a new line\n\\r\u2014Carriage return\n\\t\u2014Tab\n\\v\u2014Vertical tab\n\\\u2019\u2014Single quote character (for compatibility with C)\n\\\\\u2014Backslash\nn\u2014n is an octal number representing an 8-bit ASCII character\n\n$ printf \u201cTwo separate\\nlines\\n\u201d\nTwo separate\nLines\nAny 8-bit byte or ASCII character can be represented by or \\ and its octal value.\n\n$ printf \u201cASCII 65 (octal 101) is the character 101\\n\u201d\nASCII 65 (octal 101) is the character A\nprintf recognizes numbers beginning with a zero as octal notation, and numbers beginning with 0x as hexadecimal notation. As a result, printf can convert numbers between these different notations.\n\n $ printf \u201c%d\\n\u201d 010\n8\n$ printf \u201c%d\\n \u201c 0xF\n15\n$ printf \u201c0x%X\\n \u201c 15\n0xF\n$ printf \u201c0%o\\n \u201c 8\n010\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to use bash&#8217;s printf formatting and store the value into a variable, you can use bash&#8217;s print&#8217;s -v flag as follows:<\/p>\n<p>calc_elaspsed_time() { local SECONDS=$1 ((h=SECONDS\/3600)) ((m=SECONDS%3600\/60)) ((s=SECONDS%60)) printf -v ELAPSED_TIME &#8220;%02d:%02d:%02d&#8221; $h $m $s } <\/p>\n<p>printf Command The built-in printf (print formatted) command prints a message to the screen. You [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3371"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3371"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3371\/revisions"}],"predecessor-version":[{"id":3373,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3371\/revisions\/3373"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}