Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check from within bash script that autocompletion is initialized

I'm looking for a method to check from within my shell script that script specific completion have been initialized by user using complete -F ...

I want this check to print out an advice on how to initialize the completion like:

Warning: Auto completion is not initialized. Please run : source ....; complete -F ...

The problem is that the script,being run in a sub-shell has no information about "complete" environment of the parent shell where user is working.

So complete -p| grep my-script-name never return any result.

User is expected to run "source" and "complete" commands or add them into his .bashrc manually, because we're working on a server where we have no access to the bash completion system directory.

Alternatively if you know a method of initializing(and not only checking) the auto-complete from within the script, I would happily accept it.

like image 971
LiMar Avatar asked Jun 08 '26 04:06

LiMar


1 Answers

The only way your script can have access to such information for the parent shell is if it is included instead of executed as a sub-shell. Rather than instructing your users they can include some configuration, you can design your script so it works whether it is run or included.

Then you can simply inform your users that if they want completion enabled they need to include your script rather than run it as an executable script (with instuctions to use source or . as you wish).

However, in this case, I would be inclined to either add this information in documentation or add it into a banner which is always displayed (but can be disabled with an option switch like -q), rather than support two modes of running the script (since the gain is so small).

like image 194
sillyMunky Avatar answered Jun 10 '26 06:06

sillyMunky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!