6 ms·
More Things I Wish I’d Known About Bash
- nerdponx 9y agoJust another plug for Zsh: it has all of these features and then some. - Safe-by-default parameter expansion: no word splitting unless you ask for it, even if you don't quote the expansion. - Ability to use histoy expansions (like !!:gs/foo/bar) on parameter expansions, meaning "${foo:A:h}" is equivalent to "$(dirname $(realpath $foo))" - Much better array support, including both integer-indexed and associative arrays - A built-in CLI option parser that's pretty robust ("zparseopts") - Lazy-loaded functions - Floating-point arithmetic
- zwischenzug 9y agoPeople keep mentioning zsh to me in response to this article - I think I'll have to go and read up on it more.
- e12e 9y agoI still stick with bash - but if you're willing to move to a "friendlier" shell - my recommendation would be fish. Better out-of-the-box box experience than zsh - but still has the issue that most servers will have bash as the default interactive shell for root etc. https://fishshell.com/ https://fishshell.com/
- barrkel 9y agoProduction servers don't generally have zsh available, while most have bash. That means that ssh sessions will use a different shell, and scripts I write will use a different shell. And so, the upside for using some funky shell locally is greatly reduced. Whereas if I stick with bash, I can run my scripts almost everywhere and almost every server I ssh into has a familiar environment. Thus my knowledge of edge cases and scripting idioms from bash pay dividends. For situations where I need better arrays, associative arrays, floating point arithmetic, I'm probably better off writing it in an actual scripting language.
- nerdponx 9y agoFair enough! One tends to get spoiled with Zsh, trying to go back to Bash. In the past I've actually downloaded Zsh, compiled it from source, and ran it out of ~/.local/bin with absolutely no issues. But that's not something I'd advise.
- zwischenzug 9y agoPrevious post discussion: https://news.ycombinator.com/item?id=16084763 https://news.ycombinator.com/item?id=16084763
- SteveNuts 9y agoI'm so happy that Bash is getting the love it deserves.
- zwischenzug 9y agoMe too!
- awll 9y agoSigh. Hit ‘up’, ‘left’ until at the ‘p’ and type ‘e’ and return. One could also use <c-p>,<c-a> and <c-f> to achieve the same result much quicker.
- DrRobinson 9y agoI prefer "grep !*", which will be replaced by all arguments to the previous command
- zwischenzug 9y agoTrue, I use that too, but gets trickier in the middle of the line.
- mdaniel 9y agoAs a for-your-consideration, C-r leaves the cursor at the matched string when doing reverse search; so "echo helol world", enter, mutter "rats", C-r, o-l-sp, right (just to break the search), and voila you are now positioned on the offending substring
- binaryphile 9y agoYup. The moment you need editing capabilities more sophisticated than those three keys, though, I recommend switching to vi input mode (set -o vi) if you are familiar with vim keybindings. Although tapping Esc is not as quick as Ctrl (or, my chosen alternative, Ctrl-[), you have an entire library of editing commands already at your beck and call. I find that using 'f' or 'F' and '.' more quickly triangulates the problem area of the text in most cases. Of course, if you are an emacs user, more power to you with the emacs bindings.
- DrRobinson 9y agoIMO you might as well run "fc" (if you have vim set as your editor) in that case, rather than changing mode.
- 9y ago
- hyperpape 9y agoI imagine most uses of random in Bash don't need to be that robust, but it might be worth mentioning that ${RANDOM}${RANDOM} has a lot of bias as a random number generator.
- zwischenzug 9y agoFair point - have updated accordingly.
- _kst_ 9y agoIf you want 30 rather than 15 bits of randomness: echo $(((RANDOM << 15) + RANDOM))
- cryptonector 9y agoIf you want random numbers in bash then use dd if=/dev/urandom ... $RANDOM is not cryptographically secure.
- iamdave 9y agoWell that first example with the mistyped grep command just blew my mind.
- guhcampos 9y agoPortuguese speakers have counted for a long time with this gem, simply the best Bash doc/cheat sheet I've ever seen on the web. This is probably my oldest bookmark still relevant after 15 years. It's in portuguese and I'm not sure if there's an official translation, yet it's easy enough to decipher if you know bash, and Google Translate will do a pretty decent job. I gift you "Aurelio's Swiss Army Knife of the Bash Shell" - http://aurelio.net/shell/canivete/ http://aurelio.net/shell/canivete/
- icebraining 9y agoAh, curious. Despite being a native Portuguese speaker, ever since I learned English well enough (which was before I started using Linux), I always search for stuff in English, so I never came across that guide. That said, that guide doesn't seem to have the tricks from the top article, at least 1-4.
- gerdesj 9y agoThanks for the heads up - it does work via translate. I'll drop this: http://tldp.org/LDP/abs/html/ http://tldp.org/LDP/abs/html/ If anyone, who has to do anything BASH related, has not seen it then they should!
- gdavisson 9y agoI'd recommend against the Advanced Bash-Scripting Guide -- it uses a lot of bad practices (like unquoted variable references!) that you're better off not learning. Instead, check out http://mywiki.wooledge.org/BashGuide http://mywiki.wooledge.org/BashGuide and http://mywiki.wooledge.org/BashFAQ http://mywiki.wooledge.org/BashFAQ. Also, http://www.shellcheck.net http://www.shellcheck.net is a good automated tool for sanity-checking syntax errors and common mistakes.
- bewuethr 9y agoAurelio is the madman/genius behind Arkanoid in sed[1], Sokoban in sed[2] and sedsed, a sed debugger[3]. I have no trouble believing that he has a few shell tricks up his sleeve. [1]: http://aurelio.net/projects/sedarkanoid/ http://aurelio.net/projects/sedarkanoid/ [2]: http://aurelio.net/projects/sedsokoban/ http://aurelio.net/projects/sedsokoban/ [3]: http://aurelio.net/projects/sedsed/ http://aurelio.net/projects/sedsed/
- _kst_ 9y agoAs I mentioned in my comment on the article, setting $TMOUT if you want a timeout on a 'read' command is unnecessary and unclear. Just use "read -t": read -t 5 foo || foo='No reply' Setting $TMOUT affects all following 'read' commands. Also, setting $TMOUT in an interactive shell sets a timeout for a response to the primary prompt, terminating the shell if the user doesn't respond in time.
- antoineMoPa 9y ago"Sigh. Hit ‘up’, ‘left’ until at the ‘p’ and type ‘e’ and return.". My solution for this one would be : UP CTRL-A RIGHT RIGHT e Which needs less thinking and 6 keystrokes instead of 8.
- Domenic_S 9y agoEven better: https://github.com/nvbn/thefuck https://github.com/nvbn/thefuck
- hiq 9y agoI don't like to use arrow keys, and on my system the following works: CTRL-P CTRL-A CTRL-F CTRL-F e I also prefer this to the solution in the article.
- chrissoundz 9y agoPotentially of interest: https://github.com/Fakerr/goto https://github.com/Fakerr/goto sort of like the easymotion vim plugin.
- cryptonector 9y agoBut that requires modifiers. Insert modal vs. not flame war.
- ckuehl 9y agoThis is a somewhat dangerous pattern for picking temporary files (from #8): $ NEWFILE=/tmp/newfile_${RANDOM} $ touch $NEWFILE The problem is that any user on the box can create files under /tmp. An attacker can set up a bunch of symlinks like /tmp/newfile_1, ..., /tmp/newfile_99999 pointing to a file owned by your user. When your script then writes into this temporary file, you'll write through the symlink and clobber one of your own files. Especially dangerous if root :) This has been a historic source of software vulnerabilities (often with the PID used instead as the guessable component instead of random, though). One recommended alternative is to use the `mktemp` command instead.
- xchaotic 9y agothis is from a guy writing abook on Docker, so I hope this is for a single purpose cointainer with now other users of THAT /tmp
- jwilk 9y agoNitpick: $RANDOM gives you an integer between 0 and 32767 (inclusive).
- cryptonector 9y agoit's a very dangerous pattern. Use mktemp(1) instead!
- gvalkov 9y agoFiy, heredocs also have a variant that strips all leading tab characters. Quoting from `man 1 bash`: If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here- documents within shell scripts to be indented in a natural fashion.
- Xophmeister 9y agoThere’s also a variant, where you single quote the marker, that won’t expand variables: cat <<‘EOF’ This will not be ${expanded} EOF
- partycoder 9y agoI agree with most of it except for: ${RANDOM}${RANDOM} A preferred way would be od -vAn -N4 -tu4 < /dev/urandom /dev/urandom gives you random bytes, od dumps them in different formats (e.g: hex, octal, decimal and such). This takes 4 random bytes and outputs them as a 4 byte unsigned int.
- jwilk 9y agoExplanation of options: -v: Don't suppress duplicate lines. (It doesn't make a difference here, because there's always only one line.) -An: Don't write input offsets. -N4: Read at most 4 bytes. -tu4: Print 4-byte unsigned integers.
- alinspired 9y agohttps://explainshell.com/explain?cmd=od+-vAn+-N4+-tu4 https://explainshell.com/explain?cmd=od+-vAn+-N4+-tu4
- dorfsmay 9y agoInteresting, most of these are from korn shell and well documented in "The korn shell command and programming language" by Bolsky and Korn.
- cryptonector 9y agoBut you really should know that set -e is broken.
- cryptonector 9y agoSo... why the downvotes? Is it not the case that set -e is broken? Or is it that I provided too little detail? Or is it that it's broken in the same way in all shells? FWIW, the bug (yes, it's a bug that the Open Group has decided is not a bug, but it really cannot be anything other than a bug!) is this: non-zero exits by commands/functions are ignored when invoked by functions invoked in a conditional expression context.
- coroxout 9y agoI use pushd and popd every day. If you forget what's in the stack you can see what's in it by using the "dirs" command. You can also use pushd -n (where n is a number, although I usually end up needing trial and error to get the right one) to rotate the list of dirs without removing any from the stack - useful if the list has more than 2 directories, or 2+ directories you need to switch between repeatedly. pushd and popd also work out of the box in Windows command prompt, although you don't get "dirs" or any fancier options like in bash, just push and pop on a plain old stack.
- sli 9y ago^x^y^ Is that third caret necessary? I've never had to include it. Although that may be zsh taking a shortcut on bash syntax.
- bewuethr 9y agoIt's optional, also in Bash, even though I can't find it explicitly mentioned in the manual.
- 7kmph 9y agoThings I Wish I’d Known About Bash: don't use it for script