Hacker News new | ask | show | jobs
by metadat 599 days ago
Functions don't work everywhere. Bash functions only work in the current shell context unless exported via an `export -f myfun' statement in between the function declaration and downstream sub-shell usage.

Working example:

  pzoppin() {
      printf 'echo is for torrent scene n00bs from %s\n' "$*"
      trap "printf '%s\n' <<< \"$*\"" RETURN EXIT SIGINT SIGTERM
  }
  export -f pzoppin

  echo -e 'irc\0mamas donuts\0starseeds' \
      | xargs -0 -n 1 -I {} /usr/bin/env bash -c '
  echo hi
  pzoppin "$*"
  echo byee
  ' _ {}
The above will fail miserably without the magic incantation:

  `export -f pzoppin'
Why'd they design an otherwise perfectly usable, mapless language without default c-style global functions? :)