问题描述
我编写了一个脚本,其中必须在可能包含波浪号的用户定义目录中找到一些文件(因此,可能有 user_defined_directory='~/foo'
).构造看起来像
I write a script where must find some files in a user-defined directory which may contain tilde (thus, it's possible to have user_defined_directory='~/foo'
). The construct looks like
found_files=$(find "$user_defined_directory" -type f … )
我使用引号来覆盖该路径中可能的空格,但根据手册页,波浪号扩展在引号中不起作用.我知道 :
运算符可能可以进行这种扩展,但我不知道如何在这里使用它.
I use quotes to cover possible spaces in that path, but tilde expansion does not work in quotes according to man page. I know about :
operator that probably can do this expansion, but I can’t figure out how to use it here.
'user-defined-directory' 取自用户 $HOME 目录中的另一个配置文件.它不是作为参数传递给我的脚本,而是从我编写的脚本中的另一个配置中解析出来的.
The ‘user-defined-directory’ is being taken from another configuration file in user $HOME directory. It is not passing to my script as a parameter, it’s being parsed from that another config in the script I write.
推荐答案
您可以使用 "${user_defined_directory/#~/$HOME}"
替换开头的~"带有当前用户主目录的字符串.请注意,这不会处理 ~username/subdir
格式,只会处理普通的 ~
.如果您需要处理更复杂的版本,则需要编写一个更复杂的转换器.
You can use "${user_defined_directory/#~/$HOME}"
to replace a "~" at the beginning of the string with the current user's home directory. Note that this won't handle the ~username/subdir
format, only a plain ~
. If you need to handle the more complex versions, you'll need to write a much more complex converter.
这篇关于引号中的波浪号扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!