I am trying to print all of the program file names on a system accessable from bash. (eg the path) I have been using 'which $(compgen -c)' but that does not seem efficient. Is there a better way?
Asked
Active
Viewed 77 times
1 Answers
0
This seems faster:
IFS=: read -ra __ <<< "$PATH"
find "${__[@]}" -mindepth 1 -maxdepth 1 -xtype f -executable
And this isn't faster but it prints accurately the commands found in the filesystem and only printing one path for every name depending on which would get executed first, not twice e.g. if /bin/echo
and /usr/bin/echo
both exist would only print either of the two. It could happen since compgen -c
doesn't only print the echo
found in the filesystem but the echo
builtin command as well. The command above would print all executables found in $PATH
.
type -P $(compgen -c | awk '!a[$0]++')
If your commands has spaces, use IFS=$'\n'
in a subshell:
( IFS=$'\n'; type -P $(compgen -c | awk '!a[$0]++'); )
Disabling pathname expansion could also be safer:
( IFS=$'\n'; set -o noglob; type -P $(compgen -c | awk '!a[$0]++'); )
Saving to an array as well:
IFS=$'\n' read -rd '' -a __ < <(compgen -c | awk '!a[$0]++')
type -P "${__[@]}"

konsolebox
- 72,135
- 12
- 99
- 105