- How do I get a list of all available shell commands
- 10 Answers 10
- How to get list of commands used in a shell script?
- 4 Answers 4
- List all commands that a shell knows
- 7 Answers 7
- Methods to List All Available Commands and Aliases in Linux
- 1. How to use the .bashrc to List All Available Commands and Aliases in Linux ?
- 2. How to use the built in shell library function to List All Available Commands and Aliases in Linux ?
- [Need Linux system support ? We can help you . ]
- Conclusion
How do I get a list of all available shell commands
In a typical Linux shell (bash) it is possible to to hit tab twice, to get a list of all available shell commands. Is there a command which has the same behaviour? I want to pipe it into grep and search it.
10 Answers 10
You could use compgen. For example:
You also could grep it, like this:
You can list the directories straight from $PATH if you tweak the field separator first. The parens limit the effect to the one command, so use: (. ) | grep .
«tab» twice & «y» prints all files in the paths of $PATH. So just printing all files in PATH is sufficient.
Just type this in the shell:
This redirect all the commands to a file «my_commands».
List all the files in your PATH variable (ls all the directories in the PATH). The default user and system commands will be in /bin and /sbin respectively but on installing some software we will add them to some directory and link it using PATH variable.
There may be things on your path which aren’t actually executable.
#!/bin/sh for d in $; do for f in "$d"/*; do test -x "$f" && echo -n "$f " done done echo ""
This will also print paths, of course. If you only want unqualified filenames, it should be easy to adapt this.
Funny, StackOverflow doesn’t know how to handle syntax highlighting for this. 🙂
Similar to @ghoti, but using find:
#!/bin/sh for d in $; do find $d -maxdepth 1 -type f -executable done
Bash uses a builtin command named ‘complete’ to implement the tab feature.
I don’t have the details to hand, but the should tell you all you need to know:
(IFS=':'; find $PATH -maxdepth 1 -type f -executable -exec basename <> \; | sort | uniq)
It doesn’t include shell builtins though.
An answer got deleted, I liked it most, so I’m trying to repost it:
compgen is of course better
echo $PATH | tr ':' '\n' | xargs -n 1 ls -1
I found this to be the most typical shell thing, I think it works also with other shells (which I doubt with things like IFS=’:’ )
Clearly, there maybe problems, if the file is not an executable, but I think for my question, that is enough — I just want to grep my output — which means searching for some commands.
How to get list of commands used in a shell script?
I have a shell script of more than 1000 lines, i would like to check if all the commands used in the script are installed in my Linux operating system. Is there any tool to get the list of Linux commands used in the shell script? Or how can i write a small script which can do this for me? The script runs successfully on the Ubuntu machine, it is invoked as a part of C++ application. we need to run the same on a device where a Linux with limited capability runs. I have identified manually, few commands which the script runs and not present on Device OS. before we try installing these commands i would like to check all other commands and install all at once. Thanks in advance
Arek Thanks, it helps little bit but not exactly what i was looking for. I need to know what all commands the script uses. i can not run the entire script at one shot as it runs part by part based on the arguments. as of now we do not have all the information about arguments.
1000 lines isn’t that long, and most of it is probably either shell-specific (like if , while , etc) or repetitive (the same command being called repeatedly with different arguments.) You’ll probably spend less time scanning it manually than you will looking for a way to do it automatically.
Your best bet: update your script to wrap command execution with your own function, then in this function log the command and just pass through to the command itself. run() < echo "$1" >/tmp/commands; $@; > or such.
4 Answers 4
I already tried this in the past and got to the conclusion that is very difficult to provide a solution which would work for all scripts. The reason is that each script with complex commands has a different approach in using the shells features. In case of a simple linear script, it might be as easy as using debug mode. For example: bash -x script.sh 2>&1 | grep ^+ | awk » | sort -u In case the script has some decisions, then you might use the same approach an consider that for the «else» cases the commands would still be the same just with different arguments or would be something trivial (echo + exit).
In case of a complex script, I attempted to write a script that would just look for commands in the same place I would do it myself. The challenge is to create expressions that would help identify all used possibilities, I would say this is doable for about 80-90% of the script and the output should only be used as reference since it will contain invalid data (~20%).
Here is an example script that would parse itself using a very simple approach (separate commands on different lines, 1st word will be the command):
# 1. Eliminate all quoted text # 2. Eliminate all comments # 3. Replace all delimiters between commands with new lines ( ; | && || ) # 4. extract the command from 1st column and print it once cat $0 \ | sed -e 's/\"/./g' -e "s/'[^']*'//g" -e 's/"[^"]*"//g' \ | sed -e "s/^[[:space:]]*#.*$//" -e "s/\([^\\]\)#[^\"']*$/\1/" \ | sed -e "s/&&/;/g" -e "s/||/;/g" | tr ";|" "\n\n" \ | awk '' | sort -u
There are many more cases to consider (command substitutions, aliases etc.), 1, 2 and 3 are just beginning, but they would still cover 80% of most complex scripts. The regular expressions used would need to be adjusted or extended to increase precision and special cases.
In conclusion if you really need something like this, then you can write a script as above, but don’t trust the output until you verify it yourself.
List all commands that a shell knows
What can I type at my shell (which happens to be bash ) that will list all the commands that are recognized? Also, does this differ by shell? Or do all shells just have a «directory» of commands they recognize? Secondly, different question, but how can I override any of those? In other words how can I write my own view command to replace the one existing on my Ubuntu system, which appears to just load vim .
7 Answers 7
compgen -c # will list all the commands you could run.
compgen -a # will list all the aliases you could run. compgen -b # will list all the built-ins you could run. compgen -k # will list all the keywords you could run. compgen -A function # will list all the functions you could run. compgen -A function -abck # will list all the above in one go.
A shell knows four kinds of commands.
- Aliases: these are nicknames for a command with some options. They are defined in the shell’s initialization file ( ~/.bashrc for bash).
- Functions: they are snippets of shell code given a name. Like aliases, they are defined in the shell’s initialization file.
- Builtins: the shell comes with a small number of built-in commands. Most builtins manipulate the shell state ( cd changes the current directory, set changes options and positional parameters, export changes the environment, …). Most shell offer largely the same builtins but each shell has a few extensions to the basic set.
- External commands: they are independent of the shell. Like other programs, the shell executes external programs by looking them up in the executable search path. The PATH environment variable contains a colon-separated list of directories to search for programs.
In case there are commands of several types by the same name, the first match in the order above is executed¹.
You can see what type of command a name corresponds to by running type some_name .
You can list aliases by running the alias built-in with no argument. There is no way to list functions or builtins that works in all shells. You can find a list of builtins in the shell’s documentation.
In bash, the set builtin lists functions with their definitions as well as variables. 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.
In zsh, you can list the currently available commands of a given type with echo $ , echo $ , echo $ and echo $ (that last one lists external commands only).
The following shell-agnostic snippet lists all available external programs:
case "$PATH" in (*[!:]:) PATH="$PATH:" ;; esac set -f; IFS=: for dir in $PATH; do set +f [ -z "$dir" ] && dir="." for file in "$dir"/*; do if [ -x "$file" ] && ! [ -d "$file" ]; then printf '%s = %s\n' "$" "$file" fi done done
There is an edge case in Bash: hashed commands.
A full search of the directories in $PATH is performed only if the command is not found in the hash table
set -h mkdir ~/dir-for-wat-command echo 'echo WAT!' >~/dir-for-wat-command/wat chmod +x ~/dir-for-wat-command/wat hash -p ~/dir-for-wat-command/wat wat wat
The PATH environment variable doesn’t contain ~/dir-for-wat-command , compgen -c doesn’t show wat , but you can run wat .
If you want to shadow an existing command, define an alias or a function.
¹ Exception: a few builtins (called special builtins) cannot be shadowed by a function — bash and zsh don’t comply with POSIX on that point in their default mode though.
Methods to List All Available Commands and Aliases in Linux
In Linux, commands are the key things that are very interesting to run and execute the respective programs. Executing such commands and their aliases lets the user run many important tasks. There are many ways to list them out of which one is to write the shell script. But Linux makes it easy with the keyword of the shell library which is compgen.
Here at LinuxAPT , we will look into different methods to list all the available commands and aliases in Linux.
1. How to use the .bashrc to List All Available Commands and Aliases in Linux ?
One way is to write the shell script by adding it on .bashrc. Let us add a few lines of shell scripts so it can list the command and aliases.
i. To list all the command
Add these lines of shell script to list all the available commands:
function ListAllCommands
echo -n $PATH | xargs -d : -I <> find <> -maxdepth 1 \
-executable -type f -printf '%P\n' | sort -u
>
ii. To list all the aliases
Add these lines of shell script to list all the available aliases:
function ListAllCommands
COMMANDS=`echo -n $PATH | xargs -d : -I <> find <> -maxdepth 1 \
-executable -type f -printf '%P\n'`
ALIASES=`alias | cut -d '=' -f 1`
echo "$COMMANDS"$'\n'"$ALIASES" | sort -u
>
To list all the available commands and aliases in $PATH that is using mycommand, run the below command:
2. How to use the built in shell library function to List All Available Commands and Aliases in Linux ?
There is a built-in keyword of the shell library which is compgen that is very easy to use and execute to list all the commands and the aliases in Linux:
You are allowed to use any of the listed flags as per your requirement:
- compgen -c → list all the commands that we can run.
- compgen -a → list all the aliases that we can run.
- compgen -b → list all the built-ins that we can run.
- compgen -k → list all the keywords that we can run.
- compgen -A function → list all the functions that we can run.
- compgen -A function -abck → list all the above flags can do at once.
Here, we are going to list all the commands that we can run and aliases. So the -c and -a flag is used in such cases.
To list all the commands that we can run, let’s run the command:
To list the files on commands.txt, let’s print the contents with the cat command:
Here, we have successfully printed the available commands that we can run and there are many more commands which we could not capture on the screenshot.
To list all the aliases that we can run, let’s run the command:
To list the files on aliases.txt, let’s print the contents with the cat command:
Here, we have successfully printed the available aliases that we can run.
Creating a script with the use of compgen command is also one way to list the commands and aliases that we could run. Check the example below for further details regarding the script:
$ echo "compgen -c" > commands.sh
Here, we have created a list.sh script file with content «compgen -c» in it.
Let’s give the execute permission to the script with the command :
Now, run the script with the command:
Here, all the available commands are listed with the above script by using the compgen command.
[Need Linux system support ? We can help you . ]
Conclusion
This article covers how to list all the available commands and aliases that we can run on Linux. In fact, The compgen is bash built-in command and it will show all available commands, aliases, and functions for you. This command works under Linux, macOS, *BSD and Unix-like system when Bash shell is installed.