{"id":403,"date":"2026-06-21T16:18:00","date_gmt":"2026-06-21T20:18:00","guid":{"rendered":"http:\/\/myblog.vindaloo.com\/?p=403"},"modified":"2026-06-21T16:18:00","modified_gmt":"2026-06-21T20:18:00","slug":"using-arrays-in-the-shell","status":"publish","type":"post","link":"http:\/\/myblog.vindaloo.com\/?p=403","title":{"rendered":"Using arrays in the shell"},"content":{"rendered":"\n<p>This started as a post about shell arrays as means of maintaining filenames with spaces in them.  Sadly, it fails at that. Still, compare two shell constructs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>$ egrep -li 'icon' $(find ~\/src -type f -name '*.py' | egrep -v venv)<\/code><\/code><\/pre>\n\n\n\n<p>Against:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ set -A python_files $(find ~\/src -type f -name '*.py' | egrep -v venv)\n$ for f in ${python_files&#91;@]}; do egrep -li icon \"${f}\"; done<\/code><\/pre>\n\n\n\n<p>Note well that the second construct preserves whitespacing in filenames even if one where foolish enough to create a python program with whitespace in it&#8217;s filename.<\/p>\n\n\n\n<p>I find myself doing the former a lot and there are a few places in my source code tree where there are embedded spaces in a directory. It&#8217;s probably time to retrain my finger memory to use the latter construct. This post comes because I find myself revisiting things occasionally. When people start with Unix, a common question is what&#8217;s the difference between <code>$*<\/code>, <code>$@<\/code>, <code>\"$*\"<\/code>, and <code>\"$@\"<\/code>? This isn&#8217;t original work but here&#8217;s a shell script that answers the question.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat foo.sh\n#! \/bin\/bash\n\necho \"## \"'$*' -- expands elements\nfor arg in $*; do\n    echo \"\\\"${arg}\\\"\"\ndone\necho \n\necho \"## \"'$@' -- expands elements\nfor arg in $@; do\n    echo \"\\\"${arg}\\\"\"\ndone\necho \n\necho \"## \"'\"$*\"' -- all elements\nfor arg in \"$*\"; do\n    echo \"\\\"${arg}\\\"\"\ndone\necho \n\necho \"## \"'\"$@\"'-- expands elements maintaining whitespace\nfor arg in \"$@\"; do\n    echo \"\\\"${arg}\\\"\"\ndone\necho \n\nexit 0\n<\/code><\/pre>\n\n\n\n<p>here&#8217;s what you get when you run the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ \/bin\/bash foo.sh one two 'three four' five\n## $* -- expands elements\n\"one\"\n\"two\"\n\"three\"\n\"four\"\n\"five\"\n\n## $@ -- expands elements\n\"one\"\n\"two\"\n\"three\"\n\"four\"\n\"five\"\n\n## \"$*\" -- all elements\n\"one two three four five\"\n\n## \"$@\"-- expands elements maintaining whitespace\n\"one\"\n\"two\"\n\"three four\"\n\"five\"\n\n$ <\/code><\/pre>\n\n\n\n<p>With arrays things are slightly different:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#! \/bin\/bash\n\n## If Korn Shell -- set -A list \"$@\" also works\n\nlist=( \"$@\" )\n\necho \"length: ........... ${#list&#91;@]}\"\necho \"all elements: ..... ${list&#91;@]}\"\necho\n\nfor arg in \"${list&#91;@]}\"; do\n    echo \"\\\"${arg}\\\"\"\ndone\necho\n\nexit 0\n<\/code><\/pre>\n\n\n\n<p>And the output when you run it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ \/bin\/ksh foo.sh one two 'three four' five\nlength: ........... 4\nall elements: ..... one two three four five\n\n\"one\"\n\"two\"\n\"three four\"\n\"five\"\n\n$ \/bin\/bash foo.sh one two 'three four' five\nlength: ........... 4\nall elements: ..... one two three four five\n\n\"one\"\n\"two\"\n\"three four\"\n\"five\"\n\n(venv) $ output<\/code><\/pre>\n\n\n\n<p>As above, sadly once you pipe the output of this into another program, in this case <code>egrep -v ...<\/code> to throw away the python that was pulled into your virtual environment, the spacis seems to get lost. Maybe some python can fix this?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This started as a post about shell arrays as means of maintaining filenames with spaces in them. Sadly, it fails at that. Still, compare two shell constructs: Against: Note well that the second construct preserves whitespacing in filenames even if one where foolish enough to create a python program with whitespace in it&#8217;s filename. I &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/myblog.vindaloo.com\/?p=403\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using arrays in the shell&#8221;<\/span><\/a><\/p>\n","protected":false},"author":54,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-403","post","type-post","status-publish","format-standard","hentry","category-unix"],"_links":{"self":[{"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=\/wp\/v2\/posts\/403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403"}],"version-history":[{"count":1,"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions"}],"predecessor-version":[{"id":404,"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions\/404"}],"wp:attachment":[{"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/myblog.vindaloo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}