{"id":15894,"date":"2019-06-13T16:15:33","date_gmt":"2019-06-13T16:15:33","guid":{"rendered":"https:\/\/www.bartbusschots.ie\/s\/?p=15894"},"modified":"2019-06-17T14:28:50","modified_gmt":"2019-06-17T14:28:50","slug":"bash-to-zsh-allowing-comments-in-interactive-shells","status":"publish","type":"post","link":"https:\/\/www.bartbusschots.ie\/s\/2019\/06\/13\/bash-to-zsh-allowing-comments-in-interactive-shells\/","title":{"rendered":"Bash to Zsh: Allowing Comments in Interactive Shells"},"content":{"rendered":"<div class=\"pps-series-post-details pps-series-post-details-variant-classic pps-series-post-details-18702\" data-series-id=\"581\"><div class=\"pps-series-meta-content\"><div class=\"pps-series-meta-text\">This entry is part 5 of 6 in the series <a href=\"https:\/\/www.bartbusschots.ie\/s\/series\/bash-to-zsh\/\">Bash to Zsh<\/a><\/div><\/div><\/div><p>As I continue my move from Bash to Zsh at Apple&#8217;s strong suggestion I continue to bump into little differences that cause me minor problems. Today it was the fact that while Bash treats comments as comments even when they&#8217;re entered in an interactive shell, Zsh does not, at least not by default on MacOS.<\/p>\n<p><strong>TL;DR<\/strong> &#8211; <code>setopt INTERACTIVE_COMMENTS<\/code><\/p>\n<p><!--more--><\/p>\n<p>I have commands saved as Evernote notes and\/or TextExpander snippets that contain comments for context or explanation. This is not a real example, but it illustrates the point.<\/p>\n<p>The curl command to get a single-line version of my local weather from the CLI is:<\/p>\n<pre class=\"lang:sh decode:true \" >\r\ncurl 'http:\/\/wttr.in\/Maynooth?format=3&amp;m'\r\n<\/pre>\n<p>That&#8217;s pretty cryptic! Sure, it&#8217;s obvious how to specify the city, but what does <code>format=3<\/code> mean? And while we&#8217;re at it, why does the URL end in <code>&m<\/code>?<\/p>\n<p>There wouldn&#8217;t be much point in saving a snippet like that in TextExpander or Evernote without an explanation for those parameters. That&#8217;s why I&#8217;d save that command like so:<\/p>\n<pre class=\"lang:sh decode:true \" >\r\ncurl 'http:\/\/wttr.in\/Maynooth?format=3&amp;m' # format 3 for single line &amp; m for metric\r\n<\/pre>\n<p>I can copy\/paste or expand that command into Bash and it will execute perfectly and dutifully ignore the comment. When I tried a similar command in Zsh this morning things didn&#8217;t go so well! Instead of ignoring my comment Zsh tried to interpret it as a command, something it failed miserably at!<\/p>\n<p>When this happened my first thought was that Zsh used a different syntax for comments, but that didn&#8217;t seem likely since I&#8217;ve read over and over again that most Bash shell scripts work just fine in Zsh. That seems impossible if the comment syntax is different.<\/p>\n<p>What&#8217;s really going on here is much more subtle \u00e2\u20ac\u201d when executing shell command non-interactively (i.e. while executing a script) Zsh does ignore comments, but it doesn&#8217;t ignore them when you run Zsh as an interactive shell. I honestly don&#8217;t understand why anyone would think that was a good idea! I should be able to copy-and-paste a command from a Zsh shell script into a Zsh shell and have it behave the same way, but I can&#8217;t if the line from the script contains a comment. Sorry, but the only word I can think of to describe that is &#8216;dumb&#8217;!<\/p>\n<p>Thankfully there is a fix \u00e2\u20ac\u201d Zsh proves an option named <code>INTERACTIVE_COMMENTS<\/code> which instructs the shell to ignore comments in interactive shells. This option can be enabled with the command <code>setopt INTERACTIVE_COMMENTS<\/code>.<\/p>\n<p>To fix this issue in all your interactive Zsh shells, add lines like the following to your <code>~\/.zshrc<\/code> file:<\/p>\n<pre class=\"lang:zsh decode:true \" >\r\n# allow comments in interactive shells (like Bash does)\r\nsetopt INTERACTIVE_COMMENTS\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"pps-series-post-details pps-series-post-details-variant-classic pps-series-post-details-18702 pps-series-meta-excerpt\" data-series-id=\"581\"><div class=\"pps-series-meta-content\"><div class=\"pps-series-meta-text\">This entry is part 5 of 6 in the series <a href=\"https:\/\/www.bartbusschots.ie\/s\/series\/bash-to-zsh\/\">Bash to Zsh<\/a><\/div><\/div><\/div><p>As I continue my move from Bash to Zsh at Apple&#8217;s strong suggestion I continue to bump into little differences that cause me minor problems. Today it was the fact that while Bash treats comments as comments even when they&#8217;re entered in an interactive shell, Zsh does not, at least not by default on MacOS. [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[12,446],"tags":[414,406,458,580],"series":[581],"class_list":["post-15894","post","type-post","status-publish","format-standard","hentry","category-computers-tech","category-sysadmin","tag-bash","tag-commandline","tag-tutorial","tag-zsh","series-bash-to-zsh"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7t9xK-48m","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/15894","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/comments?post=15894"}],"version-history":[{"count":1,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/15894\/revisions"}],"predecessor-version":[{"id":15895,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/15894\/revisions\/15895"}],"wp:attachment":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/media?parent=15894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/categories?post=15894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/tags?post=15894"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/series?post=15894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}