I'm writing a setup file for some wrapper scripts, and mapping these scripts to completions for underlying commands.
In my .bash_profile, this maps git's completions to git_wrapper:
complete -F _git git_wrapper
In my setup.sh, the following does not work because the script is not interactive (type _git
fails because _git
is not available in non-interactive shells):
if type _git &>/dev/null && ! grep "complete.*_git git_wrapper" ~/.bash_profile &>/dev/null ; then
echo "complete -F _git git_wrapper" >> ~/.bash_profile
fi
From within setup.sh, how can I check if the _git completion would be available to an interactive shell?
From a script, how can I check if a function would exist in an interactive shell?
You can test whether a function would exist in an interactive shell by running an interactive shell (see below) and looking for it.
Note that "interactive" is a defined term here. On one hand, a shell is "interactive" if it is started without any non-option arguments and without the -c
option, and its standard input and error are both connected to terminals. That's natural, but not so useful for your purpose. On the other hand, however, a shell is also "interactive" if it is started with the -i
option, all other considerations aside. Your script can use that to perform your test in an interactive subshell, but perhaps more usefully, you can use that to make part or all of your script itself run in an interactive shell.