Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tilde expansion in quotes

Tags:

bash

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.

like image 692
tijagi Avatar asked Apr 07 '13 04:04

tijagi


1 Answers

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.

like image 106
Gordon Davisson Avatar answered Sep 28 '22 17:09

Gordon Davisson