1

I've added a function to my bash profile:

function git-checkout-origin { git checkout -B $1 origin/$1; }
export -f git-checkout-origin

the function works fine, but everyonce in a while when running some command that executes commands in a shell I get this error:

sh: error importing function definition for `git-checkout-origin'

I'm not quite sure what's causing this, although it seems likely that sh & bash use different syntaxes for functions, or that the function is otherwise incompatible with sh.

I don't need the function in subshells, though the broken function isn't really breaking things either, but I'd like to get rid of the error.

1
  • 1
    BTW, if you put this code into shellcheck.net with a #!/bin/sh shebang, it'll point out both the issues identified in your answer. Commented Dec 13, 2017 at 18:36

1 Answer 1

1

It turns out that there are two ways in which my function wasn't compatible with sh:

Sign up to request clarification or add additional context in comments.

3 Comments

Also, sh isn't guaranteed to support exported functions at all; if your sh is provided by dash or ash, it won't.
BTW, consider adding quotes: git_checkout_origin() { git checkout -B "$1" "origin/$1"; } -- that way you don't get undesired behavior with odd IFS values (or the default IFS value with arguments that either contain spaces or can be interpreted as glob expressions).
...if you want to make a function available to noninteractive child-process instances of /bin/sh, the portable way to do that is to put the definition in a file, and export an environment variable ENV containing the name of that file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.