shell - 命令/别名可以可变吗?

标签 shell alias bash

我需要不时查看日志文件的特定行:

$ head -10 log.txt|tail -1  # to view line 10 of log.txt

然后我在我的 bashrc 中写了一个函数 v 让生活更轻松:

$ v 10

好吧,也许我在这里有点 split :我也想忽略空格:

$ v10

我知道的唯一方法是定义很多别名:

alias v1='v 1'
alias v2='v 2'
alias v3='v 3'
alias v4='v 4'
...

有什么好的方法吗?


感谢@Chirlo 和@anishsane 的想法。

这是我的最终版本,基于@anishsane 的一些修复:

eval "`declare -f command_not_found_handle | sed s/command_not_found_handle/command_not_found_handle_orig/`"
command_not_found_handle(){
    if expr match "$1" "v[0-9][0-9]*" >/dev/null ; then
       v ${1:1}
       return $?
    fi
    command_not_found_handle_orig "$@"
}

最佳答案

检查 command_not_found_handle ()如果你有 bash > 4,你可以覆盖它来处理这种情况。当您键入 v10 并且 bash 无法解析它时,将调用此函数并将 v10 作为第一个参数,然后您捕获它并在那里做您的事情。

关于shell - 命令/别名可以可变吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15757937/

相关文章:

mysql - 列别名在 SQL fiddle 中不起作用

如果参数和重定向之间没有空格,则 Bash 不正确的行为

bash - 从管道读取的 while 读取循环后变量被重置

linux - 在/usr/bin/env 中设置环境变量在 Linux 上挂起进程

linux - 从 *nix 在远程 Windows 机器上执行 exe 或 bat 文件

linux - Aliasing `grep -w ' label for '` 只搜索第一个词(label)

python - 如何让我的别名从 postfix 服务器调用 shell 脚本来调用 python 程序?

java - Docker Entrypoint.sh 用于自更新程序

shell - 通过获取 shell 脚本在 Qt Creator 中设置环境变量

bash - 关于 autoconf 生成的 shell 脚本部分的许多问题