bashcookbook (@bashcookbook) profile picture

writers of the best bash book

Joined December 2008
Photo by bashcookbook (@bashcookbook)
JP & Carl are back with the next step to increase your bash skills - "bash Idioms" - just out from O'Reilly.
3
Busy Monday afternoon? I'm giving a tutorial on bash at #OSCON Austin TX; I'd be happy to see you there. Introduce yourself during a break.
1
This Monday evening, live in Annapolis, Carl talks on bash at the Annapolis Linux User's Group. annapolislinux.org/
1
bash has a builtin printf so it's fast, like echo. Use printf -v MYVAR '%fmt' args to"print" into the variable MYVAR.
1
3
M=Merry;C=Christmas;for ((c=0;c<12;c++)); do echo $M $C; done # it's simple but says it well enough
1
4
Odd use of :+ to change a variable and echo its new value in one line: echo ${VAR:+${VAR,,}} converts it to l/c and echoes it, too.
1
More with "!": You can list all your shell variables starting with "SH" this way: echo "${!SH@}" use only literal characters not patterns.
3
${!name} is an indirect reference; if name=value then ${!name} is the same as ${value}. ${#} is the arg count; with 5 args ${!#} becomes $5
2
Only a few hours left: #CyberMonday : Save 50-60% on ALL video training & ebooks + more deals: oreil.ly/Cyber-Monday via @oreillymedia
A quick way to reference the last parameter given to your shell script: ${!#} So call myscript.sh a b c d e If you echo ${!#} you'd get "e"
1
Had a great time at SC'15 last week (sc15.supercomputing.org) - heard several people wish that users had better bash skills. Follow us!
Use [[ "123abc456" =~ ([0-9]*)([a-z]*)([0-9]*) ]] to parse a string via regex; echo ${BASH_REMATCH[0]} for what matched; pieces in [1], ..
1
2
What's the ONLY place in shell that uses regular expressions (regex)? It's the =~ comparison as in: if [[ $NUM =~ ^[0-9]{1,3}$ ]] ; then ...
1
Normally empty, CDPATH can be :-separated list of directories for when you type "cd path" (unless path is absolute). Try: CDPATH=".:~"
Do you type this a lot: cmd 2>&1 | other - to redirect both std out & std err into the pipe? Now there's a shorthand: cmd |& other
2
22
35
If you declare an integer, then you can use += to add to its value with or without the $(( )) around the right hand side - but no spaces!
1
1
You don't have to declare x as integer to do math; just put the expression inside dollar-double-parens as in: x=$((5+4)) ; echo $x gives: 9
1
1
Try this: x=5+4; echo $x and you get "5+4" of course. But first: declare -i x (which says x will hold integers) then echo $x gives: 9.
3
5
Been too long - time to re-start tweets on bash; there's always more to learn and handy short-cuts worth knowing.
1
Our book cover is pink so it's part of @OReillyMedia's Valentine's day promo - see the deal at: oreil.ly/1CoU3N5 HappyValentinesDay!
1