<story><title>Shell Style Guide</title><url>https://google.github.io/styleguide/shell.xml</url></story><parent_chain><item><author>bm1362</author><text>&lt;p&gt;&lt;pre&gt;&lt;code&gt;    for i in ‘ls &amp;#x2F;tmp&amp;#x2F;foo’; do echo $i | grep bar | cut -d’-‘ -f3,4; done
&lt;/code&gt;&lt;/pre&gt;
Is just so much easier to remember than Python’s OS library, right? I can intuitively string that together but can’t write python without looking up the docs and reading a paragraph about idiosyncrasies.</text></item><item><author>jerf</author><text>It&amp;#x27;s a 10-20 line Python script if you write it without any support code. If you&amp;#x27;re writing any amount of Python code that does shell things, you should either write or get an existing library.&lt;p&gt;Also, it really isn&amp;#x27;t 10-20 lines to spit out the contents of some files in Python:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &amp;gt;&amp;gt;&amp;gt; filelist = [&amp;quot;tmp.pl&amp;quot;, &amp;quot;tmp.go&amp;quot;, &amp;quot;Tmp.hs&amp;quot;]
    &amp;gt;&amp;gt;&amp;gt; for f in filelist:
    ...     print(open(f).read())
&lt;/code&gt;&lt;/pre&gt;
It&amp;#x27;s trivial to code golf that down to 1 line, but if we&amp;#x27;re writing in Python at any sort of scale, this is more realistic. Easy things are still generally easy. They just aren&amp;#x27;t optimized down to shell level in terms of keystroke count. They&amp;#x27;re often more optimized than bash in terms of conceptual complexity, though.&lt;p&gt;For instance, if one of my files has a space in it, shell becomes a conceptual minefield, requiring you to understand the half-a-dozen ways it might process strings and how that interacts with filenames and arguments passed to commands, whereas the Python just keeps doing what you probably meant, because it knows what&amp;#x27;s a string and what&amp;#x27;s something else. Shell makes a &lt;i&gt;lot&lt;/i&gt; of sacrifices for that concision, and there&amp;#x27;s a lot of &amp;quot;Well, this will &lt;i&gt;probably&lt;/i&gt; be OK on anything I run it on...&amp;quot;. I never put spaces in my file name, but use periods or underscores instead, precisely so I don&amp;#x27;t screw myself over with shell. I shouldn&amp;#x27;t have to do that. Personally I think the crossover point is in the high single digit number of bash lines. The only advantage bash still holds over Python at that point is large pipelines, and 80% of &lt;i&gt;those&lt;/i&gt; can be replaced by Python functions. For the remainder, use a pipelining library.</text></item><item><author>SOLAR_FIELDS</author><text>For basic file system operations it’s a necessity. Write 10-20 line python script or type a one line grep&amp;#x2F;awk&amp;#x2F;sed piipeline to spit out the contents of some files?</text></item><item><author>cellard00r</author><text>Often times it&amp;#x27;s easier to use binaries and manage their input&amp;#x2F;output through shell code.&lt;p&gt;Having a good understanding of shell code will also allow you to easily write slick one-liners that will save you lots of time.</text></item><item><author>tootie</author><text>I absolutely hate coding in bash or looking at bash or suddenly being in Windows and being up a tree because bash isn&amp;#x27;t supported well (it is now if you install WSL). I only use bash to set environment variables and maybe string together 2 or 3 build commands. If I need so much as an if statement, I&amp;#x27;m going to switch to a real programming language.</text></item></parent_chain><comment><author>jerf</author><text>I said Python wasn&amp;#x27;t going to be more concise. I said it has a variety of other useful properties. For instance, at least as I write this, you have &amp;quot;cut -d’-‘&amp;quot;; I assume you meant apostrophes (something getting too polite in a c&amp;amp;p, I assume) but the apostrophes are unnecessary, but you&amp;#x27;re so used to the compromises in shell I talked about you probably put those in there automatically. I&amp;#x27;m not criticizing that; it&amp;#x27;s a reasonable accommodation to shell&amp;#x27;s quirks. But it is a quirk.&lt;p&gt;Your code also breaks if the filenames have spaces:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    $ ls -1                                                                      
    bar-def-gh i-jkl-mno-p                                                                         
    bar-def-ghi-jkl-mno-p                                                                          
    $ for i in `ls`; do echo $i | grep bar | cut -d- -f3,4; done
    gh
    ghi-jkl
&lt;/code&gt;&lt;/pre&gt;
The natural Python code you&amp;#x27;d write to replace it will do what you expect and print the file with a space in it in the way you&amp;#x27;d expect. You can fix this in bash via &amp;quot;for i in [asterisk]&amp;quot; (or, in this case, &amp;quot;for i in [asterisk]bar[asterisk]&amp;quot;), but, well, remember when I said bash requires you to understand the half-a-dozen ways strings and filenames interact...? In Python, there&amp;#x27;s just the one way that strings work.&lt;p&gt;(HN is forcing me to say [asterisk]. AFAIK there&amp;#x27;s still no escaping for them.)&lt;p&gt;You lucked out a bit here too; I don&amp;#x27;t think I can get echo to do anything very interesting based on file names. Had you interpolated that somewhere more interesting I&amp;#x27;d give you some security issues too, if anybody not trusted can create files, which, again, the &lt;i&gt;naive&lt;/i&gt; Python code would be much more robust to, because in Python, you have to explicitly ask to bring a string up to an execution context or a function parameter context, unlike shell&amp;#x27;s complicated rules for interleaving them. You pay a steep price for the convenience.&lt;p&gt;Of course, this is a matter of scale. I use that sort of pipeline interactively every day. I just start shuddering when it goes in a file for a script, and shudder more if it goes into source control.</text></comment>
<story><title>Shell Style Guide</title><url>https://google.github.io/styleguide/shell.xml</url></story><parent_chain><item><author>bm1362</author><text>&lt;p&gt;&lt;pre&gt;&lt;code&gt;    for i in ‘ls &amp;#x2F;tmp&amp;#x2F;foo’; do echo $i | grep bar | cut -d’-‘ -f3,4; done
&lt;/code&gt;&lt;/pre&gt;
Is just so much easier to remember than Python’s OS library, right? I can intuitively string that together but can’t write python without looking up the docs and reading a paragraph about idiosyncrasies.</text></item><item><author>jerf</author><text>It&amp;#x27;s a 10-20 line Python script if you write it without any support code. If you&amp;#x27;re writing any amount of Python code that does shell things, you should either write or get an existing library.&lt;p&gt;Also, it really isn&amp;#x27;t 10-20 lines to spit out the contents of some files in Python:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &amp;gt;&amp;gt;&amp;gt; filelist = [&amp;quot;tmp.pl&amp;quot;, &amp;quot;tmp.go&amp;quot;, &amp;quot;Tmp.hs&amp;quot;]
    &amp;gt;&amp;gt;&amp;gt; for f in filelist:
    ...     print(open(f).read())
&lt;/code&gt;&lt;/pre&gt;
It&amp;#x27;s trivial to code golf that down to 1 line, but if we&amp;#x27;re writing in Python at any sort of scale, this is more realistic. Easy things are still generally easy. They just aren&amp;#x27;t optimized down to shell level in terms of keystroke count. They&amp;#x27;re often more optimized than bash in terms of conceptual complexity, though.&lt;p&gt;For instance, if one of my files has a space in it, shell becomes a conceptual minefield, requiring you to understand the half-a-dozen ways it might process strings and how that interacts with filenames and arguments passed to commands, whereas the Python just keeps doing what you probably meant, because it knows what&amp;#x27;s a string and what&amp;#x27;s something else. Shell makes a &lt;i&gt;lot&lt;/i&gt; of sacrifices for that concision, and there&amp;#x27;s a lot of &amp;quot;Well, this will &lt;i&gt;probably&lt;/i&gt; be OK on anything I run it on...&amp;quot;. I never put spaces in my file name, but use periods or underscores instead, precisely so I don&amp;#x27;t screw myself over with shell. I shouldn&amp;#x27;t have to do that. Personally I think the crossover point is in the high single digit number of bash lines. The only advantage bash still holds over Python at that point is large pipelines, and 80% of &lt;i&gt;those&lt;/i&gt; can be replaced by Python functions. For the remainder, use a pipelining library.</text></item><item><author>SOLAR_FIELDS</author><text>For basic file system operations it’s a necessity. Write 10-20 line python script or type a one line grep&amp;#x2F;awk&amp;#x2F;sed piipeline to spit out the contents of some files?</text></item><item><author>cellard00r</author><text>Often times it&amp;#x27;s easier to use binaries and manage their input&amp;#x2F;output through shell code.&lt;p&gt;Having a good understanding of shell code will also allow you to easily write slick one-liners that will save you lots of time.</text></item><item><author>tootie</author><text>I absolutely hate coding in bash or looking at bash or suddenly being in Windows and being up a tree because bash isn&amp;#x27;t supported well (it is now if you install WSL). I only use bash to set environment variables and maybe string together 2 or 3 build commands. If I need so much as an if statement, I&amp;#x27;m going to switch to a real programming language.</text></item></parent_chain><comment><author>plorkyeran</author><text>This is going to come down to what you&amp;#x27;re most familiar with. I personally can never remember what the arguments to `cut` are due to rarely using it, while I&amp;#x27;d be able to write the equivalent python from memory.</text></comment>