Bash 编程 - 创建自定义终端命令 - Mac

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5461684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:42:29  来源:igfitidea点击:

Bash Programming - Creating a Custom Terminal Command - Mac

macosbashvi

提问by CodeGuy

I tried making a custom command for my terminal. I'm running Mac OS X Version 10.6.6.

我尝试为我的终端创建一个自定义命令。我正在运行 Mac OS X 版本 10.6.6。

Here are the steps I took:

以下是我采取的步骤:

  1. Opened the terminal

  2. Typed "ls -a" and saw a ".bash_profile" file

  3. Typed "vi .bash_profile" and saw that the file was empty

  4. Added the following to the top of the file: "test() { echo hi }"

  5. Saved the vi session by typing ":wq"

  1. 打开了终端

  2. 输入“ls -a”并看到一个“.bash_profile”文件

  3. 输入“vi .bash_profile”,看到文件是空的

  4. 在文件顶部添加以下内容:“test() { echo hi }”

  5. 通过键入“:wq”保存 vi 会话

Then, I was back to the terminal, and typed "test", however nothing outputted (namely, I didn't see "hi").

然后,我回到终端,输入“test”,但没有任何输出(即,我没有看到“hi”)。

So, what am I doing wrong? Thanks!

那么,我做错了什么?谢谢!

回答by Laurent G

try to force re-evaluation of file by typing source .bash_profile

尝试通过键入强制重新评估文件 source .bash_profile

回答by codingishard

I know this is pretty late, but for future people who google this question, here's what you need to do:

我知道这已经很晚了,但对于未来使用谷歌这个问题的人来说,这是你需要做的:

When you edit the bash.profile file, you need to use the aliascommand. For example: alias [name]="[script]"Since you are trying to make a test command that sends an output of "hi", you would enter testwhere [name] is, and echo hiwhere [script] is. Do not forget to use quotations around the script

编辑 bash.profile 文件时,需要使用该alias命令。例如:alias [name]="[script]"由于您正在尝试创建一个发送“hi”输出的测试命令,您将输入testwhere [name] is,并echo hiwhere [script] is。不要忘记在脚本周围使用引号