简体   繁体   English

如何在bashrc文件中为别名命令动态设置路径的一部分

[英]How to set part of a path dynamically for alias command in bashrc file

I'm trying to have a alias command and the part of its path keeps changing. 我正在尝试使用别名命令,并且其路径的一部分不断变化。 Eg: 例如:

/var/mywork/swag/wsnew/
/var/mywork/swag/ws/
/var/mywork/swag/wsold/

and my alias command to achieve is something link this 和我的别名命令要实现的是一些链接

alias cws='cd /var/mywork/swag// 别名cws ='cd / var / mywork / swag //

since last directory in the path is keep changing. 因为路径中的最后一个目录一直在变化。 I wanted to get automatically update in alias command. 我想自动获取别名命令中的更新。 Is there anyway ? 反正有吗?

I tried something like 我尝试了类似的东西

alias cws='cd /var/mywork/swag/getenv("WSP")/ 别名cws ='cd / var / mywork / swag / getenv(“ WSP”)/

so whenever I set WSP to required path it automatically takes. 因此,每当我将WSP设置为所需路径时,它都会自动使用。 But it didn't help. 但这没有帮助。

Like most problems with aliases, this is easier to solve with a shell function: 像大多数别名问题一样,使用shell函数更容易解决:

cws() {
    cd /var/mywork/swag/"$WSP"
}

Like melpomene said, you are better off using a function, but if you, for whatever reason, want to stick with an alias, you can do 就像melpomene所说的那样,最好还是使用函数,但是如果您出于某种原因想要坚持使用别名,则可以

alias cws='cd /var/mywork/swag/$WSP'

But note that this differs from using a shell function in one point, which is related to setting a variable for just one command: If you write 但是请注意,这与在某一点上使用shell函数有所不同,后者与仅为一个命令设置变量有关:如果您编写

WSP=aaa
...
WSP=xxx
...
WSP=yyy cws

and cws is defined as a function, this would cd to yyy, but if it is an alias, it would cd to xxx. 并且cws被定义为函数,它将cd转换为yyy,但是如果它是别名,则将cd转换为xxx。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM