[英]Setting a pathname as an alias in the .bashrc file
I'm attempting to use aliases in the .bashrc file to store the paths I commonly go to (Ubuntu 14.04) ie alias pathname="/home/Dommol/test/next"
But when I attempt to use the alias cd pathname
I get an error -bash: cd: pathname: No such file or directory
. 我试图在.bashrc文件中使用别名来存储我通常会去的路径(Ubuntu 14.04),即alias pathname="/home/Dommol/test/next"
但是当我尝试使用别名cd pathname
我得到了错误-bash: cd: pathname: No such file or directory
。
Question: How do I get bash to recognize that I am trying to use the alias pathname
and not trying to change to to the directory pathname
? 问题:如何让bash识别出我在尝试使用别名pathname
而不是尝试更改为目录pathname
?
As an aside, I could make the alias alias pathname="cd /home/Dommol/test/next"
and just type pathname
to change alias pathname="cd /home/Dommol/test/next"
,我可以将别名alias pathname="cd /home/Dommol/test/next"
然后键入pathname
进行更改
alias
is used to alias a command, not a shell variable. alias
用于别名命令,而不是shell变量。 To do what you want, set a shell variable in your .bashrc
: 要执行所需的操作,请在.bashrc
设置一个shell变量:
pathname="/home/Dommol/test/next"
Then at the prompt: 然后在提示下:
$ cd $pathname
Using an alias
to make a custom command with the arguments you want this in your .bashrc
, as you noted in your "aside": 使用alias
在.bashrc
使用所需参数作为自定义命令,如您在“ aside”中所述:
alias pathname="cd /home/Dommol/test/next"
Then at the prompt: 然后在提示下:
$ pathname
The answer by lurker should be the accepted solution. 潜伏者的答案应该是公认的解决方案。 However, to answer your original question, I think, this could work: 但是,我想回答您的原始问题,这可能会起作用:
#Your test code:
alias pathname="/home/Dommol/test/next"
cd pathname
#Similar functionality
ln -s /home/Dommol/test/next pathname
cd -P pathname
If you have more such directories & want to cd from any location, you could have this: (Note that this code below is limited to cd
command.) 如果您有更多这样的目录并希望从任何位置进行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.
Best solution would be to create it as a variable, as already explained by lurker's answer. 最佳解决方案是将其创建为变量,如lurker的答案所解释。 That solution will work for other commands as well. 该解决方案也适用于其他命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.