英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论