如何将从bash转换到fish的导出行转换为fish(变量扩展快捷方式)?

huangapple go评论106阅读模式
英文:

How to convert export line from bash to fish(variable expansion shortcuts)?

问题

export LIB="{LIB:-}${LIB:+:}/example/lib"

如果在fish中运行,会显示:

${ is not a valid variable in fish.

有什么方法可以将这行代码转换成fish语法吗?

英文:

I have a bash profile as to add a ':' if LIB exist, skip the ':' when LIB not defined.

export LIB="{LIB:-}${LIB:+:}/example/lib"

If run in fish, it will show

 ${ is not a valid variable in fish.

Any trick to convert this line to fish?

答案1

得分: 2

In short: In this simple case, try

set -gx LIB (string join : -- $LIB /example/lib)

This will construct a string with string join - if $LIB was set, it'll add it and a : before "/example/lib", if it wasn't it won't.

The underlying thing here is that fish doesn't have these variable expansion shortcuts like ${foo:+-/%#} and such.

The intention here is to do something similar to what you would do with $PATH - "add :/foo if $PATH is set, or set it to /foo otherwise".

For $PATH, because it is a list in fish (as it is a PATH variable - these are automatically joined/split with ":"), you would simply do

set -gx PATH $PATH /foo

You could define your variable $LIB as a path variable like

set -gx --path LIB $LIB /example/lib

and fish would do the same for it. Now inside fish it would be accessible as a list so you could use $LIB[1] to get the first element, and outside of fish it would be available in the colon-delimited form so applications know what to do with it.

But if you don't want to do that, see the string join method above.

英文:

In short: In this simple case, try

set -gx LIB (string join : -- $LIB /example/lib)

This will construct a string with string join - if $LIB was set, it'll add it and a : before "/example/lib", if it wasn't it won't.

The underlying thing here is that fish doesn't have these variable expansion shortcuts like ${foo:+-/%#} and such.

The intention here is to do something similar to what you would do with $PATH - "add :/foo if $PATH is set, or set it to /foo otherwise".

For $PATH, because it is a list in fish (as it is a PATH variable - these are automatically joined/split with ":"), you would simply do

set -gx PATH $PATH /foo

You could define your variable $LIB as a path variable like

set -gx --path LIB $LIB /example/lib

and fish would do the same for it. Now inside fish it would be accessible as a list so you could use $LIB[1] to get the first element, and outside of fish it would be available in the colon-delimited form so applications know what to do with it.

But if you don't want to do that, see the string join method above.

huangapple
  • 本文由 发表于 2023年2月18日 16:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定