Questions tagged [compgen]

A bash built-in command to generate command completions.

compgen is a built-in command used to define automatic tab-completion behaviour for options, filenames and other positional arguments to shell commands.

It's intended to be used from within a shell function generating possible completions, which is then used by the complete builtin to define a completion specification or compspec.

See also: Bash Reference Manual: Programmable Completion

28 questions
19
votes
1 answer

How do I autocomplete nested, multi-level subcommands?

I am trying to develop an autocomplete or tab-complete feature for my own set of commands. For example, assume foo is my binary: CLI>> foo [TAB] [TAB] It should show the main commands configure and…
Puneeth
  • 419
  • 1
  • 6
  • 18
8
votes
3 answers

Listing all variables in a file in bash

When writing bash scripts I like to write self contained functions which take an argument and perform operations based on that/those argument(s) rather than have global variables being declared in several different places in the code decreasing…
Alexandre Thenorio
  • 2,288
  • 3
  • 31
  • 50
6
votes
1 answer

Command to list all available commands in Windows Command Prompt

I’d like to programmatically get a list of all the available commands in Windows Command Prompt (cmd.exe). Is there something like compgen -c in Bash, but for Windows?
Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
5
votes
1 answer

specify another directory to compgen for auto completing directories, than using pwd

compgen -d -- $cur will suggest completions for the directories in pwd only right? Can I get it to suggest directory completions for some dir while being in another dir. For eg. if I am in /curr_dir, and I want to generate directory completions for…
udiboy1209
  • 1,472
  • 1
  • 15
  • 33
4
votes
3 answers

Simulate Bash's COMPREPLY response without actually completing it

Steps to reproduce Create directory on tmp, add 1 file inside. mkdir /tmp/testdir && touch /tmp/testdir/examplefile Paste below script on /tmp/completion.sh # BEGIN AUTOCOMPLETE function _foo_complete() { local comnum cur opts [[…
Liso
  • 188
  • 2
  • 14
4
votes
3 answers

Can code injection be done using eval in this code?

This is my first time using bash and I know that eval have some risks when using it. Can you do a code injection to this ? For example I want to run ls command and see the files. #!/bin/bash echo "[*] Please enter the name:" echo -n "> " read…
Fish
  • 53
  • 4
4
votes
1 answer

autocomplete boost program options

after hours of search I ended up writing my first stackoverflow question. I want to create a completion function for a bash script, so far so good ;). This bash script calls other executables that have their own autocompletion. Example: $ my_script…
3
votes
1 answer

how does bash do `ssh` autocompletion?

I've got the "bash-completion" package installed. ssh completion on the command line (in bash) is working: ssh TAB-TAB will complete past used hosts and ssh -TAB-TAB will complete available ssh options. However when I search for currently defined…
3
votes
0 answers

Is it possible to make a bash script that delegates completion to another program?

I'm trying to write a wrapper script around another program (e.g. foo_wrapper around foo), which simply overrides some flags on every call to foo. I want users to be able to interact with foo_wrapper and foo exactly the same way, including seeing…
karagog
  • 138
  • 1
  • 6
3
votes
2 answers

Subprocess library won't execute compgen

I am trying to make list of every command available on my linux (Lubuntu) machine. I would like to further work with that list in Python. Normally to list the commands in the console I would write "compgen -c" and it would print the results to…
xlogic
  • 1,509
  • 2
  • 11
  • 9
2
votes
0 answers

Globbing filenames from file using compgen

I'm simplifying a messy Bash script that synchronizes directories and their contents and files among several machines on a network using rsync. The script gets file names, directory names, and glob patterns from a sources.txt file with contents like…
Al Pacifico
  • 800
  • 5
  • 17
2
votes
1 answer

How does this bash completion function work? (for cloudfoundry cli)

I try to understand this bash completion function ? (for cloudfoundry cli) I read https://debian-administration.org/article/317/An_introduction_to_bash_completion_part_2 and also the bash ref guide at…
mhoff
  • 601
  • 8
  • 13
2
votes
1 answer

How to make bash autocomplete work in the middle of line?

How to write autocomplete in bash, so that if I have: mycommand first_argument|garbage where | denotes the cursor, it should pass "first_argument" and not "first_argumentgarbage" to compgen? In the examples I have it behaves in the wrong…
pihentagy
  • 5,975
  • 9
  • 39
  • 58
2
votes
1 answer

Use compgen to get autocomplete for another command, which flag to use?

I have a custom auto-completed command (call it commandA) in commandB I want to steal the autocomplete options for the first argument to commandA So, for example the options for argument1 for commandA are: abcdef abcabc abc123 I would like something…
Chris
  • 377
  • 2
  • 11
1
vote
2 answers

Compgen doesn't complete words containing colons correctly

I'm encountering a problem with creating a Bash completion function, when the command is expected to contain colons. When you type a command and press tab, Bash inserts the contents of the command line into an array, only these arrays are broken up…
Lou
  • 2,200
  • 2
  • 33
  • 66
1
2