${var} 参数扩展表达式可以嵌套在 bash 中吗? [英] Can ${var} parameter expansion expressions be nested in bash?
问题描述
我拥有的是这个:
progname=${0%.*}
progname=${progname##*/}
这是否可以嵌套(或不嵌套)为一行,即单个表达式?
Can this be nested (or not) into one line, i.e. a single expression?
我试图从脚本名称中去除路径和扩展名,以便只保留基本名称.以上两行工作正常.我的C"天性只是驱使我更加混淆这些.
I'm trying to strip the path and extension off of a script name so that only the base name is left. The above two lines work fine. My 'C' nature is simply driving me to obfuscate these even more.
推荐答案
如果说嵌套,你的意思是这样的:
If by nest, you mean something like this:
#!/bin/bash
export HELLO="HELLO"
export HELLOWORLD="Hello, world!"
echo ${${HELLO}WORLD}
那么不,你不能嵌套 ${var}
表达式.bash 语法扩展器不会理解它.
Then no, you can't nest ${var}
expressions. The bash syntax expander won't understand it.
但是,如果我理解您的问题,您可能会考虑使用 basename
命令 - 它从给定的文件名中去除路径,如果给定扩展名,也会去除它.例如,运行 basename/some/path/to/script.sh .sh
将返回 script
.
However, if I understand your problem right, you might look at using the basename
command - it strips the path from a given filename, and if given the extension, will strip that also. For example, running basename /some/path/to/script.sh .sh
will return script
.
这篇关于${var} 参数扩展表达式可以嵌套在 bash 中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!