bash:获取以给定字符串开头的命令列表

Pao*_*sco 15 linux bash scripting

是否有可能使用Bash获得以某个字符串开头的命令列表?
我想在输入命令的开头后得到打印<tab>两次的内容,例如,将其存储在变量中.

Jac*_*son 33

您应该能够使用compgen命令,如下所示:

compgen -A builtin [YOUR STRING HERE]
Run Code Online (Sandbox Code Playgroud)

例如,"compgen -A builtin l"返回

let 
local 
logout
Run Code Online (Sandbox Code Playgroud)

您可以使用其他关键字代替"builtin"来获得其他类型的完成.Builtin为您提供shell内置命令."文件"为您提供本地文件名等.

这是一个动作列表(来自完整的BASH手册页,它使用compgen):

  alias      Alias names.  May also be specified as -a.
  arrayvar   Array variable names.
  binding    Readline key binding names.
  builtin    Names  of  shell builtin commands.  May also be specified as -b.
  command    Command names.  May also be specified as -c.
  directory  Directory names.  May also be specified as  -d.
  disabled   Names of disabled shell builtins.
  enabled    Names of enabled shell builtins.
  export     Names of exported shell variables.  May also be specified as -e.
  file       File names.  May also be specified as -f.
  function   Names of shell functions.
  group      Group names.  May also be specified as -g.
  helptopic  Help topics as accepted by the help builtin.
  hostname   Hostnames, as taken from the file specified by the HOSTFILE shell
                 variable.
  job        Job  names, if job control is active.  May also be specified as
                 -j.
  keyword    Shell reserved words.  May also be specified as -k.
  running    Names  of  running  jobs,  if  job  control  is active.
  service    Service names.  May also be specified as -s.
  setopt     Valid arguments for the -o option  to  the  set builtin.
  shopt      Shell  option  names  as  accepted by the shopt builtin.
  signal     Signal names.
  stopped    Names  of  stopped  jobs,  if  job  control  is active.
  user       User names.  May also be specified as -u.
  variable   Names  of  all  shell  variables.   May also be specified as -v.
Run Code Online (Sandbox Code Playgroud)