I want to source a bash function from a string instead of a file at the command line, so instead of this:

source file.sh

I want to do something like:

source `cat <<EOF
   echo "nodejs version: $(node --version)"
   echo "r2g version: $(r2g --version)"
   echo "npm version: $(npm --version)"
   export -f some_func;

is this possible somehow?

  • Why jump through so many hoops? Just put the text into the code right there? – Jeff Schaller Dec 19 '18 at 2:12
  • yeah actually what might be convenient is to send it all to the clipboard – Alexander Mills Dec 19 '18 at 2:14
  • Why not define your function in a shell startup file? – Erwan Dec 19 '18 at 2:19
  • 1
    source <( cat <<END ... ), but it seem like an awfully fragile and awkward workflow. I won't write this as an answer because I think it's ugly. – Kusalananda Dec 19 '18 at 8:29
  • 1
    This is good because instead of "curl -L https:dom.top/install.sh | bash" which won't result in stuff affecting the current login shell, "source <( curl -L https:dom.top/install.sh )" will affect aliases and the likes in the shell after without saving the file to disk when you might prefer it private, so thanks. – Master James May 30 '19 at 8:45

None of these worked, not sure why:

#!/usr/bin/env bash

source <(echo "export foo=bar");
echo "$foo"

source <(cat <<< "export foo=bar");
echo "$foo"

source <(cat <<EOF
  export foo=bar


echo "$foo"

perhaps source and . cannot read from a file descriptor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.