Live data from Hacker News

Things I Wish I'd Known About Bash

zwischenzugs.com

21–30 of 272 posts

Re: Things I Wish I'd Known About Bash

#21
post #15

Can someone explain :h as the article's description was not clear at all to me what was going on there.

Yeah, I couldn't make that work for me (on Linux or MacOS) .. although I'd love it if there were a way to quickly get 'just the directory' or 'just the filename' in bash with a shortcut, instead of having to resort to $(dirname blah) and so on .. I'm sure there is some way but :h doesn't look to be the shortcut as expected.

Assuming blah is in a var named name, you can use:

    dirname => "${name%/*}"
    basename => "${name##*/}"
Sadly you can't use it with !$ since it's not a variable. The closest you can do is:

    $ ls foo/bar/baz
    ls: foo/bar/baz: No such file or directory
    $ last=!$; echo "${last##*/}"
    baz
    $ echo "${last%/*}"
    foo/bar

Re: Things I Wish I'd Known About Bash

#22
post #18

Earlier quoted context omitted.

https://fishshell.com/docs/current/commands.html#psub

Thanks a lot. Shells have so many features it's easy to miss one.

Watch out, there are some limitations to fish's psub preventing it to work as bash's >()

https://github.com/fish-shell/fish-shell/issues/1786

Re: Things I Wish I'd Known About Bash

#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 the command name.  There are two forms:

              $(command)
       or
              `command`
    (detailed description follows)
I'm still in favor of using more verbose and especially more clear constructs, but not because they are easier for Google, but because they ideally hold enough information on their own that you don't even need to look it up to know what it does.

Re: Things I Wish I'd Known About Bash

#24
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…

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 are so many sites that start with a problem and then explain the tools.

Re: Things I Wish I'd Known About Bash

#25

Earlier quoted context omitted.

For portability. bash is not available everywhere, but POSIX sh is a requirement of POSIX so you can expect it to be there.

Exactly: GNU bash is on most GNU/Linux systems, but: * mac OS uses an ancient version of bash * Embedded systems use busybox sh * Android uses its own version of sh * None of the BSDs come with bash, since, y'know GNU licensing EDIT: Formatting

And to make things interesting Ubuntu uses dash as sh

https://wiki.ubuntu.com/DashAsBinSh

Re: Things I Wish I'd Known About Bash

#26

Can someone explain :h as the article's description was not clear at all to me what was going on there.

Wild guess: perhaps OP confused bash syntax with vim syntax, where :h removes the last component?

http://vimdoc.sourceforge.net/htmldoc/cmdline.html#filename-...

Re: Things I Wish I'd Known About Bash

#27

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 ?

Re: Things I Wish I'd Known About Bash

#28
post #24
post #23

Earlier quoted context omitted.

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…

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.

Re: Things I Wish I'd Known About Bash

#29
+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 thousands of lines of shell to track down why something doesn't work on the stupid AIX box.

Re: Things I Wish I'd Known About Bash

#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.
Post reply on HN