macOS 修改bash環境變量PATH及自建腳本直接輸入腳本名執行的方法
bash添加PATH環境變量的方法
- 新建
~/Desktop/Myshell
文件夾。 - 打開~/.bash_profile文件,配置PATH環境變量
- 添加一行:
export PATH=$PATH:~/Desktop/Myshell
保存。(~/Desktop/Myshell
爲需要添加的目錄) - 執行
source ~/.bash_profile
。 - 重啓terminal, 執行
echo $PATH
查看添加結果,不同的目錄會以冒號:
分隔,如下:
$ [liuyang] [~] $ echo $PATH
/opt/subversion/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/liuyang108/.rvm/bin:/Users/liuyang108/.rvm/bin:/Users/liuyang108/Desktop/Myshell
- 後續可以把自己的bash腳本添加到該目錄下,就能直接執行腳本名,而無需輸入路徑信息。
自建腳本直接輸入腳本名執行方法
~/Desktop/Myshell
目錄下新建腳步文件mypush
- 修改腳步文件的執行權限:
chmod +x ~/Desktop/Meshell/mypush
- 將目錄
~/Desktop/Myshell
添加到PATH環境變量 - terminal執行
mypush
即可
注:mypush文件爲簡化的git push腳本,意在執行 git push origin branchName:refs/for/branchName
,腳本內容如下:
#!/bin/bash
localBranch=`git symbolic-ref --short -q HEAD`
if [ -z "$localBranch" ]; then
echo "[Error] Invalid Path"
exit 1
fi
if [ $# -eq 0 ]; then
remoteBranch=$localBranch
elif [ $# -eq 1 ]; then
remoteBranch=${1}
else
echo "[Error] Only one param needed"
exit 2
fi
git pull --rebase origin ${remoteBranch}
if [ $? -ne 0 ]; then
exit 3
fi
git push origin ${localBranch}:refs/for/${remoteBranch}
exit 0