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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With