Live data from Hacker News

Things I Wish I'd Known About Bash

zwischenzugs.com

31–40 of 272 posts

Re: Things I Wish I'd Known About Bash

#31
post #23
post #20

Bash has a huge number of little shortcuts that are difficult to learn. When one encounters a sequence of symbols like $(...), it is difficult to Google for its meaning. The reason shells nevertheless have these shortcuts is of course because they are shells: from the commandline it can be very convenient to use shortcuts. But, in my opinion, that's where it should stop: one shouldn't use a shell language for scripti…

Yeah, but why are you trying to use a search engine that cares less and less about exact matches, when there's a manual? >man bash /\$\( # search pattern needs escaping ... value is evaluated as an arithmetic expression even if the $((...)) expansion is not used (see Arithmetic Expansion below). Word split‐ ... n # go to next match Command Substitution Command substitution allows the output of a command to replace th…

It is a reference, and searching for the pattern GP mentioned finds you references to arithmetic expression evaluation and to command substitution. Those are the terms one can Google for if further explanation is required.

Re: Things I Wish I'd Known About Bash

#32

+1 for the parts that are portable to Bourne shell/ksh/zsh. -1000 for the parts that are specific to Bash. Stuff like that has been huge pain in my ass over the years. Some clever programmer uses some bash-ism and the build breaks on some ancient hardware that doesn't have bash. I realize my complaint sounds like Henry Spencer's Ten Commandments and perhaps it feels outdated but trust me, you don't want to wade into…

The article doesn't mention which are portable and which aren't.

Re: Things I Wish I'd Known About Bash

#33
post #24

Earlier quoted context omitted.

Man pages are specifically reference documents, not tutorials or guidebooks. To say one should use a man page is to say that one must completely digest the entirety of the tool prior to ever actually using it. That’s just simply not feasible, nor should it be expected of anyone beyond trivial tools. Man pages simply don’t provide the context for solving a problem like a guidebook or tutorial would, which is why there…

Which is why the parent advised to treat the man page like a reference document, by searching in it. Some man pages are just badly written and are indigestible even when searching for a specific thing, but in general, that approach works quite often.

And even in the worst case, the man pages give you more context to use in your subsequent web search.

To use the original example, once you've identified that $(...) is Command Substitution, you have something that pulls up meaningful results in every search engine.

Re: Things I Wish I'd Known About Bash

#34
post #7

..it'd be nice if what was actually happening was explained of just statements like "[ is the original form for tests, and then [[ was introduced, which is more flexible and intuitive"

The primary differences are:

* word splitting and pathname expansion don't happen in [[...]];

* "==" does pattern matching in [[...]], but it does string comparison in [...].

Re: Things I Wish I'd Known About Bash

#35
post #7

..it'd be nice if what was actually happening was explained of just statements like "[ is the original form for tests, and then [[ was introduced, which is more flexible and intuitive"

I recommend reading `man bash` for the section on Conditional Execution. It's remarkably readable and useful for man pages. It also cleanly explains the difference between the two.

Re: Things I Wish I'd Known About Bash

#38

Hang out in #bash on IRC Freenode and you will be a Bash jedi http://mywiki.wooledge.org/BashFAQ best resource IMO for quick Bash syntax lookups as I always need to refer to the BashFaq to remember parameter expansion sub-string retrieval. parameter result ----------- ------------------------------ $name polish.ostrich.racing.champion ${name#*.} ostrich.racing.champion ${name##*.} champion ${name%%.*} polish ${name%.…

Extending your example above, how would you get a result of racing.champion or polish.ostrich ?

You'd have to do it twice, using a temporary variable:

    $ name="polish.ostrich.racing.champion"
    $ temp="${name#*.}"
    $ echo "${temp#*.}"
    racing.champion
That's if you want it generic (i.e., splitting on "." characters). In one shot, you could make the pattern more specific:

    $ echo "${name#*ostrich.}"
    racing.champion
...or use something like sed or awk:

    $ awk 'BEGIN {FS=OFS="."} {print $(NF-1), $NF}' 

Re: Things I Wish I'd Known About Bash

#39
post #30

I wrote a book on Bash too. The most important thing for anyone to know about Bash is that it's intended as a command language, not a general purpose scripting language. If it's longer than 10 lines, or if it uses two or more variables, you should probably have written it in something other than Bash.

Amen to that. Now if only I could have gotten my Systems Programming professor to feel the same way... :)

Re: Things I Wish I'd Known About Bash

#40

Hang out in #bash on IRC Freenode and you will be a Bash jedi http://mywiki.wooledge.org/BashFAQ best resource IMO for quick Bash syntax lookups as I always need to refer to the BashFaq to remember parameter expansion sub-string retrieval. parameter result ----------- ------------------------------ $name polish.ostrich.racing.champion ${name#*.} ostrich.racing.champion ${name##*.} champion ${name%%.*} polish ${name%.…

Extending your example above, how would you get a result of racing.champion or polish.ostrich ?

    ${name%.*.*}   polish.ostrich
    ${name#*.*.}                  racing.champion
Post reply on HN