{"id":725,"date":"2023-03-01T12:38:21","date_gmt":"2023-03-01T20:38:21","guid":{"rendered":"https:\/\/porkrind.org\/missives\/?p=725"},"modified":"2023-03-01T12:38:21","modified_gmt":"2023-03-01T20:38:21","slug":"shell-printf-tricks","status":"publish","type":"post","link":"https:\/\/porkrind.org\/missives\/shell-printf-tricks\/","title":{"rendered":"Shell printf tricks"},"content":{"rendered":"<p>Do you know the <code>printf<\/code> trick in shell?<\/p>\n<p>Shell <code>printf<\/code> repeats its format pattern if you give it more arguments than the pattern has.<br \/>\nYou can use this for all kinds of tricks:<\/p>\n<pre><code class=\"language-bash\">$ printf \"\/some\/path\/%s \" file1 file2 file3 file4\n\/some\/path\/file1 \/some\/path\/file2 \/some\/path\/file3 \/some\/path\/file4 \n$ printf \"%s=%s\\n\" a 1 b 2 c 3 d 4\na=1\nb=2\nc=3\nd=4\n<\/code><\/pre>\n<p>It&#8217;s is really nice when you combine it with some other output:<\/p>\n<pre><code class=\"language-bash\">$ printf -- \"-p %s \" $(ps aux | grep fcgiwr[a]p | f 2)\n-p 10613 -p 10615 -p 10616 -p 10617 -p 10619 -p 10620 -p 10621 -p 10622 -p 10623\n<\/code><\/pre>\n<p>Say I wanted to pass all the pids of a program that forks a lot to <code>strace<\/code>. I could do:<\/p>\n<pre><code class=\"language-bash\">strace $(printf -- \"-p %s \" $(ps aux | grep fcgiwr[a]p | f 2))\n<\/code><\/pre>\n<p>An aside, you may have noticed that non-standard <code>f<\/code> command in the <code>ps<\/code> pipeline. I got that from <a href=\"https:\/\/perl.plover.com\/classes\/mybin\/samples\/slide010.html\">here<\/a> a long time ago (2008 according to the file&#8217;s timestamp) and it&#8217;s really fantastic\u2014perfect for all sorts of unixy things that output tables (<code>ps<\/code>, <code>ls -l<\/code>, etc).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you know the printf trick in shell? Shell printf repeats its format pattern if you give it more arguments than the pattern has. You can use this for all kinds of tricks: $ printf &#8220;\/some\/path\/%s &#8221; file1 file2 file3 file4 \/some\/path\/file1 \/some\/path\/file2 \/some\/path\/file3 \/some\/path\/file4 $ printf &#8220;%s=%s\\n&#8221; a 1 b 2 c 3 d &hellip; <a href=\"https:\/\/porkrind.org\/missives\/shell-printf-tricks\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Shell printf tricks<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-725","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/posts\/725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/comments?post=725"}],"version-history":[{"count":8,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/posts\/725\/revisions"}],"predecessor-version":[{"id":736,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/posts\/725\/revisions\/736"}],"wp:attachment":[{"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/media?parent=725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/categories?post=725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/porkrind.org\/missives\/wp-json\/wp\/v2\/tags?post=725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}