5 ms·
Every complaint on HN about Bash is from one of two people: 1) someone who has never read the man page, 2) someone who wishes Bash were Python. Bash is my favo
by throwawaaarrgh 4y ago
Every complaint on HN about Bash is from one of two people: 1) someone who has never read the man page, 2) someone who wishes Bash were Python.
Bash is my favorite programming language, because it's not really a programming language. Rather than being some academic philosophical high-minded solution, it was written by systems practitioners for backwards compatibility and usability. Once you learn it, it replaces 85% of other languages for systems work.
- AtlasBarfed 4y agoYes whitespace sensitivity, magic characters (WITH whitespace sensitivity), no standard library, no unicode, horrible math expressions, awful string, mishmash of acceptable good practices and outmoded stuff, printlns to debug, subpar error messages, no particularly good IDE, dynamic typing but really none of the convenience. I honestly have no idea how anyone got anything done before Stackoverflow with bash. It's all copy paste and half the things you need to do in modern scripting isn't in typical UNIX command set (curl, etc) and there's no dependency resolution mechanism. But yeah, read the manpage to fix everything. Sure dude. The article is right though. Command line and pipes is a flow type model, but programming is a different ordered thing in the natural sense: Command flow: cat file | grep values | sort values Programming flow: sort(grep(cat(file))
- martopix 4y agoI am admittedly someone who wishes Bash were Python (ish). What's wrong with that though? Of course I want the REPL features of Bash, its terseness, its builtin commands for all sorts of OS and filesystem operations. But is it wrong to ask for a more modern syntax on the language side of things (i.e. the for loop for example)?
- graymatters 4y agoAh, that’s kind of like refugees fleeing all kinds of failing states to better places, that have rule or law, democracy, human rights, prosperity and then actively trying to install the norms of the old country in the new place they fled to.
- deleted 4y ago[deleted]
- dfinninger 4y agoWell, a bash for loop is pretty close to Python: for x in a b c; do echo $x done Or ranges: for i in {1..10}; do echo $i; done Do you mean list comprehensions?
- intrepidhero 4y agoThe number one use case of `for` in shell scripting is over the output of `$(ls .)` And I immediately run into a problem with filenames that have spaces and then I discover that a stringly typed language has lots of sharp edges. I fall back on `for filename in os.listdir():`
- npongratz 4y agoIf I may humbly offer a suggestion, looping over filenames should [edit: almost] never be done with ls. find can be used with much better fidelity: https://www.shellcheck.net/wiki/SC2012 https://www.shellcheck.net/wiki/SC2012 "ls is only intended for human consumption: it has a loose, non-standard format and may "clean up" filenames to make output easier to read."
- cb321 4y agoShells usually do not re-split on whitespace after filename generation. So, you could also just use an asterisk for your number one use case: dash$ touch "hi ho" dash$ touch "there, buddy" dash$ for f in *; do echo $f; done hi ho there, buddy (EDIT: That was in a scratch directory.) But as @npongratz alludes to in sibling https://news.ycombinator.com/item?id=34727735 https://news.ycombinator.com/item?id=34727735 find . [predicates] -print0 | xargs -0 is bulletproof and directory scanning, output, and input loops all run at full C speed. (One predicate is `-maxdepth 1` to not recurse.)
- dolmen 4y agoTry this: touch -- -n
- 4y ago
- funOtter 4y agoI would like to learn it. Besides the man page, any other resources (books, web pages, etc) that you recommend? Thanks!
- byproxy 4y agoI’d recommend installing Shellcheck and an associated plugin for your editor of choice. I have the plugin installed for VS Code and open the link presented to me when I do anything “wrong” as it’ll give a good explanation for the “better/more correct” way of doing things.
- throwawaaarrgh 4y ago- Absolutely, everyone should use Shellcheck - https://tldp.org/LDP/Bash-Beginners-Guide/html/index.html https://tldp.org/LDP/Bash-Beginners-Guide/html/index.html - https://www.gnu.org/software/bash/manual/bash.html https://www.gnu.org/software/bash/manual/bash.html - https://devdocs.io/bash/ https://devdocs.io/bash/ - https://tldp.org/LDP/abs/html/ https://tldp.org/LDP/abs/html/ - https://devhints.io/bash https://devhints.io/bash
- tenebrisalietum 4y agoNope, bash isn't a programming language. It's a shell - a shell is a program whose purpose is to launch other programs.
- chasil 4y agoI suggest that you port CMD.EXE to POSIX.
- dmix 4y ago> Once you learn it, it replaces 85% of other languages for systems work. It's funny you say no one learns it, then say you could use it for 85% of systems work. Then the next dude comes along who has to maintain it and we're back to square one.
- jascii 4y agoI'm not sure that's quite true. I like many others are by no way a Bash expert, and will generally reach for Python if I have to build anything of reasonable complexity. I (and many, many like me) have however had enough Bash exposure to do day to day maintenance on Bash scripts.
- capitol_ 4y agoIt's clear that I should write more complaints about the lack of algebraic datatypes in bash.
- chasil 4y agoFrom the perspective of writing a parser, advanced knowledge is required. The lex and yacc parsing tools will be woefully insufficient to write a POSIX shell, as the language is not LR-parsable. It would have been much cleaner and more consistent if awk had been chosen as the scripting language of the shell, as that had a yacc grammar for most of its life. https://archive.fosdem.org/2018/schedule/event/code_parsing_posix_s_hell/ https://archive.fosdem.org/2018/schedule/event/code_parsing_...
- mananaysiempre 4y agoAs far as I remember that talk, the parsing problems come down to two points: - Shell aliases are grammar-defying macros in basically the same way as C preprocessors macros are but unlike those are not separable from the actual execution, which, yes, they’re a misfeature that defies static analysis, I don’t know why people seem to prefer aliases to shell functions so much; - The "modern" $(... $(...) ...) syntax, unlike the "legacy" (per shellcheck) `... \`...\` ...` syntax, requires invoking the parser from the lexer, which, also yes, but as far as I know that’s a problem that every language with general expression interpolation must face—Python’s f-strings tried to sidestep the problem, but I’m not sure the resulting restricted language with weird escaping rules was worth it. For a talk that was supposed to be about problems encountered while building a parser, I was surprised by how much of a non-event both of the problems were.
- chasil 4y agoThis does not change the ambiguity of the language, nor does it remove the need for a prelexer. Quoting the talk, very quickly... Which command outputs \\? echo "\\\" echo "\\\\" echo "\\\\\\" You may see this as a non-event, but the rest of us are not quite comfortable with this.
- Karellen 4y agoHow is that different from backslash-escaped strings in C, Java, Javascript, Python, and... uh, a whole bunch of other common languages? Which of those gives you a string with two backslashes in those other languages? Exactly the same one as (ba)sh.
- jerf 4y agoThe simplest thing you can do should be the correct thing to do. It is impossible to do this 100% in a programming language, absolutely. But shell violates this all over the place for programming purposes. And it's not like "read the bash man page" has a nice clear "hey, here's the way to write bash as a safer programming language". I like bash as an interactive shell. Don't love it but like it enough to not go looking around for "better". As a programming language it's not great. It's convenient, but in terms of discussing which is a bigger minefield of problems, C or bash, I'm not sure. C is worse in practical impact because more people put it on the network, but bash is right up there in how much like walking through a minefield it is. I still use it but I definitely have it linted through shellcheck to do anything serious, and shellcheck always has things to say. (And yes, bash is far from the only programming language to have this problem.)
- ElectricalUnion 4y ago> C is worse in practical impact because more people put it on the network Unfortunately the one thing that I learned in the whole Shellshock Vulnerability thing is that a surprising number of things [still?] runs on shell/GCI.
- coldtea 4y ago>Every complaint on HN about Bash is from one of two people: 1) someone who has never read the man page, 2) someone who wishes Bash were Python. Suitcase designer before 1970 [1]: "Every complaint about luggage is from one of two people 1) someone who has no strength to carry them, 2) someone who wishes suitcases were like carts - as if it makes sense to add four small wheels and a telescopic handle to a suitcase!" [1] http://edition.cnn.com/2010/TRAVEL/10/04/wheeled.luggage.anniversary/index.html#:~:text=Sadow%20applied%20for%20a%20U.S.,the%20market%20to%20wheeled%20luggage http://edition.cnn.com/2010/TRAVEL/10/04/wheeled.luggage.ann.... >Rather than being some academic philosophical high-minded solution, it was written by systems practitioners for backwards compatibility and usability. Rather than written it was accumulated and piled upon.
- H4ZB7 4y agoahh, the myth of the pragmatist who outsmarts the academic. one day you will grow up. bash is a terrible language, and no academic background is needed to come to this conclusion
- dolmen 4y agoYou haven't written MS-DOS batch files.
- ranger207 4y agoBash's primary problem is that everything is a string. PowerShell is a shell that makes for a decent programming language primarily because it passes objects between pipes instead of just strings. (Well, and the fact that 90% of what it's used for is made by Microsoft so there's a degree of consistency everywhere)