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

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

问题描述

我尝试为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天全站免登陆