{"id":4063,"date":"2016-03-05T13:31:27","date_gmt":"2016-03-05T13:31:27","guid":{"rendered":"https:\/\/really.zonky.org\/?p=4063"},"modified":"2016-03-06T12:36:22","modified_gmt":"2016-03-06T12:36:22","slug":"shell-efficiency-and-clarity","status":"publish","type":"post","link":"https:\/\/really.zonky.org\/?p=4063","title":{"rendered":"Shell: Efficiency and Clarity"},"content":{"rendered":"<p>Just to amuse myself, I&#8217;ve been re-reading and re-learning the Unix shell&#8217;s <em>${} <\/em>details<i>,\u00a0<\/i>and it occurred to me that whilst these were all very well and cute, they very easily lead to impenetrable code. But they are more\u00a0<em>efficient<\/em>.<\/p>\n<p>Take the following two ways of getting the current date :-<\/p>\n<pre>\u2713 mike@pica\u00bb <em>print -P \"%D\"<\/em> \r\n16-03-05\r\n\u2713 mike@pica\u00bb <em>echo $(date)<\/em> \r\nSat 5 Mar 13:14:38 GMT 2016\r\n<\/pre>\n<p>It&#8217;s not exactly helpful that they return the date\/time in different formats. But glossing over that for the moment, which one is clearer? That is right &#8211; the second one clearly says that it is going to &#8220;echo&#8221; the date. Even if this usage is particularly stupid (as\u00a0<em>date<\/em> will echo the date all by itself), the second wins as far as clarity goes.<\/p>\n<p>However\u00a0it is also less efficient &#8211; rather than get the date and show it to the terminal, the shell invokes a sub-process to display the date, captures it and then uses it to show to the terminal. In the old days when terminals consisted of printing mechanisms that actually hit a template of a letter against an inked up ribbon against a roll of paper and hoped that the result was readable, this inefficiency could result in very slow code.<\/p>\n<p>But today this level of inefficiency should not make that much difference, and if it does, then why are you writing code in the shell? There are far better languages out there.<\/p>\n<p>In addition, there is a bit of a gotcha with the\u00a0<em>print -P &#8220;%D&#8221;<\/em> option \u2026 it only works if you happen to be using\u00a0<em>zsh<\/em> :-<\/p>\n<pre>\u2713 mike@pica\u00bb <em>print -P \"%D\"<\/em>\r\n16-03-05\r\n\u2713 mike@pica\u00bb <em>\/bin\/sh<\/em>\r\n$ <em>print -P \"%D\"<\/em>\r\nfile: option requires an argument -- 'P'\r\nUsage: file [-bcEhikLlNnprsvz0] [--apple] [--mime-encoding] [--mime-type]\r\n            [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...\r\n       file -C [-m magicfiles]\r\n       file [--help]\r\nWarning: unknown mime-type for \"-P\" -- using \"application\/octet-stream\"\r\nError: no such file \"-P\"\r\nError: no such file \"%D\"\r\n$ \r\n\u2717 mike@pica\u00bb <em>\/bin\/ksh<\/em>\r\n$ <em>print -P \"%D\"<\/em>\r\n%D\r\n$ \r\n\u2713 mike@pica\u00bb <em>\/bin\/bash<\/em>\r\nmike@pica:~\/.lyx$ <em>print -P \"%D\"<\/em>\r\nfile: option requires an argument -- 'P'\r\nUsage: file [-bcEhikLlNnprsvz0] [--apple] [--mime-encoding] [--mime-type]\r\n            [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...\r\n       file -C [-m magicfiles]\r\n       file [--help]\r\nWarning: unknown mime-type for \"-P\" -- using \"application\/octet-stream\"\r\nError: no such file \"-P\"\r\nError: no such file \"%D\"\r\nmike@pica:~\/.lyx$ exit\r\n\r\n<\/pre>\n<p>Confusing is it not?<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3856\" src=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt.png?resize=695%2C463&#038;ssl=1\" alt=\"damascus-unix-prompt\" width=\"695\" height=\"463\" srcset=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt.png?w=792&amp;ssl=1 792w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/damascus-unix-prompt.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/p>\n<p>Of course if the shell would intercept common usages such as\u00a0<em>$(date)<\/em> and optimise them, that would be perfectly reasonable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just to amuse myself, I&#8217;ve been re-reading and re-learning the Unix shell&#8217;s ${} details,\u00a0and it occurred to me that whilst these were all very well and cute, they very easily lead to impenetrable code. But they are more\u00a0efficient. Take the following two ways of getting the current date :- \u2713 mike@pica\u00bb print -P &#8220;%D&#8221; 16-03-05 <a href='https:\/\/really.zonky.org\/?p=4063' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_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},"jetpack_post_was_ever_published":false,"_share_on_mastodon":"0"},"categories":[4,209],"tags":[1386],"class_list":["post-4063","post","type-post","status-publish","format-standard","hentry","category-it","category-linux-it","tag-shell","category-4-id","category-209-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"share_on_mastodon":{"url":"","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1f2KI-13x","_links":{"self":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/4063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4063"}],"version-history":[{"count":4,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/4063\/revisions"}],"predecessor-version":[{"id":4069,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/4063\/revisions\/4069"}],"wp:attachment":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}