bash - 如何在Zsh中使用 "source"命令

标签 bash macos terminal zsh

我正在 Mac 上的 Zsh 终端内工作。

我正在尝试使用 source命令,以便我可以按名称调用我的脚本,而不是键入“.sh”脚本的路径。 source命令不会返回任何错误,但是一旦我尝试按名称调用“.sh”文件,它就会返回“未找到命令”。

我也试过在使用 source 时输入绝对路径命令,但没有运气。

终端命令:

source ~/Documents/marco.sh
marco
zsh: command not found: marco

马可
#!/usr/bin/env bash  
touch ~/Documents/marco.txt
echo $(pwd) > ~/Documents/marco.txt

最佳答案

要完全按照您想要的方式进行操作,您可以制作 marco功能:

# marco.sh
marco () {
  touch ~/Documents/marco.txt
  echo $(pwd) > ~/Documents/marco.txt
}

那你source它并运行 marco .如果你要经常使用它,我建议把这个函数放在 .zshrc 中或另一个将由您的 shell 自动获取的文件。

正如评论中所建议的,你也可以把你原来的 marco.sh在你的道路上。我喜欢用 ~/bin对于这些类型的个人可执行文件:
$ mkdir ~/bin
$ mv ~/Documents/marco.sh ~/bin
$ export PATH="$HOME/bin:$PATH"
$ marco.sh

再次输入 export PATH您的 .zshrc 中的线路或类似的文件。

关于bash - 如何在Zsh中使用 "source"命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60233256/

相关文章:

python - Scrapy安装: no acceptable C compiler found in $PATH

bash - OS X GNU- screen 破坏着色

linux - bash 脚本昨天的 zip 文件

linux - Cat 标题并使用 awk 重命名列标题?

mysql - 如何在 mySQL 中使用 bash 别名

bash - 所有 bash history No such file or directory error on directories with spaces

macos - 基于 View 的 TableView 在基于 View 的 TableView 中

c++ - 无法在 OS X 10.9 Mavericks 上编译 Asterisk

file - 根据名称在终端中移动内容

linux - terminfo 似乎在 csh 中不起作用