{"id":1694,"date":"2015-05-23T09:47:02","date_gmt":"2015-05-23T16:47:02","guid":{"rendered":"http:\/\/joelinoff.com\/blog\/?p=1694"},"modified":"2015-05-23T09:48:11","modified_gmt":"2015-05-23T16:48:11","slug":"bash-function-that-accepts-white-space-arguments","status":"publish","type":"post","link":"https:\/\/joelinoff.com\/blog\/?p=1694","title":{"rendered":"Bash function that accepts white space arguments"},"content":{"rendered":"<p>This example shows how to create a bash function that will accept white space arguments. This doesn&#8217;t come up often but when it does I have to re-discover how to do it. This little reminder will make that rediscovery process unnecessary.<br \/>\n<!--more--><\/p>\n<pre class=\"theme:vs2012-black font-size:12 line-height:17 lang:bash decode:true\">\r\n# Run a command.\r\n# Decorate it with prefix and suffix information.\r\n# Handle arguments with white space.\r\n# Usage:\r\n#   runcmd git --nopager diff -stat\r\n#   runcmd git commit -a -m 'this is my commit message' file.ext\r\nfunction runcmd() {\r\n  # Build the Cmd string from the input.\r\n  # Quote arguments that have embedded white space.\r\n  local Cmd=''\r\n  for Arg in \"$@\" ; do\r\n    if echo \"$Arg\" | grep ' ' 1>\/dev\/null ; then\r\n      Cmd=\"$Cmd '$Arg'\"\r\n    else\r\n      Cmd=\"$Cmd $Arg\"\r\n    fi\r\n  done\r\n  Cmd=${Cmd:1}  # strip leading space\r\n\r\n  # Prefix.\r\n  echo ''\r\n  echo '# ================================================================'\r\n  echo \"# Cmd: $Cmd\"\r\n  echo \"# Pwd: $(pwd)\"\r\n  echo '# ================================================================'\r\n\r\n  # Execute the command.\r\n  eval time $Cmd\r\n  local Status=$?\r\n\r\n  # Suffix.\r\n  echo \"# Status: $Status\"\r\n  if (( $Status )) ; then\r\n    exit $Status\r\n  fi\r\n  return 0  # just in case the caller is checking\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example shows how to create a bash function that will accept white space arguments. This doesn&#8217;t come up often but when it does I have to re-discover how to do it. This little reminder will make that rediscovery process unnecessary.<\/p>\n","protected":false},"author":1,"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},"categories":[15,5,16],"tags":[],"_links":{"self":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1694"}],"collection":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1694"}],"version-history":[{"count":11,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1694\/revisions"}],"predecessor-version":[{"id":1705,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1694\/revisions\/1705"}],"wp:attachment":[{"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joelinoff.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}