我想在一個腳本來創建一個別名,像這樣:設置別名
#!/bin/bash
shopt -s expand_aliases
#Currently just using 'git' to try and get it working
#It'll be replaced with the following line when it works
#compgen -c | while read x;
echo 'git' | while read x;
do
a=`echo $x | tr '[:lower:]' '[:upper:]'`;
echo $a;
echo $x;
if [ "$a" != '' ]
then
echo 'got here';
alias $a=$x;
alias;
GIT;
fi
done
而一個簡單的測試腳本作品(testme
添加到別名在我當前的shell):
#!/bin/bash
shopt -s expand_aliases
alias testme="echo It Worked"
alias
testme
輸出:
GIT
git
got here
alias GIT='git'
alias grep='grep --colour=auto'
alias ls='ls --color=auto'
GIT: command not found
GIT
在別名顯示了,BU t不能執行。我試過用. ./script.sh
和source script.sh
來調用它。我究竟做錯了什麼?
你不應該依賴腳本中的別名。你應該直接使用變量。但如果你仍然想要別名,也許這可以幫助:http://stackoverflow.com/questions/5240755/how-to-use-aliases-defined-in-bashrc-in-shell-script – Lynch 2013-04-10 03:27:29
腳本的整個觀點是*設置別名,不運行它。這有點無用,但很有趣! – SomeKittens 2013-04-10 13:21:34