4 ms·
The trouble is that bash is necessary to setup python. For instance, the lingua franca of Docker is bash (well sh, but you should obviously change that). I've
by disgruntledphd2 6y ago
The trouble is that bash is necessary to setup python. For instance, the lingua franca of Docker is bash (well sh, but you should obviously change that).
I've been resisting learning (any more) bash for a while now, but I think I'm going to have to. This looks like it could reduce some of my terror at the insanities of shell scripts (why does one even need two quote characters that do different things?)
- sethammons 6y agoOne set of quotes (double quotes) allows for interpolation of commands/variables. Try: $ echo ‘pwd’ $ echo “pwd”
- mauvehaus 6y agoYou're right, but it's neither of those for command interpolation. Double quotes enable variable expansion, single quotes do not. Command interpolation is back quotes, which is a carryover from sh. The more modern bash way is as follows: $ echo $(pwd) Incidentally: how does one type back quotes on an iPhone?! Edited to add: shellcheck [0] will flag the back quote usage if you're writing a bash script instead of a sh script. [0] https://www.shellcheck.net/ https://www.shellcheck.net/
- justusthane 6y agoLongpress single-quote.
- mauvehaus 6y agoThat ... lacks discoverability. `Thank you`!
- sethammons 6y agoLike most iPhone tips. How was I to discover long press space bar for cursor placement?! I like it, but discoverability...
- Tsiklon 6y agoIt was much better on the 3D Touch equipped models, press “through” the keyboard to get cursor placement, press “through” again while on a word to select the whole word.
- sethammons 6y agoThanks for the correction
- bewuethr 6y agoShellCheck always points out backticks as "legacy".
- unkulunkulu 6y agoRuby has two kinds of quotes as well (interpolating and non-interpolating), but that must be borrowed from sh
- HeckFeck 6y agoI'd wager Ruby borrowed from Perl, which was the original borrower from sh.
- wilsonrocks 6y agoJs has 3 quotes, of which 2 do the same, and the remaining one does two different things...
- disgruntledphd2 6y agoAnd that's another language I've been avoiding learning ;)
- pdkl95 6y agoedit: minor bugfix in example code A couple recommendations that make bash a much saner and avoid entire classes of problems: First, whenever you are expanding a list of args, use "$@"! That exact four character sequence expands to the properly quoted positional params ("$1" "$2" ...) with any necessary escaping included so each param expands as a single word. Almost all of the problems you've probably heard about bash "not handling spaces properly" or otherwise having problems with whitespace or strange characters in filenames are fixed by using "$@". If you're using arrays/hashes, you can get the same effect using "${somearrayorhash[@]}" (quotes included, just like "$@"). Removing the quotes or using the tradition $* is almost always a bug. Second, always use explicit quotes/brackets! Forget that they were ever optional. Using "$@" fixes most of the whitespace-in-filename problems; expanding your variables with explicit quotes fixes the rest. Assuming these: showargs() { echo "$# args" for i in "$@" ; do echo "arg[${i}]" done } declare -- name="filename with spaces\\!.txt" declare -A h='([a]="b c" [foo]="'\''bar'\'' \"baz\" qu*x" )' Instead of using the traditional shortcuts (which cause problems): showargs $name # 3 args # arg[filename] # arg[with] # arg[spaces\!.txt] showargs ${h[*]} # or ${h[@]} # 7 args # arg[b] # arg[c] # arg['bar'] # arg["baz"] # arg[qu*x] Always using quotes/brackets simply does the right thing: showargs "${name}" # 1 args # arg[filename with spaces\!.txt] showargs "${h[@]}" # 2 args # arg[b c d e] # arg['bar' "baz" qu*x] Bash still has it quirks and strange historical baggage, but in my experi4nce, using these two rules (and actually taking the time to read the bash(1) manpage...) changed writings shell scripts from an annoying mess of buggy arcane incantations into an actually sane(-ish) programming language.