# \[nuby\] shell-like substitution in a string

**URL:** <https://rubytalk.org/t/nuby-shell-like-substitution-in-a-string/14191>\
**Category:** ruby-talk\
**Created:** [13 October 2004 16:13 UTC](https://rubytalk.org/t/nuby-shell-like-substitution-in-a-string/14191 "2004-10-13T16:13:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Warren\_Brown2](https://avatars.discourse-cdn.com/v4/letter/w/f17d59/32.png) [@Warren\_Brown2](https://rubytalk.org/u/Warren_Brown2)\
**Post date:** [13 October 2004 16:13 UTC](https://rubytalk.org/t/nuby-shell-like-substitution-in-a-string/14191/1 "2004-10-13T16:13:36Z")

</div>

Lionel,

> I'd like to make some kind of substitution like in a  
> shell, i.e. having:  
> "${tool} -o ${target} -L${libpath} -l${lib} ${source}"
> 
> with an env like this:  
> env = {  
> "tool" =\> "gcc",  
> "target" =\> "hello.exe",  
> "libpath" =\> "/usr/lib",  
> "lib" =\> "somelib",  
> "source" =\> "hello.c"  
> }
> 
> it should give:  
> "gcc -o hello.exe -L/usr/lib -lsomelib hello.c"

&nbsp;&nbsp;&nbsp;&nbsp;If there any problem with simply:

"#{env['tool']} -o #{env['target']} -L#{env['libpath']} -l#{env['lib']}  
#{env['source']}"

&nbsp;&nbsp;&nbsp;&nbsp;I hope this helps.

&nbsp;&nbsp;&nbsp;&nbsp;- Warren Brown

---

<div class="post-metadata">

**Author:** ![Lionel\_Thiry](https://avatars.discourse-cdn.com/v4/letter/l/8c91f0/32.png) [@Lionel\_Thiry](https://rubytalk.org/u/Lionel_Thiry)\
**Post date:** [14 October 2004 22:19 UTC](https://rubytalk.org/t/nuby-shell-like-substitution-in-a-string/14191/2 "2004-10-14T22:19:56Z")

</div>

Warren Brown a écrit :

> Lionel,
> 
> > I'd like to make some kind of substitution like in a  
> > shell, i.e. having:  
> > "${tool} -o ${target} -L${libpath} -l${lib} ${source}"
> > 
> > with an env like this:  
> > env = {  
> > "tool" =\> "gcc",  
> > "target" =\> "hello.exe",  
> > "libpath" =\> "/usr/lib",  
> > "lib" =\> "somelib",  
> > "source" =\> "hello.c"  
> > }
> > 
> > it should give:  
> > "gcc -o hello.exe -L/usr/lib -lsomelib hello.c"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;If there any problem with simply:
> 
> "#{env['tool']} -o #{env['target']} -L#{env['libpath']} -l#{env['lib']}  
> #{env['source']}"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;I hope this helps.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- Warren Brown

I want to manage those cases too:

Initial description:  
"${tool} -o ${target} -L${libpath} -l${lib} ${source}"

Env:  
env = {  
"tool" =\> "gcc",  
"target" =\> "hello.exe${ext}",  
"ext" =\> ".exe",  
"libpath" =\> "/usr/lib",  
"lib" =\> "somelib",  
"source" =\> "hello.o"  
}

Interpolation:  
"gcc -o hello.exe -L/usr/lib -lsomelib hello.c"

Or:  
Initial description:  
"${tool} -o ${target} -L${libpath} -l${lib} ${sources}"

Env:  
env = {  
"tool" =\> "gcc",  
"target" =\> "hello.exe${ext}",  
"ext" =\> ".exe",  
"libpath" =\> ["/usr/lib", "/usr/local/lib"],  
"lib" =\> ["somelib", "anotherlib"],  
"sources" =\> ["hello.o", "display.o", "other.o"]  
}

Interpolation:  
"gcc -o hello.exe -L/usr/lib -L/usr/local/lib -lsomelib -lanotherlib hello.o display.o other.o"

Then, no, simple ruby string substitution cannot fit into my goals. But thanks for the remark.

Lionel Thiry
