bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: compgen -W doesn't split wordlist containing single quotes


From: Clark Wang
Subject: Re: compgen -W doesn't split wordlist containing single quotes
Date: Sat, 17 Mar 2018 12:16:24 +0800

On Sat, Mar 17, 2018 at 1:00 AM, <address@hidden> wrote:

>
> It works correctly if the single quote is itself quoted. Our test wordlist
> would then be: "foo\'bar aaa bbb"
>
> address@hidden:~/tmp$ compgen -W "foo\'bar aaa bbb" -- a
> aaa
> address@hidden:~/tmp$ compgen -W "foo\'bar aaa bbb" -- b
> bbb
> address@hidden:~/tmp$ compgen -W "foo\'bar aaa bbb" -- f
> foo'bar
>

This seems to work but it does not. For example in the command line

  # some-cmd f<TAB>

will become

  # some-cmd foo'bar

then you press ENTER and it'll still wait for another ' char.

Actually every word in the -W "wordlist" needs to be sh-quoted twice (with
``printf %q'' or the new address@hidden'' syntax). It'll be a bit easier if you
use an array. For example:

  # arr=(foo\'bar aaa bbb)
  # for ((i=0; i<address@hidden; ++i)); do arr[i]=$(printf %q "${arr[i]}"); done
  # for ((i=0; i<address@hidden; ++i)); do arr[i]=$(printf %q "${arr[i]}"); done
  # printf '%s\n' "${arr[*]}"
  foo\\\'bar aaa bbb
  # compgen -W "${arr[*]}" -- a
  aaa
  # compgen -W "${arr[*]}" -- f
  foo\'bar

Then in the command line

  # some-cmd f<TAB>

will become

  # some-cmd foo\'bar

and then you press ENTER and the command will be executed.

-clark


reply via email to

[Prev in Thread] Current Thread [Next in Thread]