0

I want to wrap a function with its full path.

$ pwd
/home/me

$ which pwd
/bin/pwd

$ function /bin/pwd() { 
echo "wrapping pwd" 
command /bin/pwd
} && export -f /bin/pwd

$ /bin/pwd 
wrapping pwd
/home/me

$ bash -c "/bin/pwd"
bash: error importing function definition for `BASH_FUNC_/bin/pwd'
/home/me

Is there a way to successfully export functions named after some path? First export does not complain, but the exports in sub-shells do. The end goal is to set environment variables before launching the actual command.

1

A function name can only contain alphanumeric characters and underscores, and must begin with an alphabetic character or an underscore.

  • So there is no workaround to wrapping the full path of a command in sub-shells? – kaligne Sep 23 '16 at 12:54
  • @user3298319 You seem to be confusing the concepts of a "function" and a "program living on the file system". A program needs a full path to find it on the file system. A function has no reason for a full path, because it's built/added into the shell itself. On the other hand, when you use a full path, then you are going to get the program from the file system. – hymie Sep 23 '16 at 14:16
  • Ok but I don't understand why the export of my full-path function works in the current shell and not in the sub-shell. – kaligne Sep 23 '16 at 14:44
  • 1
    The posix standard says a shell "may allow other characters in a function name as an extension" -- apparently bash allows slashes! Except they're weird enough that inheritance (with export) doesn't work right. But it's amazing that they work at all! – Gordon Davisson Sep 24 '16 at 3:51
  • @GordonDavisson You should add that as an answer. – wjandrea Sep 24 '16 at 16:35

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.