{"id":27,"date":"2011-07-12T12:07:12","date_gmt":"2011-07-12T12:07:12","guid":{"rendered":"http:\/\/www.vidarholen.net\/contents\/blog\/?p=27"},"modified":"2015-04-06T17:25:45","modified_gmt":"2015-04-06T17:25:45","slug":"why-bash-is-like-that-order-of-expansions","status":"publish","type":"post","link":"https:\/\/www.vidarholen.net\/contents\/blog\/?p=27","title":{"rendered":"Why Bash is like that: Order of expansion"},"content":{"rendered":"<p>Bash can seem pretty random and weird at times, but most of what people see as quirks have very logical (if not very good) explanations behind them. This series of posts looks at some of them.<\/p>\n<pre># Why can't you use variables in {}?\r\nfor i in {0..$n}; do ..<\/pre>\n<p><strong>Short answer: {..} is expanded before $n<\/strong><\/p>\n<p>Shell execution is based on successive expansions. {..} is evaluated early, before variable expansion, and thus you can&#8217;t use variables in them.<\/p>\n<p>This also implies that you can use {..} in variable names, like <code>a1=foo; a2=bar; echo $a{1,2}<\/code>.<\/p>\n<p>Instead, use <code>for ((i=0; i&lt;n; i++)); do ...<\/code>.<\/p>\n<pre># Why aren't any of the Linux rename tools like Windows 'ren'?\r\ntouch foo0001.jpg foo0002.jpg\r\nren foo*.jpg bar*.jpg        # Windows\r\nrename foo bar foo*.jpg      # Coreutils rename\r\nrename 's\/foo\/bar\/' foo*.jpg # Perl (debian) rename<\/pre>\n<p><strong>Short answer: globs are expanded before the command sees them<\/strong><\/p>\n<p>Bash expands globs before running the command. This means that running <code>rename foo*.jpg bar*.jpg<\/code> is exactly the same as running <code>rename foo0000.jpg foo0001.jpg ...<\/code>. Since rename can&#8217;t know what pattern was originally used, it has to use an alternative syntax.<\/p>\n<p>Of course, you could write a rename where you quote the globs, like <code>rename \"foo*.jpg\" \"bar*.jpg\"<\/code>, but that&#8217;s not simpler than the coreutils version. It just adds complexity, edge cases and general confusion.<\/p>\n<p>There have been proposals for environment variables to set so that commands can see the shell arguments with globs intact, but that has its own problems so they weren&#8217;t widely used.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bash can seem pretty random and weird at times, but most of what people see as quirks have very logical (if not very good) explanations behind them. This series of posts looks at some of them. # Why can&#8217;t you use variables in {}? for i in {0..$n}; do .. Short answer: {..} is expanded &hellip; <a href=\"https:\/\/www.vidarholen.net\/contents\/blog\/?p=27\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Why Bash is like that: Order of expansion&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[6],"tags":[11,21,39],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-basic-linux","tag-bash","tag-shell-script","tag-why-bash-is-like-that"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27"}],"version-history":[{"count":1,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":476,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions\/476"}],"wp:attachment":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}