0

If I have a program located in /a/prog and /a is not in PATH, is it possible to specify a bash completion for running ./prog in /a so that it doesn't affect any other program named prog located anywhere else?

To clarify, here's an example:

_complete_prog() { [ "$COMP_CWORD" == 1 ] && \ COMPREPLY=($(compgen -W "aaa bbb" "${COMP_WORDS[1]}")) } complete -F _complete_prog ./prog 

This will make only the first argument of ./prog tab complete as follows

$ ./prog <TAB><TAB> aaa bbb 
  1. I don't want this completion to apply for ./prog if that's a different file, e.g. /b/prog.

  2. I want for the same completion to apply for /a/prog and ././prog or any other path that leads to the same file.

4
  • Stack Overflow is for programming questions, not questions about using or configuring Unix and its utilities. Unix & Linux or Super User would be better places for questions like this. Commented Nov 12, 2024 at 17:09
  • Bash completions are programmable. This is a bash programming question. Commented Nov 12, 2024 at 17:27
  • Most customization of bash completion doesn't require programming, it's just using built-in pattern matching options. If this turns out to be more complicated, then feel free to reopen this question. Commented Nov 12, 2024 at 17:28
  • It's not really clear to me what you're asking for. Do you want your shell to respond to ./pr<tab> by expanding to ./prog? By default, bash already does that (if prog has the executable bit set). Maybe you just need to chmod +x /a/prog to get the behavior you want. Commented Nov 13, 2024 at 15:40

1 Answer 1

1

Just like this:

$ complete -W 'aaa bbb' foo $ complete -W 'ccc ddd' ./foo $ complete -W 'eee fff' /a/foo $ foo <TAB><TAB> aaa bbb $ ./foo <TAB><TAB> ccc ddd $ /a/foo <TAB><TAB> eee fff 

As we can see, Bash completion can distinguish between foo and ./foo.

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

2 Comments

The problem is that ./foo and /a/foo have to be specified separately. Is there a way to have the same completion for the program file regardless of what path is used to run it? For example, so that ./foo and ././foo behave the same.
i dont think that's possible. for example, with compete -d cd, bash even cannot handle "cd" <TAB><TAB>.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.