简体   繁体   English

如何使用 bash 脚本在 linux 中创建命令?

[英]How do I make a command in linux using a bash script?

I am trying to make a command for the terminal.我正在尝试为终端发出命令。 I have a bash script prepared (la.sh) and I want to just be able to type la to run it.我准备了一个 bash 脚本 (la.sh),我只想输入 la 来运行它。 How am I able to get the code so that I can just type la?我怎样才能获得代码以便我可以输入 la?

I have tried putting it in the /bin folder however had no luck.我试过把它放在 /bin 文件夹中,但是没有运气。

What can I do to fix this?我能做些什么来解决这个问题?

I am using the latest version of Manjaro Gnome.我正在使用最新版本的 Manjaro Gnome。

Thanks a lot!!!非常感谢!!!

BTW, the script was literally just ls .顺便说一句,脚本实际上只是ls

It was just a practice script.这只是一个练习脚本。

The file la.sh must be placed in your path .文件 la.sh 必须放在您的路径中 Then you can create an alias for it.然后你可以为它创建一个别名。

alias la="la.sh"

Lets consider that your script is stored under /some/path/la.sh .让我们考虑您的脚本存储在/some/path/la.sh下。 In my opinion, you have several solutions to accomplish your goal:在我看来,您有几种解决方案来实现您的目标:

Option 1:选项1:

Add the script to your user's path so you can directly call it.将脚本添加到您的用户路径,以便您可以直接调用它。

echo "export PATH=$PATH:/some/path/" >> ~/.bashrc

Then you will be able to use in your terminal:然后你就可以在你的终端中使用:

$ la.sh

Using this option you can call la.sh with any parameters if needed.使用此选项,您可以根据需要使用任何参数调用la.sh If the requirement is to call simply la you can also rename the script or create a softlink:如果要求只是调用la您还可以重命名脚本或创建软链接:

mv /some/path/la.sh /some/path/la

or或者

ln -s /some/path/la.sh /some/path/la

Option 2:选项 2:

Create an alias for the script.为脚本创建别名。

echo "alias la='/some/path/la.sh'" >> ~.bashrc

Then you will be able to use in your terminal:然后你就可以在你的终端中使用:

$ la

However, using this option you will not be able to pass arguments to your script (executing something similar to la param1 param2 ) unless you define a more complex alias (an alias using a function in the .bashrc , but I think this is out of the scope of the question).但是,使用此选项您将无法将参数传递给您的脚本(执行类似于la param1 param2 ),除非您定义一个更复杂的别名(使用.bashrc的函数的别名,但我认为这是不合适的)问题的范围)。

IMPORTANT NOTE: Remember to reload the environment in your terminal ( source .bashrc ) or to close and open again the terminal EVERY TIME you make modifications to the .bashrc file.重要说明:记住在您的终端( source .bashrc )中重新加载环境,或者每次您对.bashrc文件进行修改时关闭并再次打开终端。 Otherwise, you will not be able to see any changes.否则,您将无法看到任何更改。

If you want to have a command be available under two different names ( la.sh and la in your case), I recommend against using an alias: An alias defined in your .bashrc is only available in an interactive bash;如果您想让命令以两个不同的名称(在您的情况下为la.shla )可用,我建议不要使用别名:在 .bashrc 中定义的别名仅在交互式 bash 中可用; If you run, say, a non-bash interactive shell, or writing a bash script, you can't use it.如果您运行非 bash 交互式 shell,或编写 bash 脚本,则无法使用它。

The IMO most general way is to create a link. IMO 最通用的方法是创建链接。 Since you said that you have already placed la.sh into bin, you can create the link in the same directory, ie既然你说已经把la.sh放到bin里了,那么可以在同目录下创建链接,即

ln /bin/la /bin/la.sh # creates a hard link

or或者

ln -s /bin/la /bin/la.sh # creates a symbolic link

In your case, either one is fine.在你的情况下,任何一个都可以。 If you want to find out more about the differences between hard and symbolic link, look for instance here .如果您想了解有关硬链接和符号链接之间差异的更多信息,请查看此处的示例。

It worked with a mixture of everybody's answers.它结合了每个人的答案。

All I had to do was go into the directory that la.sh was in. Rename it to just la as a text file.我所要做的就是进入la.sh的目录。将其重命名为la作为文本文件。 Run chmod 777 la to turn it into executable to anybody.运行chmod 777 la把它变成任何人都可以执行的。 Add it to my path by using the command export PATH=$PATH:~/Directory/It/Was/In/使用命令export PATH=$PATH:~/Directory/It/Was/In/将其添加到我的路径中

Thank you to all who contributed.感谢所有做出贡献的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM