shell - 如何在不给出完整路径的情况下调用命令?

标签 shell ubuntu terminal command

我在 /usr/share/my-editor/editor-executable 下安装了一个文本编辑器.

当我想在终端中启动编辑器时,我必须编写完整的路径:

# /usr/share/my-editor/editor-executable file-to-open

我会为编辑器创建一个命令,这样我就可以简单地数字:
# my-editor file-to-open

我能怎么做?

最佳答案

无法启动可执行文件的原因是 shell 在 PATH 中定义的任何路径中查找命令。环境变量(从现在开始的已知路径)。
您可以使用以下命令检查那些已知路径:

echo $PATH

如您所见,/usr/bin在那里定义以及其他路径。

无论如何,您可以通过多种方式获得您想要的东西。

请注意,当我使用 时~ 目录,该命令将仅对当前用户可用。

创建别名 my-editor
当您想要运行在已知路径中找不到的命令时,这是我最喜欢的。这对你来说也是个好主意。在 bash你可以把别名放在~/.bash_aliases .
echo alias my-editor=/usr/share/my-editor/my-editor-executable >> ~/.bash_aliases

在某些已知路径中创建指向文件的链接

这是您完成它的方式,只是为了澄清一下,如果您在任何已知路径中创建了链接,它也会起作用。
ln -s /usr/share/my-editor/my-editor-executable /usr/bin/my-editor

定义一个名为 my-editor 的函数

我认为这太多了,因为你的需要,但如果你想试一试,这取决于你。它可以用于其他目的。
您必须在 shell 读取的文件中定义它。例如~/.bashrcbash . Shell files invocation.
cat >> ~/.bashrc << "EOF"
function my-editor() {
    /usr/share/my-editor/my-editor-executable "$@"
}
EOF

添加 /usr/share/my-editor/PATH
您可以将新路径添加到 PATH多变的。在 Ubuntu 中,PATH变量一般设置在/etc/environment如果您修改此文件,则所有用户都可以访问新路径。

但是,如果您想成为唯一有权访问新路径的人,您可以在其中一个个人 shell 文件中设置它。例如在 bash : ~/.bashrc . Shell files invocation.
echo 'export PATH="$PATH:/usr/share/my-editor/"' >> ~/.bashrc

[ bash ] 在哈希表中输入命令

bash 中获得相同结果的单一方法正在添加 my-editor入 shell hash table 。同样,您必须将命令添加到 bash 读取的某个文件中。 (~/.bashrc)。
echo 'hash -p /usr/share/my-editor/my-editor-executable my-editor' >> ~/.bashrc

将可执行文件移动到已知路径

最后,如果您不再需要他当前目录中的文件 ( my-editor-executable ),您只需将其移动到已知路径即可。
mv /usr/share/my-editor/my-editor-executable /usr/bin/my-editor

关于shell - 如何在不给出完整路径的情况下调用命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26313870/

相关文章:

linux - Solaris 的 grep -A 备用命令是什么?

linux - Bash 错误报告

linux - Ubuntu:允许非 root 用户创建和模拟用户

linux - 从 ubuntu 访问网站非常慢,但在 windows 中运行速度更快

ubuntu - 如何从终端判断您位于基本屏幕?

macos - 我可以将命令行脚本转换为 AppleScript 吗?

linux - 在 linux 启动时运行脚本

regex - 如何使用 grep 从文件中获取 Skype 用户名?

PostgreSQL 9.6 : pgadmin4 is not opening on localhost:5050 (Ubuntu 16. 04)

linux - 如何在 CentOs 中使用命令行杀死所有打开的终端