Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: get list of commands starting with a given string

Tags:

Is it possible to get, using Bash, a list of commands starting with a certain string?
I would like to get what is printed hitting <tab> twice after typing the start of the command and, for example, store it inside a variable.

like image 936
Paolo Tedesco Avatar asked Feb 04 '09 14:02

Paolo Tedesco


People also ask

How do I iterate through a string in bash?

Example-2: Iterating a string variable using for loopCreate a bash file named 'for_list2.sh' and add the following script. Assign a text into the variable, StringVal and read the value of this variable using for loop.

What does $() mean in bash?

Dollar sign $ (Variable) The dollar sign before the thing in parenthesis usually refers to a variable. This means that this command is either passing an argument to that variable from a bash script or is getting the value of that variable for something.

What is $_ in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.

How do I list all the types of commands in Bash?

In bash, ksh or zsh, typeset -f lists functions with their definitions. In bash, you can list all command names of any type with compgen -c. You can use compgen -A alias, compgen -A builtin compgen -A function to list commands of a specific type. You can pass an additional string to compgen to list only commands that start with that prefix.

How to check if string starts with character in bash script?

Bash check if string starts with character using if statement if..else..fi allows to make choice based on the success or failure of a command: #!/bin/bash input = "xBus" if then echo "Start with B" else echo "No match" fi Bash check if a variable string begins with # value

What is a string in Bash?

Bash String is a data type similar to integer or boolean. It is generally used to represent text. It is a string of characters that may also contain numbers enclosed within double or single quotes. Example: “geeksforgeeks”, “Geeks for Geeks” or “23690” are strings

How do I search for a specific command in Bash?

Type Ctrl R and then type part of the command you want. Bash will display the first matching command. Keep typing Ctrl R and bash will cycle through previous matching commands. To search backwards in the history, type Ctrl S instead.


2 Answers

You should be able to use the compgen command, like so:

compgen -A builtin [YOUR STRING HERE]

For example, "compgen -A builtin l" returns

let 
local 
logout

You can use other keywords in place of "builtin" to get other types of completion. Builtin gives you shell builtin commands. "File" gives you local filenames, etc.

Here's a list of actions (from the BASH man page for complete which uses 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.
like image 137
Jacob Mattison Avatar answered Sep 24 '22 09:09

Jacob Mattison


A fun way to do this is to hit M-* (Meta is usually left Alt).

As an example, type this:

$ lo

Then hit M-*:

$ loadkeys loadunimap local locale localedef locale-gen locate
  lockfile-create lockfile-remove lockfile-touch logd logger login
  logname logout logprof logrotate logsave look lorder losetup 

You can read more about this in man 3 readline; it's a feature of the readline library.

like image 20
porges Avatar answered Sep 24 '22 09:09

porges