Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to expand variables from string?

2,164 views
Skip to first unread message

Olivier Langlois

unread,
Oct 16, 2007, 11:24:35 PM10/16/07
to
Hi,

First, sorry if I am cross-posting this question. I am not sure to
understand the difference between the 2 newsgroups.

My problem is that I have a shell variable containing a string
composed of variable names preceded with the expension operator ('$')
and I am fighting with the shell to find a way to make the expension.
Here is an example:

var1='$HOME'
echo $var1

will display $HOME but I would like to find a construction that would
allow me to print the $HOME value from the var1 variable.

Thank you for your assistance,
Olivier Langlois
http://www.olivierlanglois.net
http://blog.olivierlanglois.net

Ralf Goertz

unread,
Oct 17, 2007, 4:25:20 AM10/17/07
to
Olivier Langlois wrote:

> Hi,
>
> First, sorry if I am cross-posting this question. I am not sure to
> understand the difference between the 2 newsgroups.
>
> My problem is that I have a shell variable containing a string
> composed of variable names preceded with the expension operator ('$')
> and I am fighting with the shell to find a way to make the expension.
> Here is an example:
>
> var1='$HOME'
> echo $var1
>
> will display $HOME but I would like to find a construction that would
> allow me to print the $HOME value from the var1 variable.

eval echo $var1

works for me

Ralf

Crispie...@gmail.com

unread,
Oct 17, 2007, 6:48:20 PM10/17/07
to

Don't enclose the variable with quotes. For example:

HOME="Some Random Text"
TEST=$HOME
echo $TEST

This would then print the text "Some Random Text" without quotes which
is what you want ?

Glenn

FrenKy

unread,
Oct 28, 2007, 3:49:40 PM10/28/07
to
This solution has one problem:

HOME="Some Random Text"
TEST=$HOME
HOME="Some Random Text"
echo $TEST

I'd use Ralf's suggestion.

FrenKy

unread,
Oct 28, 2007, 5:38:05 PM10/28/07
to

HOME="Other Random Text"
echo $TEST

William Pursell

unread,
Oct 29, 2007, 1:57:22 AM10/29/07
to
On 17 Oct, 03:24, Olivier Langlois <olangl...@sympatico.ca> wrote:
>
> First, sorry if I am cross-posting this question. I am not sure to
> understand the difference between the 2 newsgroups.

Unaware of the existence of alt.comp.lang.shell.unix.bourne-bash,
I am grateful to you for exposing me to it. (Or it to me, rather.)

> My problem is that I have a shell variable containing a string
> composed of variable names preceded with the expension operator ('$')
> and I am fighting with the shell to find a way to make the expension.

eval is your friend:

$ FOO="foo's text"
$ BAR='$FOO'
$ echo $BAR
$FOO
$ eval echo $BAR
foo's text

(Perhaps I should have used a character other than '$' for my
prompt...)

haiyan...@gmail.com

unread,
Apr 12, 2013, 12:21:33 AM4/12/13
to
在 2007年10月17日星期三UTC+8上午11时24分35秒,Olivier Langlois写道:
Just try :
echo ${!var1}
0 new messages