title: How to autocomplete a bash commandline with file paths from a specific directory without displaying this directory?
body: <p>You can find a related question here: <a href="https://stackoverflow.com/questions/44441249/how-to-autocomplete-a-bash-commandline-with-file-paths">How to autocomplete a bash commandline with file paths?</a></p>
  
  <h1>Context</h1>
  
  <p>I am creating a shell program which is a command line tool. I want to create my own auto-completion for this tool.</p>
  
  <p>For the options --unit-test and -t, I want to auto-complete on file paths from a particular directory which I can get running my_app --directory.</p>
  
  <p><strong>e.G.</strong></p>
  
  <p><strong>Run:</strong></p>
  
  <pre><code>user@computer:~$ my_app --install [TAB][TAB]
  </code></pre>
  
  <p>would do:</p>
  
  <pre><code>Public/          bin/                 Desktop/              
  Documents/       Music/               Downloads/
  user@computer:~$ my_app --install 
  </code></pre>
  
  <p>(display the current directory)</p>
  
  <p><strong>Run:</strong></p>
  
  <pre><code>user@computer:~$ my_app --unit-tests [TAB][TAB]
  </code></pre>
  
  <p>would do:</p>
  
  <pre><code>folder/              folder2/             folder3/
  .hidden_file         file.extension       file2.extension
  user@computer:~$ my_app --unit-tests 
  </code></pre>
  
  <p>(display suggestions for specific directory without complete with it)</p>
  
  <p><strong>my_app_autocomplete file</strong></p>
  
  <pre><code>__my_app_autocomplete()
  {
      local cur prev opts
      COMPREPLY=()
      cur="${COMP_WORDS[COMP_CWORD]}"
      prev="${COMP_WORDS[COMP_CWORD-1]}"
      opts="--help -h --install -i --run -r --rebuild -rb --show-running-containers -ps --stop -s --remove -rm --logs -l --bash -b --sass -css --unit-tests -t"
      containers="nginx php mysql mongo node"
      sass="watch"
  
      # By default, autocomplete with options
      if [[ ${prev} == my_app ]] ; then
          COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
          return 0
      fi
      # By default, autocomplete with options
      if [[ ${cur} == -* ]] ; then
          COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
          return 0
      fi
      # For --install and -i options, autocomplete with folder
      if [ ${prev} == --install ] || [ ${prev} == -i ] ; then
          COMPREPLY=( $(compgen -d -- ${cur}) )
          return 0
      fi
      # For --stop --remove --logs and --bash, autocomplete with containers
      if [ ${prev} == --stop ] || [ ${prev} == -s ] || [ ${prev} == --remove ] || [ ${prev} == -rm ] || [ ${prev} == --logs ] || [ ${prev} == -l ] || [ ${prev} == --bash ] || [ ${prev} == -b ] ; then
          COMPREPLY=( $(compgen -W "${containers}" -- ${cur}) )
          return 0
      fi
      # For --sass and -css, complete with sass options
      if [ ${prev} == --sass ] || [ ${prev} == -css ] ; then
          COMPREPLY=( $(compgen -W "${sass}" -- ${cur}) )
          return 0
      fi
      # For --unit-tests and -t, complete from a specific folder
      if [ ${prev} == --unit-tests ] || [ ${prev} == -t ] ; then
          COMPREPLY=( $(compgen -d -- ${cur}) )
          return 0
      fi
  }
  complete -o filenames -F __my_app_autocomplete my_app
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>I can't find a way to do it. Do you have any ideas?</p>
  
  <h1>Investigations</h1>
  
  <h2>Using a variable containing the specific directory</h2>
  
  <p>Suggested by @D'Arcy Nader</p>
  
  <p>Adding at the beginning of <code>my_app_autocomplete</code></p>
  
  <pre><code>_directory=/absolute/path/to/the/directory/  
  </code></pre>
  
  <p>and then substitute the variable in the compgen command</p>
  
  <pre><code># For --unit-tests and -t, complete with relative to my_app folder paths
  if [ ${prev} == --unit-tests ] || [ ${prev} == -t ] ; then
      COMPREPLY=( $(compgen -d -- "${_directory}") )
      return 0
  fi
  </code></pre>
  
  <p><strong>Behavior:</strong></p>
  
  <p><strong>Run</strong></p>
  
  <pre><code>user@computer:~$ my_app --unit-tests [TAB][TAB]
  </code></pre>
  
  <p>do</p>
  
  <pre><code>user@computer:~$ my_app --unit-tests /absolute/path/to/the/directory/
  </code></pre>
  
  <p>It adds the path to the directory.</p>
  
  <p><strong>Run</strong></p>
  
  <pre><code>user@computer:~$ my_app --unit-tests /absolute/path/to/the/directory/file.ext[TAB][TAB]
  </code></pre>
  
  <p>do</p>
  
  <pre><code>user@computer:~$ my_app --unit-tests /absolute/path/to/the/directory/
  </code></pre>
  
  <p>It removes the <code>file.ext</code> part.</p>
  
  <p><strong>Problems:</strong></p>
  
  <ul>
  <li>I don't want to add the specific path in the command line</li>
  <li>It removes what I add after the specific directory instead of auto-complete it.</li>
  </ul>