如何制作“别名"长路漫漫? [英] How to make an "alias" for a long path?

查看:15
本文介绍了如何制作“别名"长路漫漫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为我在编写 shell 脚本时经常使用的路径创建一个别名".我尝试了一些东西,但失败了:

I tried to make an "alias" for a path that I use often while shell scripting. I tried something, but it failed:

myFold="~/Files/Scripts/Main"
cd myFold

bash: cd: myFold: No such file or directory

我如何使它工作?
但是,cd ~/Files/Scripts/Main 有效.

How do I make it work ?
However, cd ~/Files/Scripts/Mainworks.

推荐答案

由于它是一个环境变量(别名在 bash 中有不同的定义),你需要用类似的东西来评估它:

Since it's an environment variable (alias has a different definition in bash), you need to evaluate it with something like:

cd "${myFold}"

或:

cp "${myFold}/someFile" /somewhere/else

但我实际上发现它更容易,如果您只想轻松切换到该目录,则创建一个 real 别名(在 bash 启动文件之一中,例如.bashrc),所以我可以保存击键:

But I actually find it easier, if you just want the ease of switching into that directory, to create a real alias (in one of the bash startup files like .bashrc), so I can save keystrokes:

alias myfold='cd ~/Files/Scripts/Main'

然后你就可以使用(没有cd):

Then you can just use (without the cd):

myfold

要去掉定义,您可以使用 unalias.下面的文字记录显示了所有这些操作:

To get rid of the definition, you use unalias. The following transcript shows all of these in action:

pax> cd ; pwd ; ls -ald footy
/home/pax
drwxr-xr-x 2 pax pax 4096 Jul 28 11:00 footy

pax> footydir=/home/pax/footy ; cd "$footydir" ; pwd
/home/pax/footy

pax> cd ; pwd
/home/pax

pax> alias footy='cd /home/pax/footy' ; footy ; pwd
/home/pax/footy

pax> unalias footy ; footy
bash: footy: command not found

这篇关于如何制作“别名"长路漫漫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆