{"id":6,"date":"2006-06-26T22:03:18","date_gmt":"2006-02-07T22:07:48","guid":{"rendered":""},"modified":"2017-03-07T15:55:50","modified_gmt":"2017-03-07T15:55:50","slug":"requoting_in_bash","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/agriffis\/2006\/06\/26\/requoting_in_bash\/","title":{"rendered":"requoting in bash"},"content":{"rendered":"<p>While working with Uberlord on the Gentoo netscripts, I had a chance to review our requoting function.  Here it is:<\/p>\n<blockquote>\n<pre>function requote {\r\n        local q=\\'\r\n        set -- \"${@\/\/\\'\/$q\\'$q}\"        # quote inner instances of '\r\n        set -- \"${@\/#\/$q}\"              # add ' to start of each param\r\n        set -- \"${@\/%\/$q}\"              # add ' to end of each param\r\n        echo \"$*\"\r\n}<\/pre>\n<\/blockquote>\n<p>The purpose of this function is to make the arguments suitable for evaluation by the shell.  This happens whenever you need a single variable that will correctly evaluate to multiple arguments without distorting the original content.  In the case of passing the variable to an external program, you can&#8217;t even use bash arrays, so requoting is the only option.  Here&#8217;s a simple example:<\/p>\n<blockquote>\n<pre>connect=$(requote chat -v '' ATZ OK ATDT318714 CONNECT '' ogin: ppp word: '&lt;pa$$w0rd!&gt;')\r\npppd connect \"$connect\"<\/pre>\n<\/blockquote>\n<p>In this case it&#8217;s important that quoting is preserved so that the special characters in the password, including the angle brackets that could be misinterpreted as I\/O redirection, are passed to the chat program safely.<\/p>\n<p>Some time after writing this function, I learned about bash printf&#8217;s %q, which &#8220;means to quote the argument in a way that can be reused as shell input&#8221; (from bash built-in help).  It turns out it isn&#8217;t very easy to update our requote function to use it because, right or wrong, it drops empty arguments&#8230;<\/p>\n<blockquote>\n<pre>$ printf \"%q \" one '' '$&lt;$'; echo\r\none  \\$\\&lt;\\$ \r\n<\/pre>\n<\/blockquote>\n<p>This is the best I could come up with for now, which unfortunately needs a bash loop.  If somebody comes up with a better implementation using printf %q, I&#8217;d be interested in knowing it!<\/p>\n<blockquote>\n<pre>function requote {\r\n    declare arg\r\n    for arg; do \r\n        arg=$(printf '%q' \"$arg\")\r\n        printf '%s ' \"${arg:-''}\"\r\n    done\r\n}<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>While working with Uberlord on the Gentoo netscripts, I had a chance to review our requoting function. Here it is: function requote { local q=\\&#8217; set &#8212; &#8220;${@\/\/\\&#8217;\/$q\\&#8217;$q}&#8221; # quote inner instances of &#8216; set &#8212; &#8220;${@\/#\/$q}&#8221; # add &#8216; to start of each param set &#8212; &#8220;${@\/%\/$q}&#8221; # add &#8216; to end of each &hellip; <a href=\"https:\/\/blogs.gentoo.org\/agriffis\/2006\/06\/26\/requoting_in_bash\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">requoting in bash<\/span><\/a><\/p>\n","protected":false},"author":25,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,3],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/posts\/6"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":1,"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":11,"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/posts\/6\/revisions\/11"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/agriffis\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}