惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
IT之家
IT之家
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
V
V2EX
N
Netflix TechBlog - Medium
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
量子位
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
月光博客
月光博客
F
Fortinet All Blogs

物灵

A non-gamer’s view on games ◎物灵 I am a ruin aborigine ◎ 物灵 The spreadsheet-makers ◎物灵 وِسَ ڠ دَاِقࣨ؟ ◎物灵 领有和非领有的 C++ range ◎物灵 Do not get gentle into your CS major ◎物灵 ཡིའུ་ཀང་ལག་ ◎ 物灵 슈홋(守候) 2038넌(年) ◎物灵 Buku-name de Mini kon Sina-litera ◎物灵 ‘My Path’ after twenty years ◎物灵 My linguistic history ◎物灵 Enhypen Alphabet ◎物灵 GNU 四十年 ◎物灵 ‘If need be, I will leave w/o looking back…’ ◎物灵 omekapo, Lánzhōu ⟲物灵 说说选登 ◎ 物灵 Back in old Ruyqiuan ◎物灵 linluwi lili li pona a! ⟲物灵 「挺」的中古三字 ◎物灵 最后一天 ◎物灵 基于 RFID 及智能终端的户外团队运动系统 ◎物灵 Which character for inbound passengers? ◎物灵 Kokanu in lika Hanci ◎物灵 Number of shapes of Rubik’s snake ◎物灵 دࣱخُاْ ۋاًخُوَاتࣨ ◎物灵 Childhood ◎物灵 个人视觉识别系统(辛丑) ◎物灵 ((报告 书) = (书 (报告 书)) ◎物灵 Speech before final examination ◎物灵 第 34 届山西科创记 ◎物灵
A slice of Bash programmable completion ◎物灵
Unknown · 2025-05-02 · via 物灵

During the process of migrating a Conda environment and mitigating confliction, I noticed Bash programmable completion was experiencing an issue when the environment was not yet incomplete. I seemingly showed no curiousity, proceeding to enter the command despite STDERR for the rest of migration. This post was created after that since there are no Marginalia search engine-style sources on Bash programmable completion.

Context-dependent completion is easy to spot. Git, the first instance coming to my mind, completes options and hash strings when you are hasty writing down an arbitrary hexadecimal ID. For practical scripts, completion functions serve as a valuable aid in recalling details of commands.

The builtin command complete determines how arguments are to be completed by Readline. In the absence of arguments, it outputs available completion functions in a ready-to-load format. To relate a function to a command, use

complete -F <completion_function> <command>

Here is an example of completion function:

_mycmd_completion() {
    # retrieve words on the current line
    local cur prev
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    # complete according to the previous word
    case "$prev" in
        mycmd)
            COMPREPLY=( $(compgen -W "start stop" -- "$cur") )
            ;;
        *)
            COMPREPLY=()
            ;;
    esac
}
# enable the completion function
complete -F _mycmd_completion mycmd

The function retrieves precedent words from variables. The current line, COMP_LINE is split into words in COMP_WORDS. In the function, the target is the last word indexed as COMP_CWORD. Use COMP_LINE for the word being edited.

compgen is another builtin function that generates completions, intended to be used in a completion function. The options of compgen denote the type and lexical criteria of completion, as exemplified in the table:

ExampleUsage
compgen -f testList all filenames starting with test
compgen -d testList all directories starting with test
compgen -c gitList all commands starting with git
compgen -W "john jane joan jojo" -- joList words from the word list starting with 'jo'
compgen -W "john jane" -P "cmd:" -S "!"Adding uniform prefixes and suffixes

Therefore, the expression above completes start and/or stop according to the partially typed word. You can also pipe compgen output to grep to further filter the results.

In the end, the completion function is attached to a command.

For Python scripts, packages like shtab, argcomplete, and prompt_toolkit are responsible for completions. click, an alternative to builtin argument parsing library argparse, is a versatile tool supporting completion.

Using compgen and complete in Bash, you may create completion rules for filenames, commands, options, and more. Custom completion allows you to enhance the usability of command-line tools and scripts. Fish offers more nuanced completion generators invoked in a similar procedure; you may read the super-friendly documentation.