Live data from Hacker News

Shell Style Guide

google.github.io

71–80 of 336 posts

Re: Shell Style Guide

#71
post #17
post #8

Earlier quoted context omitted.

Sure, but not as terrifying as people rewriting shell one-liners in python or (gasp) java.

Then a lot of people don’t understand the limits of what they’re doing. For instance piping though commands works fine until one character sequence is interpreted as EOF. The fun part is it will work most of the time, and when it fails nobody will understand why (“we didn’t touch anything”), and rewriting the thing will be a political nightmare (“it was working before and was only 3 lines, why you need so much time t…

one character sequence is interpreted as EOF

I know this is just an example, but how would that happen?

Re: Shell Style Guide

#72
post #64

Earlier quoted context omitted.

Google also makes it difficult to compile Chromium on platforms where /usr/bin/python is python 3, because their shebangs are: #!/usr/bin/env python Rather than the more portable: #!/usr/bin/env python3 Notably this causes problems for people on Arch Linux, and in the near future, Fedora. This post is presented as a style guide that others should adopt. Google may have bash ubiquiotously available internally, but the…

If you find yourself compiling Google-stuff a lot on Arch, you might find this useful: https://github.com/mortie/nixConf/blob/master/bin/py2 I wrote that script exactly because of the issue you described, and I got tired of changing what /usr/bin/python is symlinked to constantly. With that script, I just run `py2 gn gen out/Debug` or whatever, and my /usr/bin/python stays intact so everything else which expects pyth…

Clearly the WONTFIX was wrongly issued. The issue should be re-opened.

Re: Shell Style Guide

#73
post #44

>Bash is the only shell scripting language permitted for executables. Whelp, wrong right off the bat. I'm gonna get down my my knees here and beg everyone reading: use POSIX shell. Do not write scripts with bash. Do not write scripts with zsh. Do not write scripts with fish. Use POSIX shell. sh has a really bad interactive mode (so does bash), so I'm not gonna give anyone a hard time for using another shell as their…

> >Bash is the only shell scripting language permitted for executables. > Whelp, wrong right off the bat. It really is a Google guide, tailored to Google engineers. If they know for sure that bash is available on any *n?x machine, then why not? It's just a local policy. And if it's that much of a problem, the other places where you get bash instead of POSIX sh from Google (chromium?) the license clearly allows you to…

>It really is a Google guide, tailored to Google engineers.

And posted in a public forum, which is why I made sure to protest its adoption by the broader public.

Re: Shell Style Guide

#74

Earlier quoted context omitted.

> Do not write scripts with bash. It's a bit too late for that. Bash is pretty much the de facto default shell for linux. Telling people not to use bash scripts is like telling web devs not to use javascript or OS devs not to use C. It may change in the future, but bash is so entrenched, it's going to take an extraordinary use case for people to move from bash.

>Telling people not to use bash scripts is like telling web devs not to use javascript or OS devs not to use C. Wow, this comparison is totally whack. Javascript and C are both standardized and the former is the only option when using a web browser. It's never too late. We managed to get people off of ANSI C and there's a similar amount of difference between ANSI* C and C89 as there is between bash and sh. * correcti…

> Wow, this comparison is totally whack

Did you really say whack?

> Javascript and C are both standardized and the former is the only option when using a web browser.

Javascript isn't the only option. It is the most popular by far and hence the "de facto" language of the web. And javascript certainly isn't "standardized" in the sense you are writing. Do you know anything about web development, browser implementation of javascript and javascript itself? Javascript is no more "standardized" than SQL is standardized. Every RDBMs implements their own flavor of SQL just like browsers do with javascript.

>It's never too late.

Did I say it was too late? Of course I didn't. Maybe if you took a step back from your fanboyism and read...

"It may change in the future, but bash is so entrenched, it's going to take an extraordinary use case for people to move from bash."

> We managed to get people off of ANSI C and there's a similar amount of difference between ANSI C and C89 as there is between bash and sh.

ANSI C and C89 are the same thing. ANSI C is just another name for C89... Sigh...

Re: Shell Style Guide

#75

Earlier quoted context omitted.

Often times it's easier to use binaries and manage their input/output through shell code. Having a good understanding of shell code will also allow you to easily write slick one-liners that will save you lots of time.

For basic file system operations it’s a necessity. Write 10-20 line python script or type a one line grep/awk/sed piipeline to spit out the contents of some files?

It's a 10-20 line Python script if you write it without any support code. If you're writing any amount of Python code that does shell things, you should either write or get an existing library.

Also, it really isn't 10-20 lines to spit out the contents of some files in Python:

    >>> filelist = ["tmp.pl", "tmp.go", "Tmp.hs"]
    >>> for f in filelist:
    ...     print(open(f).read())
It's trivial to code golf that down to 1 line, but if we're writing in Python at any sort of scale, this is more realistic. Easy things are still generally easy. They just aren't optimized down to shell level in terms of keystroke count. They're often more optimized than bash in terms of conceptual complexity, though.

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's a string and what's something else. Shell makes a lot of sacrifices for that concision, and there's a lot of "Well, this will probably be OK on anything I run it on...". I never put spaces in my file name, but use periods or underscores instead, precisely so I don't screw myself over with shell. I shouldn'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 those can be replaced by Python functions. For the remainder, use a pipelining library.

Re: Shell Style Guide

#76

Earlier quoted context omitted.

Of course portable shell scripts can define functions. What are you talking about? Shell functions are defined in POSIX. Maybe you should consult the standard before you proceed: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3...

I'm technically wrong per POSIX but practically right per your principle motivation of portability: https://www.gnu.org/software/autoconf/manual/autoconf-2.66/h... > Unfortunately, even in 2008, where shells without any function support are far and few between, there are pitfalls to avoid when making use of them. Also, finding a Bourne shell that accepts shell functions is not trivial, even though there is almost alw…

This document is 10 years old, and even then it acknowledged that finding shells without function support is a tough ask. You're not practically right, either.

When a system does not (correctly) implement portable standards, it is a bug in the system and software should not be corrected to accomodate for it. autotools disagrees with me on this point.

Re: Shell Style Guide

#77
post #56

Earlier quoted context omitted.

All Google style guides use two spaces.

not the python one! https://google.github.io/styleguide/pyguide.html anything other than 4 spaces for python look out of place for me.

This is either outdated or only for the public version of the guide: the internal guide, and all our python code, has 2-spaces indents.

Re: Shell Style Guide

#78

Earlier quoted context omitted.

>Telling people not to use bash scripts is like telling web devs not to use javascript or OS devs not to use C. Wow, this comparison is totally whack. Javascript and C are both standardized and the former is the only option when using a web browser. It's never too late. We managed to get people off of ANSI C and there's a similar amount of difference between ANSI* C and C89 as there is between bash and sh. * correcti…

> Wow, this comparison is totally whack Did you really say whack? > Javascript and C are both standardized and the former is the only option when using a web browser. Javascript isn't the only option. It is the most popular by far and hence the "de facto" language of the web. And javascript certainly isn't "standardized" in the sense you are writing. Do you know anything about web development, browser implementation…

>Javascript isn't the only option. It is the most popular by far and hence the "de facto" language of the web. And javascript certainly isn't "standardized" in the sense you are writing. Do you know anything about web development, browser implementation of javascript and javascript itself? Javascript is no more "standardized" than SQL is standardized. Every RDBMs implements their own flavor of SQL just like browsers do with javascript.

This is nonsense. I am very familiar with web browsers. Javascript is standardized as ECMAScript:

https://www.ecma-international.org/publications/standards/Ec...

The "standard library" browsers implement is also standardized:

https://www.w3.org/standards/

You're off your rocker.

>Did I say it was too late? Of course I didn't. Maybe if you took a step back from your fanboyism and read...

Here is a direct quote from your comment:

>It's a bit too late for that. Bash is pretty much the de facto default shell for linux.

>ANSI C and C89 are the same thing. ANSI C is just another name for C89... Sigh...

Derp, I meant K&R C or pre-ANSI C. My mistake.

Re: Shell Style Guide

#79
post #43

>Bash is the only shell scripting language permitted for executables. Whelp, wrong right off the bat. I'm gonna get down my my knees here and beg everyone reading: use POSIX shell. Do not write scripts with bash. Do not write scripts with zsh. Do not write scripts with fish. Use POSIX shell. sh has a really bad interactive mode (so does bash), so I'm not gonna give anyone a hard time for using another shell as their…

I have the opposite opinion. I have seen developers proudly putting /bin/sh instead of /bin/bash, without any other shell to test. When the script was run on ubuntu instead of redhat, this called dash and we discovered that the script was finally not compliant on subtle details. Writing POSIX compliant scripts is harder. Where is the reward when bash is present (almost) everywhere?

A factor two or three in memory consumption.

Re: Shell Style Guide

#80

>Bash is the only shell scripting language permitted for executables. Whelp, wrong right off the bat. I'm gonna get down my my knees here and beg everyone reading: use POSIX shell. Do not write scripts with bash. Do not write scripts with zsh. Do not write scripts with fish. Use POSIX shell. sh has a really bad interactive mode (so does bash), so I'm not gonna give anyone a hard time for using another shell as their…

Truly portable shell scripts cannot define functions. Have you ever seen autotools' output? No thanks, I'll be impure and wrong and get the job done. Also your comment is off-topic... not sure why you see every thread about Bash as the right place to start this pedantic flameskirmish.

I don't see how the comment is off-topic. While the HN title is "Google's bash style guide", the actual guide has the title "Shell Style Guide". To some, that reads as if a "Programming Style Guide" started by saying only C is allowed.
Post reply on HN