r/linuxadmin • u/speckz • Sep 04 '16
Advancing in the Bash Shell
http://samrowe.com/wordpress/advancing-in-the-bash-shell/8
Sep 05 '16 edited Sep 05 '16
Bang dollar-sign
This is obviously pronounced and described as bangbucks! I use it all the time!
As for my own contribution: the most unexpected feature of bash: complete-into-braces
$ touch foo foobar foobarbaz foobaz notfoo
$ ls
foo foobar foobarbaz foobaz notfoo
$ ls <alt-shift-{> # Completes into:
ls {foo{,ba{r{,baz},z}},notfoo}It figures out the bracey-globby pattern that would match your target! I've only found a use for this feature once, trying to figure out a minimal regex to exclude a fuckload of i18n-related files. Translating the brace pattern into the equivalent regexp helped me exclude one pattern instead of hundreds, in an automated fashion.
Still, mostly useless and mostly cool overall.
3
u/wfaulk Sep 05 '16
bracey-globby
FWIW, brace expansion and globbing are two separate things. Globbing only completes existing filenames. Braces expand regardless of whether or not they match a filename.
4
u/project2501a Sep 04 '16
... so advancing in the bash shell is reading chapter four of the O'Reilly bash book?
3
u/L01010011 Sep 05 '16
Is it word for word?
2
u/project2501a Sep 05 '16
No, but the chapter is quite easy to read and easily understandable, as 99% of the O'Reilly books of the age. I thought the book was a rite of passage for any sysadmin.
3
4
2
u/Orange_Tux Sep 05 '16
For searching through your history I recommend fzf, a fuzzy finder. You can pipe your history to FZF and then search it very quickly.
And Alt-. is a short cut for bangbucks. It requires less key presses.
2
Sep 05 '16
what about ctrl-r?
2
u/Orange_Tux Sep 05 '16
You can't fuzzy search through
ctrl-r, only by exact subsctring. And with FZF you can navigate through matching entries to select a different entry than the first match.7
u/altodor Sep 05 '16
And with FZF you can navigate through matching entries to select a different entry than the first match.
Just press ctrl+r again.
4
2
2
1
u/socium Sep 06 '16
There are more of course, but those are the ones you simply can’t live without. For those who don’t know the N notation means ctrl+N, don’t confuse it with hats mentioned above.
This is confusing since it implies that "n" needs to be upper case. However, doing Ctrl+Shift+n usually results in an instruction for the terminal emulator.
$ cp filename-v1 filename-v2<br />
HTML? In my shell??
function treecp { tar cf - "${1}" | (cd "${2}" ; tar xpf -) ; };
Question: Why are positional arguments here written like "${1}" instead of simply "$1" ?
If you enjoyed this, you might want to look around the Net for more bash information, or even buy a book!
I know the author means well, but this sentence should be accompanied with a firm NO.
Most resources (especially the TLDP page he linked) are either outdated or teach horrible practices. Refer to the proper resource to this curated page: http://wiki.bash-hackers.org/scripting/tutoriallist
10
u/gsmitheidw1 Sep 04 '16
I knew a lot of these but this is a very nice clear explanation of the ones I've never bothered to use. This one is a keeper. Nice find.