8 ms·
The bash book to rule them all
- deleted 3y ago[deleted]
- asicsp 3y agoDupe: https://news.ycombinator.com/item?id=38187450 https://news.ycombinator.com/item?id=38187450 (the url here has `index.html` at the end, so wasn't automatically caught as dupe)
- et1337 3y agoI answered every one of those questions “yes, but why is this even a thing?” Only in shell can something as simple as global variables be a “fallacy”.
- notcensored02 3y ago[dead]
- Already__Taken 3y agoI feel anyone that really likes bash needs to have a concerted effort to learn how powershell works. It's got a lot of failings and MS has made deploying things and using them a pain but there's some genius in there and specifically the help system is categorically better. nushell is having a similar crack at it. a rising tide lifts all ships but bash is the mooring block.
- Already__Taken 3y agoI feel like anyone disagreeing with me that they won't swap to powershell every day from now on didn't really read my point and just clutched bash harder. I just said learn from it a bit, you don't have to live with it. It's not perfect, it won't replace your unix tools and bash because it never promised to, it's different. Python isn't trying really hard to be a better javascript.
- taraparo 3y agoI gave powershell a good try but I believe it sucks. too verbose and the syntax is too inconsistent. trying nushell next, it looks very promising.
- mr_mitm 3y agoThere are short aliases for the most used Cmdlets and tab completion takes care of the rest. Type "Get-Process -" and then press CTRL-Space for a list of all arguments. That works with any Cmdlet. Impossible in bash. PowerShell is one of the best ideas to ever come out of Microsoft.
- INGSOCIALITE 3y agoso one of the best ideas to ever come out of microsoft is an attempt to copy the functionality of bash and incorporate it into their .net ecosystem? i hardly think this is a compliment
- Already__Taken 3y ago> why invent PowerShell, why not just use ksh or bash? I’m a long time Unix dev so that was my first instinct. I tried and failed. There is a core architectural difference between Unix and Windows. Linux is a file-oriented OS and Windows is an API-oriented OS. In Linux, if you can modify a file and run a process, you can manage anything. That is why awk, sed, and grep are management tools. At the time, nothing on Windows worked that way. Everything was behind an API which returned a structured object. That is why awk doesn’t work with WMI, sed doesn’t work with Active Directory, and grep doesn’t work against the registry. I had to invent a new tool that manipulated this environment. - Jeffrey Snover, Powershell Inventor. https://evrone.com/blog/jeffrey-snover-interview https://evrone.com/blog/jeffrey-snover-interview Literally designed not to copy bash.
- steve1977 3y agoIt’s not like bash (or UNIX shells in general) have invented command line interfaces…
- 3y ago
- chasil 3y agoThe POSIX shell, like the 1988 version of the Korn shell before it, is able to operate with minimal memory. In the case of ksh88, it was able to be compiled into a 64k text segment which was the maximum allowed on Microsoft XENIX running on an 80286. This is a place that Powershell cannot go.
- chubot 3y agoNo this doesn't really make sense. Unix shell is "situated in" a bunch of native executables (grep, awk, ripgrep, etc.) and textual data, while PowerShell is VM-based, with bindings to Windows. If I were doing stuff with Windows registries, I might use PowerShell, but I don't. I use Unix. One is not a substitute for the other -- it's both the language AND the "bindings" that count. bash runs on Windows, and PowerShell runs on Linux, but you lose a lot of "enviroment" in both cases. I wrote about this distinction here - https://www.oilshell.org/blog/2023/06/ysh-design.html https://www.oilshell.org/blog/2023/06/ysh-design.html - Oils Is Exterior-First (Code, Text, and Structured Data) In those terms, I say PowerShell/nushell are "interior-first", while bash and OSH/YSH are "exterior-first". It makes a big difference when using them.
- forgotpwd16 3y agoBelieve GP meant it as (not necessarily end up using it due to lacking the environment as you said) "check pwsh to see how things can be if you think bash is good". Worth noting, since you mention that pwsh has bindings only to Windows, that it's possible to make Linux-specific cmdlets/bindings in fashion similar to your YSH if understood it correctly (that is layering an object environment atop the text i/o of Linux commands).
- joiewjfoi2 3y agoIn PowerShell, you can skip the need to master numerous DSLs for various data formats. Simply convert XML, JSON, YAML, etc., into objects and leverage built-in methods for manipulation. Bash, on the other hand, often requires you to familiarize yourself with tools like xmlstarlet and JQ, making the learning curve steeper. Additionally, PowerShell offers enhanced reliability since it primarily deals with structured data rather than raw text. Its consistency is also a notable advantage when compared to Bash, and the longer cmdlet names are offset by handy 2-3 letter abbreviations.
- Already__Taken 3y agoI'd like to add my issue with the specific DSL approach isn't the learning curve, but the curve is a saw tooth pattern. If you don't keep using those DSL's weekly, in 2 years I dare you to write a valid one from memory. pwsh reinforces pretty much the same* thing every time it's used, the more you learn the more you can do with every command, even ones you haven't used yet. same subject to some base-level provided by the module. i.e. the active directory cmdlets have their own dsl inside -filter because it is a different thing. It's very analogous to `a = sql.cmd('SELECT ...').map(etc...)`. you can, you shouldn't, but maybe its fine. It's up to you.
- __MatrixMan__ 3y agoI really like bash. I tried to like powershell. Nushell feels much better than powershell to me as it's not overspecialized for integration with .Net and it gets many of the shell niceties right out of the box (vi mode, simultaneous history and file autocomplete). I think there will always be a place for something like bash pipelines, where the handoff is just a bunch of bits. Some work is all about discovering the structure of your data: you can't have tools that already know it. But that's not the majority of work these days. Having the handoff be just bits is an elegant solution to the tower-of-babel problem, but it would probably be ok if we spent more time in structured-mode and only leaned into that solution when it was specifically called for. The problem is that there's no intrinsic structure to data, just useful fictions. This makes structured-mode necessarily opinionated, which means that the ecosystem will always be fragmented: bash and zsh are friends. Powershell and nushell are rivals.
- ndsipa_pomu 3y agoSorry, but powershell is not going to help me manage a bunch of linux machines and it doesn't come with all the usual gnu tools that I'm used to. I keep thinking that I should do more complicated stuff in a proper language, but BASH is just positioned as a perfect duck-tape kind of language - it ties together a bunch of other tools and I know it's going to already be there on any Linux machine that I deal with.
- mgdlbp 3y agoSome (topical) amusements: $ echo 25 | pwsh -c '$input-replace"\d",{2*"$_"}' 410 $ echo 25 | perl -pe 's/\d/2*$&/eg' 410 ``` PS> $temp:pwsh_out = 10000 PS> /bin/cat /tmp/pwsh_out 10000 ``` $ echo '''' PS> echo '''' ' ``` PS> $Y = { param($f) . { param($x) . $x $x } { param($y) . $f { param($x) . (. $y $y) $x }.GetNewClosure() }.GetNewClosure() } PS> $fac = & $Y { param($f) { param($n) $n -lt 2 ? 1 : $n * (. $f ($n - 1)) }.GetNewClosure() } PS> & $fac 5 120 ``` PS> gv -v|% m*d|% t* gv -v|% m*d|% t* PS> &($x={"&(`$x={$x})"}) &($x={"&(`$x={$x})"}) $ bash $ eval ${x='echo eval \${${x@A}}'} eval ${x='echo eval \${${x@A}}'} ``` bilingual pun, .ps1 .pl 1 1 {1} {1} end {1} sub {1} {end {1}} {sub {1}} &{end {1}} &{sub {1}} ``` awk -e 'BEGIN { printf "3\n" }' perl -e 'BEGIN { printf "3\n" }' ruby -e 'BEGIN { printf "3\n" }' pwsh -c 'BEGIN { printf "3\n" }' ``` $ time ...; time ...; .... real 0.00 ... real 0.00 ... real 0.03 ... real 0.22 ...
- no_time 3y agoPowershell for Linux fails the Litmus test of "Is it in the Debian community repo" This means it's either - Some "Open Core" bullshit with the secret sauce conveniently tucked away in some proprietary blob or restrictive license submodule - Has a build process so convoluted and mystery-meat-dependency filled it's not worth to bother with. - Has so many anti features it would require an extensive patchset (like ungoogled-chromium) to fix. So it might be interesting technology, but it's not something I'd deploy by default to every server and expect to work in 5 years time.
- eichin 3y agohttps://github.com/PowerShell/PowerShell/blob/master/LICENSE.txt https://github.com/PowerShell/PowerShell/blob/master/LICENSE... is the MIT license. (Microsoft supplies debs directly which may reduce the motivation for Debian to do so.) Oh, heh, also https://github.com/PowerShell/PowerShell/blob/master/docs/building/linux.md https://github.com/PowerShell/PowerShell/blob/master/docs/bu... the build script is written in PowerShell, so there's a bootstrapping problem :-) (Debian has solved those before of course, but with community sentiment like the above maybe noone is motivated to bother.)
- usrbinbash 3y ago> needs to have a concerted effort to learn how powershell works. Unless I am a windows administrator, my question is: Why should I? There is literally ZERO reason for me as an administrator *nix machines to ever touch pwsh. > but there's some genius in there I am sure there is. But it's buried very well under layers upon layers of deep integration with windows and .NET, the attempt to shoehorn a character-based shell into working with objects somehow, and a syntax that rivals enterprise java in its verbosity. > nd specifically the help system is categorically better Is it? Why? Man pages, infopages, and `apropos` all exist. Individual man pages may be bad, but the system as a whole is solid. It also isn't dependent on the shell. All these mechanisms are just normal programs that can be executed by any shell. > a rising tide lifts all ships but bash is the mooring block. What?
- rvdginste 3y ago> But it's buried very well under layers upon layers of deep integration with windows and .NET, the attempt to shoehorn a character-based shell into working with objects somehow, and a syntax that rivals enterprise java in its verbosity. You do know it runs on Windows, Mac (amd64/arm64) and Linux(amd64/arm)? There is definitely integration with .net, which is available for the same platforms. The integration with .net is part of its power for me. Verbosity can increase readability. > Is it? Why? Man pages, infopages, and `apropos` all exist. Individual man pages may be bad, but the system as a whole is solid. It also isn't dependent on the shell. All these mechanisms are just normal programs that can be executed by any shell. You will not loose those tools and their man pages when using PowerShell. He is referring to the documentation of all helper functions defined within PowerShell. When you create a helper function in Bash, how do you document it and what tool can you use to consult that documentation? Bash does not have named parameters for its functions, let alone metadata on them…
- usrbinbash 3y ago> You do know it runs on Windows, Mac (amd64/arm64) and Linux(amd64/arm)? "Runs somewhere" and "Is useful there" are two very different things. > Verbosity can increase readability. And usually it hinders readability. If anyone disagrees, he may explain how enterprise Java is more readable than Python, Go or Rust code. And yes, I do consider Rust more readable than Enterprise Java. And that's when we talk about PROGRAMMING LANGUAGES. We are, however, talking about SHELL LANGUAGES. Other than most PLs, shells are in fact written more than they are read, so yes, "typeability" and terseness is a feature in shells, not a problem. And before anyone says "autocomplete": If a shell requires autocomplete to be somewhat useable, I will look for another shell. > You will not loose those tools and their man pages when using PowerShell. Did I say I will? That line was in reply to the statement that "the help system is categorically better", a statement I happen to disagree with. > He is referring to the documentation of all helper functions defined within PowerShell. And I am refering to the documentation that is defined within the shell environment on *NIX terminals. > When you create a helper function in Bash, how do you document it and what tool can you use to consult that documentation? Off the top of my head: - I can embed a `-h --help` flag directly in the script - I can write a manpage and use it via `man NAME` - A manpage is also discoverable by the `apropos` program - If it's a builtin I can use bashs builtin `help PATTERN` - I can write an entry for the `info` program So yeah, I'd say there are more than enough methods, all of which are well established in the shell ecosystem, and work seamlessly with it. > Bash does not have named parameters for its functions, let alone metadata on them… Which would truly be a bummer if bash needed named parameters. Since it doesn't, I don't see why this would be an advantage. If I want named params for a shell script, I can write it in python.
- throwawaaarrgh 3y agoI mean, the man page is the Bash book to rule them all, but it's not a fun or light read. If you want to truly understand Bash you need to read it.
- skulk 3y agoI've used bash for the last 10 years but never read the man page, probably never even opened it once. Is there some nugget of knowledge that I've missed out on by learning it entirely from Google searches and Stack Exchange?
- read_if_gay_ 3y agoActually, if you truly, really want to understand Bash by my personal arbitrary standard, then you must be able to produce the entire source code from memory, because that is the real book to rule them all. Sure, you might be able to navigate your everyday tasks perfectly, but that's not what matters now is it?
- chubot 3y agoYeah, I've been deep in the bash manual and source code during my work on https://www.oilshell.org https://www.oilshell.org, which basically reimplements a "cleaned up" spec-driven bash (called OSH). But I also used bash for 15 years before that, and I hardly used the bash manual. I got by with a minimal / sane dialect, and I read a few books about Unix, and one about POSIX shell (the latter is also "meh"). Now that I've read almost all of the bash manual, I'd say it's not really a special document. It's fine, but it's not complete, and I wouldn't say it's particularly well written. It lacks examples. -- The main recommendation I have for anyone who wants to learn bash is to learn Unix and C. Learning the underlying model explains many things, explains the bad error messages, and makes it easier to use. https://www.oilshell.org/blog/2020/04/comics.html https://www.oilshell.org/blog/2020/04/comics.html - Three Comics For Understanding Unix Shell So yeah I don't think "learning shell" or "learning bash" is actually a great goal -- it's part of the operating SYSTEM, and you want to learn the OS, and its philosophy. related - https://www.oilshell.org/blog/2021/01/philosophy-design.html https://www.oilshell.org/blog/2021/01/philosophy-design.html - Unix Shell: Philosophy, Design, and FAQs -- Probably the most useful part of the bash manual is the acknowledgement that there's a lexing design bug with [[ and regular expressions. It's better to make the regex a separate var, like pat='[[:digit:]]+' if [[ $mystr ~= $pat ]]; then ... fi ... than to inline it, because bash does funky stuff when you inline it. It confuses regex metachars and bash metachars. It's trying (and failing) to make regexes syntaically part of the bash language, but it's better to treat it like Python, where Python syntax and regex syntax are separate.
- da39a3ee 3y agoI'm not sure the book is actually about bash specifically, but let's talk about shells anyway. The existence of zsh makes it all a pretty unsatisfactory situation currently. (Btw re. my own level I know the answer to most of the questions posed in the article). I'd be fine learning either bash or zsh to an advanced level, but learning both to an advanced level sounds bonkers. So I've vaguely come to the conclusion that perhaps that means learning zsh well as a command line tool, and then limiting ones shell scripts to a sane subset of bash that is sufficiently small that you don't need to fully know advanced bash. But then, I don't actually like learning advanced zsh. It seems like a silly language full of completely ad-hoc syntax. Nevertheless, I do use zsh as my shell, I think mainly because tab completion is better. (I went heavily into nushell, but at the moment it's too different from what one's colleagues are using so it can only be an auxiliary to POSIX shells, and I decided it was a time sink I couldn't afford.)
- justaj 3y agoIn order to be sure that your scripts work across most of the shells, you can also aim at POSIX compatibility.
- stephenr 3y agoThe solution you want is to target posix compatibility; you can choose how strict you want to be with that (ie essentially every posix compatible shell also supports the `local` keyword).
- mmh0000 3y agoThe wooledge bashguide:1: and bashfaq:2: are the best bash resources I’ve ever read. :1: https://mywiki.wooledge.org/FullBashGuide https://mywiki.wooledge.org/FullBashGuide :2: https://mywiki.wooledge.org/BashFAQ https://mywiki.wooledge.org/BashFAQ
- deleted 3y ago[deleted]
- ndsipa_pomu 3y agoDon't forget to read up on the pitfalls too: https://mywiki.wooledge.org/BashPitfalls https://mywiki.wooledge.org/BashPitfalls However, I would put using ShellCheck as the single most important way to improve your shell scripts
- tempodox 3y agoI love this article. Short and to the point. I'll go get this book.
- chasil 3y agoI do not agree with an approach of a book that starts with bash. Realistically, starting with the POSIX shell, most commonly seen as Debian dash, is the best introduction. Knowing this standard (and what is not in it) will let you write highly portable scripts. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V... From here, moving to a text on the 1988 version of the Korn shell is a conservative approach to extended shell syntax (as ksh88 predates the POSIX shell). Korn's own first edition is not a bad place to start. https://www.amazon.com/KornShell-Command-Programming-Language/dp/0135169720 https://www.amazon.com/KornShell-Command-Programming-Languag... The ksh88 syntax also addresses mksh directly, the Android system shell. Taking this route to bash will be more fruitful than unlearning what these other branches of the shell do not support.
- brunorsini 3y agoThanks for the recommendation. In my case, GPT revolutionized the way I use bash. If you know what you want to accomplish, gone are the days of endlessly fiddling around with syntax.
- steve1977 3y agoUntil the day when GPT hallucinates some nice (and confident) rm -rf equivalent for you ;)
- anjanb 3y agoIt shouldn't be too difficult to pipe chatgpt output to bard or something equivalent and ask for validation
- spion 3y agoYou can also (surprisingly) ask ChatGPT for validation. The part of the LLM that activates when solving problems is going to be different than the one that activates when reviewing.
- ykonstant 3y agoWhich is extremely easy to do; just a misplaced `mv` is enough. I can't believe people are relying on GPT for ad hoc destructive I/O.
- MonaroVXR 3y ago>Which is extremely easy to do; just a misplaced `mv` is enough. Or `chown -r` or was it.. `chown -R`
- pipes 3y agoI used chat gpt to explain what a complex line in a bash script was doing. Though to be fair if I'd spent a day in man pages and Google I'd probably have learned a lot more than just being given the answer. Which makes think in the long run gpt may result in worse programmers.
- ulizzle 3y agoWhat's a good useful difference between .bashrc and .bashprofile? I usually just ignore everything but the rc files.
- usrbinbash 3y agoA shell can be interactive or non-interactive (eg. shell-scripts). An interactive shell can be a login-shell or a non-login-shell. A login-shell is what you get when you login at one of the virtual terminals of a linux machine. A non-login-shell is a shell started after you logged in (eg. clicking your console-icon in a graphical desktop environment). An interactive bash will read and execute `bash_profile` (Actually, it will try to read `profile` first, for historic reasons) when it is a login shell, and `bashrc` when it is not. However, the difference is moot on most systems, since this is what you will probably find in most `~/.bash_profile` files these days: [[ -f ~/.bashrc ]] && . ~/.bashrc aka. all interactive shells, no matter if they are or aren't a login shell will just read `.bashrc` For completeness sake: A shell run via `sshd` is technically not an interactive shell, as it is started by the ssh-daemon. However, bash behaves like an interactive non-login shell in this case and reads `bashrc`. Also for completeness sake: All the files mentioned can be system-wide under `/etc/FILENAME` and user specific (`~/.FILENAME`). Bash first reads the files in `/etc`. The behavior is documented in the manpage `man bash`, under the section `INVOCATION`.
- lloeki 3y ago> An interactive shell can be a login-shell or a non-login-shell. A shell can be login and non-interactive. This happens e.g when starting a session from a X session manager. Subsequently a terminal such as Xterm starts non-login interactive sessions, inherits stuff like env vars like PATH from the login shell, and is only concerned with setting up the additional interactive stuff. Similarly doing ssh <host> <command> starts a non-interactive login shell. > However, bash behaves like an interactive non-login shell in this case and reads `bashrc`. IIRC nope: distros such as Debian often have bashrc source bash profile (or the other way around, I can't recall) which has me irate to no end+. They even have some TTY dependent stuff in profile which spits out some error in some cases when no TTY is allocated because heh not interactive. + I took great length to have my rc and profile properly separated because it's that much faster not to source the unneeded stuff (at the cost of having to logout to apply login stuff) https://github.com/lloeki/dotfiles https://github.com/lloeki/dotfiles
- BeetleB 3y ago[flagged]
- deleted 3y ago[deleted]
- Maxion 3y ago[flagged]
- deleted 3y ago[deleted]
- moffkalast 3y ago> You want a pretty book with a beautiful Saker falcon illustration on the cover? I don't really get where the trend of making software books with animals on the cover originates from, but the parodies are fantastic. https://boyter.org/2016/04/collection-orly-book-covers/ https://boyter.org/2016/04/collection-orly-book-covers/
- schoen 3y agohttps://en.wikipedia.org/wiki/O%27Reilly_Media#Early_days https://en.wikipedia.org/wiki/O%27Reilly_Media#Early_days > The original cover art consisted of animal designs developed by Edie Freedman because she thought that Unix program names sounded like "weird animals".[2]
- youngtaff 3y agoAnd if the cover animal is in colour there’s colour inside, if it’s got a black and white animal then there’s only black and white print inside
- __mharrison__ 3y agoNot true. Author of colored cover with bw book...
- youngtaff 3y agoInteresting… they obviously changed there approach in the last few years
- auselen 3y agoThanks for this, made my evening!
- tap-snap-or-nap 3y agoThe reason is provided in the last pages of many books that they are either endangered or important/keystone species who are threatened.
- 3y ago
- gerdesj 3y ago"I ♥ to /bin/bash and last week I came around the one bash book to rule them all." The thing about programming is you have to at least nod to grammar and syntax and your IDE will sort you out (if you bother with one). The thing about submitting an article on HN or to the world in general is ... why not proof read the very first line. I doubt that any en_xx would "come around" and would prefer to "come across". Now that I read that critique back, I worry myself! Anyway: "I ♥ to /bin/bash and last week I came across the one bash book to rule them all."
- xcv123 3y agoEnglish is not his native language
- captaincrowbar 3y agoIs there a (legal) way to get this book in pdf?
- xcv123 3y agohttps://www.ebooks.com/en-nz/book/210497580/efficient-linux-at-the-command-line/daniel-j-barrett/ https://www.ebooks.com/en-nz/book/210497580/efficient-linux-...
- captaincrowbar 3y agoThanks. Strangely I'd never heard of this site before.
- ragesh 3y agoI find it interesting that the e-book costs over 4x the paperback from Amazon, which I just ordered.
- teddyh 3y agoI dutifully refer you to the ancient texts: Advanced Bash-Scripting Guide <https://tldp.org/LDP/abs/html/abs-guide.html https://tldp.org/LDP/abs/html/abs-guide.html> Bash Guide for Beginners <https://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html https://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginner...>
- aAaaArrRgH 3y agoJust so everyone's aware: the author receives a commission for each book sold through their Amazon link, so take that into account when reading their review.