如何调用命令而不给出其完整路径? [英] How to call a command without giving its full path?

查看:154
本文介绍了如何调用命令而不给出其完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 / 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 以及其他路径。



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






请注意,当我使用目录时,该命令仅适用于当前用户。
$ b

创建别名 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读取的文件中定义它。例如在 bash 中的〜/ .bashrc Shell文件调用。

  cat>> 〜/ .bashrc< EOF
function my-editor(){
/ usr / share / my-editor / my-editor-executable$ @
}
EOF





添加 / usr / share / my-编辑器/ 添加到 PATH



c $ c> PATH 变量。在Ubuntu中, PATH 变量通常设置在 / etc / environment 中,如果修改此文件,



但是,如果您想成为唯一有权访问新路径的用户,您可以将其设置为 >个人 shell文件。例如在 bash 〜/ .bashrc Shell文件调用。

  echo'export PATH =$ PATH:/ usr / share / my-editor /'> 〜/ .bashrc 






[ bash ]输入命令到哈希表



一个奇怪的方法, c $ c> bash 正在将 my-editor 添加到shell hash 表。同样,您必须在由 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


I installed a text editor under /usr/share/my-editor/editor-executable.

When I want to launch the editor in terminal I have to write the complete path:

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

I would create a command for the editor so I can simply digit:

# my-editor file-to-open

How can I do?

解决方案

The reason you couldn't launch your executable is because the shell look for the command in any of the paths defined in the PATH environment variable (known paths from now). You can check those known paths with:

echo $PATH

As you can see, /usr/bin is defined there as well as other paths.

Anyway, you can get what you want in several ways.


Note below that when I use the ~ directory, the command will be only available for the current user.

Creating an alias my-editor

This is my favourite when you want to run a command which is not found in the known paths. It would be a good idea for you too. In bash you can place the alias in ~/.bash_aliases.

echo alias my-editor=/usr/share/my-editor/my-editor-executable >> ~/.bash_aliases


Creating a link to your file in some of the known paths

It's the way you have done it and just to clarify, if you had created the link in any of the known paths, it would have worked too.

ln -s /usr/share/my-editor/my-editor-executable /usr/bin/my-editor


Defining a function with name my-editor

I think it's too much due to your needs but it's up to you if want to give it a try. It can be useful for other purposes.
You must define it in a file read by your shell. e.g. ~/.bashrc in bash. Shell files invocation.

cat >> ~/.bashrc << "EOF"
function my-editor() {
    /usr/share/my-editor/my-editor-executable "$@"
}
EOF


Adding /usr/share/my-editor/ to the PATH

You can add a new path to the PATH variable. In Ubuntu, the PATH variable is generally set in /etc/environment and if you modify this file, the new path will be accesible for all users.

However, if you want to be the only one who has access to the new path, you can set it in one of the personal shell files. e.g. in bash: ~/.bashrc. Shell files invocation.

echo 'export PATH="$PATH:/usr/share/my-editor/"' >> ~/.bashrc


[bash] Entering a command into the hash table

A singular way to get the same result in bash is adding my-editor into the shell hash table. Again, you must add the command in some file read by bash (~/.bashrc).

echo 'hash -p /usr/share/my-editor/my-editor-executable my-editor' >> ~/.bashrc


Moving the executable to a known path

Finally, if you don't need the file (my-editor-executable) in his current directory anymore, you can simply move it to a known path.

mv /usr/share/my-editor/my-editor-executable /usr/bin/my-editor

这篇关于如何调用命令而不给出其完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆