{"id":9014,"date":"2005-05-27T01:21:01","date_gmt":"2005-05-26T22:21:01","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=9014"},"modified":"2005-05-27T01:22:21","modified_gmt":"2005-05-26T22:22:21","slug":"handling-arguments-with-spaces-in-bash","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2005\/05\/27\/handling-arguments-with-spaces-in-bash\/","title":{"rendered":"Handling arguments with spaces in bash"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>Way to often I get it wrong, so I decided to right this down&#8230;<\/p>\n<p>When processing the list of arguments in your bash script, remember that often arguments such as file names contain spaces.  The wrong way to go about this is:<\/p>\n<pre>\r\n#!\/bin\/bash\r\nfor FILE in $*\r\ndo\r\n  echo \"$FILE\"\r\ndone\r\n<\/pre>\n<p>The right way to do it is:<\/p>\n<pre>\r\n#!\/bin\/bash\r\nfor FILE in \"$@\"\r\ndo\r\n  echo \"$FILE\"\r\ndone\r\n<\/pre>\n<p><a href=\"http:\/\/www.tldp.org\/LDP\/abs\/html\/\">Advanced Bash-Scripting Guide<\/a> explains the difference in &#8220;<a href=\"http:\/\/www.tldp.org\/LDP\/abs\/html\/internalvariables.html\">Internal Variables<\/a>&#8221; chapter.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>Way to often I get it wrong, so I decided to right this down&#8230; When processing the list of arguments in your bash script, remember that often arguments such as file names contain spaces. The wrong way to go about this is: #!\/bin\/bash for FILE in $* do echo &#8220;$FILE&#8221; done The right way to &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2005\/05\/27\/handling-arguments-with-spaces-in-bash\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Handling arguments with spaces in bash<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62],"tags":[1960],"keyring_services":[],"class_list":["post-9014","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","tag-command-line"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":16781,"url":"https:\/\/mamchenkov.net\/wordpress\/2012\/10\/02\/bash-directory-bookmarks\/","url_meta":{"origin":9014,"position":0},"title":"Bash directory bookmarks","author":"Leonid Mamchenkov","date":"October 2, 2012","format":false,"excerpt":"While reading through the comments to this Habrahabr article (in Russian), I came across an excellent tip for the directory bookmarks in bash shell. \u00a0Here's how to set it up. Firstly, add the following lines to your .bashrc or .bash_profile file: Secondly, if you are already using ~\/.bookmarks file for\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":42765,"url":"https:\/\/mamchenkov.net\/wordpress\/2019\/07\/02\/bash-trick-repeat-last-command-until-success\/","url_meta":{"origin":9014,"position":1},"title":"Bash trick: Repeat last command until success","author":"Leonid Mamchenkov","date":"July 2, 2019","format":false,"excerpt":"More and more often I come across a scenario where I need to repeat the shell command until it succeeds. Here are a couple of examples: Reboot a server. Try to remotely login to it via ssh. This fails until the server actually boots up. Keep trying until connected.Start an\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9038,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/06\/03\/recursively-adding-files-and-directories-in-gnu-arch\/","url_meta":{"origin":9014,"position":2},"title":"Recursively adding files and directories in Gnu Arch","author":"Leonid Mamchenkov","date":"June 3, 2005","format":false,"excerpt":"One of the little annoyances of Gnu Arch is that it does not add files and directories recursively. Not to worry, though. Here is a little shell script that can assist with that task: #!\/bin\/bash # Find all files and directories, skipping .\/{arch} for FILE in `find . -path '.\/{arch}'\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27447,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/22\/bulletproof-bash-stop-script-on-error\/","url_meta":{"origin":9014,"position":3},"title":"Bulletproof Bash : Stop script on error","author":"Leonid Mamchenkov","date":"March 22, 2017","format":false,"excerpt":"The other day I've been puzzled by the results of a cron job script. \u00a0The bash script in question was written in a hurry a while back, and I was under the assumption that if any of its steps fail, the whole script will fail. \u00a0I was wrong. \u00a0Some commands\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26383,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/08\/30\/troubleshooting-with-devtcp-and-devudp\/","url_meta":{"origin":9014,"position":4},"title":"Troubleshooting with \/dev\/tcp and \/dev\/udp","author":"Leonid Mamchenkov","date":"August 30, 2016","format":false,"excerpt":"Imagine you are on a freshly installed Linux machine with the minimal set of packages, and you need to test network connectivity. \u00a0You don't have netcat, telnet, and your other usual tools. \u00a0For the sake of the example, imagine that even curl and wget are missing. \u00a0What do you do?\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26923,"url":"https:\/\/mamchenkov.net\/wordpress\/2016\/11\/21\/shell-parameter-expansion-default-values-for-shell-script-parameters\/","url_meta":{"origin":9014,"position":5},"title":"Shell parameter expansion : default values for shell script parameters","author":"Leonid Mamchenkov","date":"November 21, 2016","format":false,"excerpt":"When writing shell scripts, it's often useful to accept some command line parameters. \u00a0It's even more useful to have some defaults for those parameters. \u00a0Until now I've been using if statements to check if the parameter was empty, and if it was, to set it to the default value. \u00a0Something\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/9014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=9014"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/9014\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=9014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=9014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=9014"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=9014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}