shell variable interpolation?
Joshua McKinney
clug at bitfed.com
Mon Nov 18 14:00:43 EST 2002
On Mon, Nov 18, 2002 at 11:48:27AM +1100, Steven Hanley wrote:
> All
>
> Okay so I am wondering if there is a better way to interpolate variables
> within a string in shell than I have worked out here.
> (seeing how we have some sick disturbed individuals who do things with sh
> that it was probably never meant to do (hi Michael) I thought I might ask.
>
> I have
>
> ====
> #! /bin/sh
>
> multi_apply () {
> local string=$1
> local list=$2
> for item in $list ; do
> line=`eval echo "$string"` echo $line
> done
> }
>
> a1="one long \$item argument"
> a2="and another one"
>
> multi_apply "$a1" "$a2"
> ====
>
> which when I run it outputs
>
> one long and argument
> one long another argument
> one long one argument
>
> the idea is to pass in and a string in which the items should be inserted.
> This seems to work fine, I wonder however if there is a better/more
> efficient/faster than this.
>
> Ideas/Suggestions?
>
> See You
> Steve
>
> --
> sjh at wibble.net http://wibble.net/~sjh
> You are subtle as a window pane standing in my view
> but I will wait for it to rain so that I can see you
> Anticipate - Ani
Try:
for item in $a2 ; do
echo "${a1/placeholder/$item}"
done
--
If I can't fix it, it aint broke.
More information about the linux
mailing list