0

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.

cwohlman
  • 763
  • 6
  • 21
  • 1
    BTW, if you put this code into http://shellcheck.net/ with a #!/bin/sh shebang, it'll point out both the issues identified in your answer. – Charles Duffy Dec 13 '17 at 18:36

1 Answers1

1

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

cwohlman
  • 763
  • 6
  • 21
  • Also, `sh` isn't guaranteed to support exported functions at all; if your sh is provided by `dash` or `ash`, it won't. – Charles Duffy Dec 13 '17 at 18:01
  • 1
    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). – Charles Duffy Dec 13 '17 at 18:01
  • ...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. – Charles Duffy Dec 13 '17 at 18:04