Linux provides us with a huge amount of commands along with their aliases that we can make use of. Although these commands serve different purposes we can still make use of all these commands anywhere we like in the terminal.
There are different ways with which we can interact with Linux commands. When it comes to listing all the available commands to the terminal we also have different approaches, either we can write a shell script by ourselves or we can use a shell library function that does that for us.
Let’s consider the first approaches where I'll make use of a shell library keyword named compgen, which is a bash builtin command that can be used to list all the available commands.
In the above syntax, the flag is a placeholder that can be replaced according to our needs.
The flag can have all these different values −
-c − used to list all the commands you could run.
-a − used to list all the aliases you could run.
-k − used to list all the keywords you could run.
-b − used to list all the built-ins you could run.
-A function − used to list all the functions you could run
-A function -abck − used to list everything shown above in one go.
Since, we only need to list the commands and their aliases we will make use of the -c and -a flags only.
In order to use that just create a shell file with the command shown below −
Now insert the code shown below −
And then give permission to the script before running it −
chmod 777 sample.sh ./sample.sh
immukul@192 Downloads % ./sample.sh if then else elif fi case esac for select while until do done in . . .
In order to print the aliases, we just need to replace the code inside the sample.sh file to the code shown below −
compgen -a | grep ls
ls is /bin/ls