{"id":122,"date":"2020-05-11T23:36:40","date_gmt":"2020-05-11T22:36:40","guid":{"rendered":"https:\/\/blog.thomarite.uk\/?p=122"},"modified":"2020-05-11T23:43:19","modified_gmt":"2020-05-11T22:43:19","slug":"bash-shell-quoting","status":"publish","type":"post","link":"https:\/\/blog.thomarite.uk\/index.php\/2020\/05\/11\/bash-shell-quoting\/","title":{"rendered":"Bash: shell quoting"},"content":{"rendered":"\n<p>Another issue I had during the weekend that took me hours. Thanks that I have been reading a bit this <a href=\"https:\/\/smile.amazon.co.uk\/bash-Cookbook-Solutions-Examples-Cookbooks\/dp\/0596526784\/\">book<\/a> (1.6) and had some clues.<\/p>\n\n\n\n<p>I was trying to test a <a href=\"https:\/\/github.com\/etedor\/ceos-lab-box\">repo<\/a> to start an <a href=\"https:\/\/www.arista.com\/en\/\">Arista<\/a> lab using docker and I assumed that everything should work if I followed the instructions. My problem was the script trying to push some basic config to the switches.<\/p>\n\n\n\n<p>This is was the initial function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env bash\n...\nfunction fast_cli() {\n  params=\"${*:2}\"\n  commands=\"${params\/\/;\/\\\\\\n}\"\n  docker exec \"${1}\" bash -c \"echo -e ${commands} | FastCli -p15 -e\n}\n...<\/pre>\n\n\n\n<p>If you type that command in a bash shell directly is something like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ docker exec DOCKER_ID bash -c 'echo -e \"configure\\n hostname sp01\\n end\\n write\\n\" | FastCli -p15 -e'<\/pre>\n\n\n\n<p>As you can see that differs with what we have inside the bash script. So from the bash script we need to put between &#8216; the parameter for -c but inside the parameter we need to use &#8220;. So I had to make the change below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-  docker exec \"${1}\" bash -c \"echo -e ${commands} | FastCli -p15 -e\"\n+  # need to update this command as the quoting doesnt work in my bash\n+  docker exec \"${1}\" bash -c 'echo -e '\"'${commands}'\"' | FastCli -p15 -e'\n<\/pre>\n\n\n\n<p>The books says <em>Enclose a string in single quotes &#8216; unless it contains elements that you want the shell to interpolate<\/em>. So let&#8217;s divide the solution in parts so can be easier to digest (and remember for me in the future because this will bit me again for sure)<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>1st part: <em>&#8216;echo -e &#8216;<\/em><\/li><li>2nd part: <em>&#8220;<\/em><\/li><li>3rd part: <em>&#8216;${commands}&#8217;<\/em><\/li><li>4th part: <em>&#8220;<\/em><\/li><li>5th part: <em>&#8216; | FastCli -p15 -e&#8217;<\/em><\/li><\/ul>\n\n\n\n<p>The &#8221; need to be outside the &#8216; region because the commands need to be between &#8221; for the docker command. The 3rd part will expand the variable commands.<\/p>\n\n\n\n<p>I guess the author is using a different version of bash? This is mine<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ bash --version\nGNU bash, version 5.0.16(1)-release (x86_64-pc-linux-gnu)\nCopyright (C) 2019 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later <http:\/\/gnu.org\/licenses\/gpl.html>\n\nThis is free software; you are free to change and redistribute it.\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another issue I had during the weekend that took me hours. Thanks that I have been reading a bit this book (1.6) and had some clues. I was trying to test a repo to start an Arista lab using docker and I assumed that everything should work if I followed the instructions. My problem was &hellip; <a href=\"https:\/\/blog.thomarite.uk\/index.php\/2020\/05\/11\/bash-shell-quoting\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bash: shell quoting&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-unix"],"_links":{"self":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":4,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":128,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/122\/revisions\/128"}],"wp:attachment":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}