是否有一个Linux命令将列出此终端会话的所有可用命令和别名?

就好像你输入“a”,然后按下tab键,不过是针对字母表中的每个字母。 或者运行'alias',但也返回命令。

为什么?我想运行以下命令,看看是否有可用的命令:

ListAllCommands | grep searchstr

当前回答

试试这个脚本:

#!/bin/bash
echo $PATH  | tr : '\n' | 
while read e; do 
    for i in $e/*; do
        if [[ -x "$i" && -f "$i" ]]; then     
            echo $i
        fi
    done
done

其他回答

你可以随时进行以下操作:

1. Hold the $PATH environment variable value.
2. Split by ":"
3. For earch entry: 
    ls * $entry 
4. grep your command in that output.

只有当它们被列在env var路径中时,shell才会执行命令。

这取决于,我的意思是这取决于你用的是什么外壳。以下是我看到的限制条件:

必须运行在与你的shell相同的进程中,以捕获别名、函数和变量,这些会影响你可以找到的命令,考虑PATH或EDITOR,尽管EDITOR可能超出范围。你可以有未导出的变量来影响事情。 它是特定于shell的,或者你进入内核,/proc/pid/enviorn和朋友没有足够的信息

我使用ZSH,所以这里是一个ZSH的答案,它做以下3件事:

转储文件路径 转储别名 dump环境中的函数 他们排序

下面就是:

feed_me() {
    (alias | cut -f1 -d= ; hash -f; hash -v | cut -f 1 -d= ; typeset +f) | sort
}

如果你使用zsh,这应该做到。

compgen -c > list.txt && wc list.txt

添加到.bashrc

function ListAllCommands
{
    echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \
        -executable -type f -printf '%P\n' | sort -u
}

如果你还需要别名,那么:

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
}

也许我误解了,但如果你按下Escape直到你得到显示所有X种可能性怎么办?