首页 > 解决方案 > 为位于我的一个文件夹中的 shell 脚本设置别名

问题描述

所以我有这个文件夹 BashScripts 有以下目录路径

/home/sadnan/BashScripts

/home/sadnan成为我的$HOME。这是我打算放置自定义 shell 脚本的地方。目前只有一个名为cbi.sh. 当我从文件夹内部运行时,脚本按预期工作。现在我希望能够在全球范围内运行它。所以我将以下内容添加到我的 .bashrc 文件中

#For my personal Bash Scripts
if [ -d $HOME/BashScripts ]; then
 PATH="$PATH:$HOME/BashScripts"
fi

alias cbi='. ./cbi.sh'

所以现在当我这样做echo $PATH时会打印出以下内容

/home/sadnan/.nvm/versions/node/v12.18.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/sadnan/BashScripts

因此该文件夹似乎已添加到 PATH 中。如果我理解正确的话,应该允许我在全球范围内运行我的 shell 脚本。

然后我还为脚本设置了一个别名并命名它cbi

但是现在当我跑步cbi./cbi.sh在我的终端上时,这就是我得到的

bash: ./cbi.sh: No such file or directory

我做错了什么,我没有做太多的shell脚本。但我认为这是要走的路。

我也尝试过alias cbi=`source ./cbi.sh'alias cbi=`sh ./cbi.sh' 但都没有奏效。

标签: linuxbashshellalias

解决方案


你必须运行cbi.sh

# Wrong, the file is not named 'cbi'
cbi

# Wrong, the file is not in the current directory
./cbi.sh

# Wrong, same reason as above
alias cbi='. ./cbi.sh'
cbi

# Correct, this is the name of the file:
cbi.sh

推荐阅读