I have a command that can accept absolute and relative paths (just like ls) as first argument

# absolute
mycommand $HOME/.local
mycommand /tmp/
# relative
mycommand ../build
mycommand file_in_cwd.txt

reasonable completions for the second argument depend on the first argument. My draft for it looks like that:

__mycommand_secondarg() {
  if [[ $words[2] = \$* || $words[2] = /* ]]; then
    # do something
    # do something else

I'm wondering, did I miss relevant cases here? I thought about arguments that start with a slash /, a dollar $ (I assume this will most likely be an environment variable like $HOME that is an absolute path, if it's a subshell, substitution, or variable that will be resolved to a relative path, then I accept this as too complex for now), one or two dots ., or a somewhat normal character (maybe escaped).

Also, is there maybe a completion helper function that can tell me if a words[…] is an absolute or relative path?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.