Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globbing/pathname expansion with colon as separator

Tags:

How can I convert a string containing glob characters such as

/var/lib/gems/*/bin 

into a colon-separated string of filenames (i.e. PATH compatible) matching the pattern?

i.e. echo /var/lib/gems/*/bin will return

/var/lib/gems/1.8/bin /var/lib/gems/1.9.1/bin 

I want

/var/lib/gems/1.8/bin:/var/lib/gems/1.9.1/bin  

instead.

The obvious approach is simply to replace the space character with ':' via tr, but that doesn't work if the filename itself contains the space character.

like image 596
mjs Avatar asked Aug 07 '10 13:08

mjs


1 Answers

Actually, I thought of a better solution: use a shell function.

function join() {     local IFS=$1     shift     echo "$*" }  mystring=$(join ':' /var/lib/gems/*/bin) 
like image 146
Sean Avatar answered Nov 09 '22 18:11

Sean