Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:53:56+00:00 2026-06-07T15:53:56+00:00

The following bash completion passes an array of possible words (i.e. completions) to compgen.

  • 0

The following bash completion passes an array of possible words (i.e. completions) to compgen.

basenames=("foo" "fu bar" "baz");

COMPREPLY=($(compgen -W "${basenames[*]}" -- "${COMP_WORDS[COMP_CWORD]}"))

The problem is that the whitespace in the array elements is not preserved, that is “foo bar” is treated as to elements thanks to word splitting. Is there some way to preserve the whitespace, so that 3 elements are shown, and not 4?

EDIT

basenames contains filenames, that is nearly every character (apart from / and \0) is allowed.

EDIT 2

The -W flag expects a single word, that is something like foo bar foobar. Passing multiple elements to it (this is what ${basenames[@]} would do) won’t work.

EDIT 3

Changed examplary basenames array (so the foo and the foo from foo bar won’t get collapsed).

Using a newline to separate the words works:

local IFS=$'\n'
COMPREPLY=($(compgen -W "$(printf "%s\n" "${basenames[@]}")" --  ${COMP_WORDS[COMP_CWORD]}"))

Using \0 doesn’t:

local IFS=$'\0'
COMPREPLY=($(compgen -W "$(printf "%s\0" "${basenames[@]}")" --  ${COMP_WORDS[COMP_CWORD]}"))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:53:59+00:00Added an answer on June 7, 2026 at 3:53 pm

    Why bother with compgen? Just add them to COMPREPLY manually. The following will complete the matching filenames from /some/path, handling filenames safely.

    some_completion_function() {
        local files=("/some/path/$2"*)
        [[ -e ${files[0]} ]] && COMPREPLY=( "${files[@]##*/}" )
    }
    

    It’s not possible to have compgen handle filenames safely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following bash script: #DIR is something like: /home/foo/foobar/test/ without any whitespace
I added bash completion for Maven following the docs: http://maven.apache.org/guides/mini/guide-bash-m2-completion.html Everything works well except
Possible Duplicate: The following bash command will spawn processes to kernel death. Can you
I get the following in including MacPorts' Bash tab completion to Zsh /opt/local/etc/bash_completion:[:48: unrecognized
Say I have the following Bash script stored in the file foo.sh : #!/bin/bash
I have the following in my .bashrc file: # Git Bash Completion source $HOME/.git_completion
under Debians BASH I can do the following: foo=/path/to/some/file cat $f[TAB] As you can
Possible Duplicate: Properly handling spaces and quotes in bash completion I would like to
I use the following bash script to copy only files of certain extension(in this
I've got following bash script to do something for each parameter of the script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.