On Wed, Aug 17, 2016 at 02:32:32AM -0300, Beco wrote:
> This alias caused bash to crash:
> 
> http://sprunge.us/PMhe

Please don't use paste sites when communicating with a mailing list.
This isn't IRC.  Just include all of the necessary information in your
email.

> The alias is now corrected to check if `$1` is empty (*), but still, bash
> should not crash like this.

Bash aliases DO NOT accept arguments.  $1 has no special meaning in an
alias.  It will simply use the shell's positional parameters.  For an
interactive shell, these are usually empty.

> PS. All I did was calling the alias without an argument (a file to open)

Again, aliases don't take arguments.

> alias mdless='_mdless() { if [ -n "$1" ] ; then if [ -f "$1" ] ; then cat
> <(echo ".TH $1 7 `date --iso-8601` Dr.Beco Markdown") <(pandoc -t man $1 )
> | groff -K utf8 -t -T utf8 -man 2>/dev/null | less ; fi ; fi ;}; _mdless '

Why are you creating a function and then creating an alias to call the
function?  Just make a function in the first place.

unalias mdless 2>/dev/null
mdless() {
  if [[ "$1" && -f "$1" ]]; then
    cat <(echo ".TH $1 7 $(date --iso-8601) Dr.Beco Markdown") \
        <(pandoc -t man "$1") |
      groff -K utf8 -t -T utf8 -man 2>/dev/null |
      less
  fi
}

Now, personally I wouldn't use cat <(echo ...).  It's a bit unwieldy for
my taste.  I would prefer something simpler, like:

unalias mdless 2>/dev/null
mdless() {
  if [[ "$1" && -f "$1" ]]; then
    { echo ".TH $1 7 $(date --iso-8601) Dr.Beco Markdown"
      pandoc -t man "$1"
    } | groff -K utf8 -t -T utf8 -man 2>/dev/null | less
  fi
}

Either way, my primary lesson for you is to lose this silly notion that
you should be making aliases.  Aliases are only useful in trivial cases
like alias ll="ls -l" .  For anything more complex, use a function.

The secondary lesson is simplify, simplify, simplify.  Don't use multiple
process substitutions when you can get by with a simple command grouping.
(Also, strength reduction in general.  Don't use a regex if a glob will
do.  Don't use a glob if a string comparison will do.  Don't use a string
comparison if an integer comparison will do.  And so on.)

If you've simplified everything as far as possible and you are still able
to make bash crash, then please report it as a bug.  (It's possible that
the crash was related to the unnecessary use of process substitutions, and
therefore removing those may simply make it go away.)

Reply via email to