[英]Setting a pathname as an alias in the .bashrc file
我試圖在.bashrc文件中使用別名來存儲我通常會去的路徑(Ubuntu 14.04),即alias pathname="/home/Dommol/test/next"
但是當我嘗試使用別名cd pathname
我得到了錯誤-bash: cd: pathname: No such file or directory
。
問題:如何讓bash識別出我在嘗試使用別名pathname
而不是嘗試更改為目錄pathname
?
alias pathname="cd /home/Dommol/test/next"
,我可以將別名alias pathname="cd /home/Dommol/test/next"
然后鍵入pathname
進行更改
alias
用於別名命令,而不是shell變量。 要執行所需的操作,請在.bashrc
設置一個shell變量:
pathname="/home/Dommol/test/next"
然后在提示下:
$ cd $pathname
使用alias
在.bashrc
使用所需參數作為自定義命令,如您在“ aside”中所述:
alias pathname="cd /home/Dommol/test/next"
然后在提示下:
$ pathname
潛伏者的答案應該是公認的解決方案。 但是,我想回答您的原始問題,這可能會起作用:
#Your test code:
alias pathname="/home/Dommol/test/next"
cd pathname
#Similar functionality
ln -s /home/Dommol/test/next pathname
cd -P pathname
如果您有更多這樣的目錄並希望從任何位置進行cd安裝,則可以使用此命令:(請注意,下面的代碼僅限於cd
命令。)
mkdir -p ~/.cdpath #Random name - could be changed
export CDPATH=~/.cdpath
ln -s /home/Dommol/test/next ~/.cdpath/pathname
cd -P pathname #Will work from any starting location.
ln -s /home/Dommol/test/next2 ~/.cdpath/pathname2
cd -P pathname2 #Will work from any starting location.
最佳解決方案是將其創建為變量,如lurker的答案所解釋。 該解決方案也適用於其他命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.