echo value of a variable formed by concatenation of string
echo value of a variable formed by concatenation of string
am 06.09.2007 14:54:02 von anshul makkar
Hi,
I am writing a simple script where I need to concatenate two strings
to form a new variable and print its value
summerwinter=192.168.2.1
season1='summer'
season2='winter'
echo "[${season1}] + [${season2}] = $[${season1}${season2}]"
This script gives the error :
192.168.2.1: syntax error in expression (error token is ".168.2.1")
If instead of summerwinter=192.18.2.1 , I give summerwinter=100, then
script runs correctly.
I know that error is due to misinterpretation of "." but I am not able
to figure out the alternative.
Please if you have any suggestion or hint, then please do share it.
Thanking You
Anshul Makkar
Re: echo value of a variable formed by concatenation of string
am 06.09.2007 15:04:31 von mahurshi
On Sep 6, 5:54 am, anshul makkar wrote:
> Hi,
>
> I am writing a simple script where I need to concatenate two strings
> to form a new variable and print its value
>
> summerwinter=192.168.2.1
> season1='summer'
> season2='winter'
> echo "[${season1}] + [${season2}] = $[${season1}${season2}]"
>
> This script gives the error :
> 192.168.2.1: syntax error in expression (error token is ".168.2.1")
>
> If instead of summerwinter=192.18.2.1 , I give summerwinter=100, then
> script runs correctly.
>
> I know that error is due to misinterpretation of "." but I am not able
> to figure out the alternative.
>
> Please if you have any suggestion or hint, then please do share it.
>
> Thanking You
> Anshul Makkar
Have you tried using the ' for your summerwinter variable too?
summerwinter='192.168.2.1'
Mahurshi Akilla
Re: echo value of a variable formed by concatenation of string
am 06.09.2007 15:09:57 von Ed Morton
anshul makkar wrote:
> Hi,
>
> I am writing a simple script where I need to concatenate two strings
> to form a new variable and print its value
>
> summerwinter=192.168.2.1
> season1='summer'
> season2='winter'
> echo "[${season1}] + [${season2}] = $[${season1}${season2}]"
>
> This script gives the error :
> 192.168.2.1: syntax error in expression (error token is ".168.2.1")
>
> If instead of summerwinter=192.18.2.1 , I give summerwinter=100, then
> script runs correctly.
>
> I know that error is due to misinterpretation of "." but I am not able
> to figure out the alternative.
No, it's because of you putting a "$" outside of the "[...]". I think
what you meant to do was this:
eval echo "[${season1}] + [${season2}] = [\$${season1}${season2}]"
Regards,
Ed.
Re: echo value of a variable formed by concatenation of string
am 06.09.2007 16:02:19 von Bill Marcum
On Thu, 06 Sep 2007 12:54:02 -0000, anshul makkar
wrote:
>
> Hi,
>
> I am writing a simple script where I need to concatenate two strings
> to form a new variable and print its value
>
> summerwinter=192.168.2.1
> season1='summer'
> season2='winter'
> echo "[${season1}] + [${season2}] = $[${season1}${season2}]"
>
> This script gives the error :
> 192.168.2.1: syntax error in expression (error token is ".168.2.1")
>
> If instead of summerwinter=192.18.2.1 , I give summerwinter=100, then
> script runs correctly.
>
> I know that error is due to misinterpretation of "." but I am not able
> to figure out the alternative.
>
> Please if you have any suggestion or hint, then please do share it.
>
$[ ] is a deprecated syntax for arithmetic expansion. eval is your friend.
eval echo "[${season1}] + [${season2}] = [\$${season1}${season2}]"
--
Is it possible that software is not like anything else, that it is meant to
be discarded: that the whole point is to always see it as a soap bubble?
Re: echo value of a variable formed by concatenation of string
am 07.09.2007 06:47:56 von anshul makkar
On Sep 6, 6:04 pm, Mahurshi Akilla wrote:
> On Sep 6, 5:54 am, anshul makkar wrote:
>
>
>
> > Hi,
>
> > I am writing a simple script where I need to concatenate two strings
> > to form a new variable and print its value
>
> > summerwinter=192.168.2.1
> > season1='summer'
> > season2='winter'
> > echo "[${season1}] + [${season2}] = $[${season1}${season2}]"
>
> > This script gives the error :
> > 192.168.2.1: syntax error in expression (error token is ".168.2.1")
>
> > If instead of summerwinter=192.18.2.1 , I give summerwinter=100, then
> > script runs correctly.
>
> > I know that error is due to misinterpretation of "." but I am not able
> > to figure out the alternative.
>
> > Please if you have any suggestion or hint, then please do share it.
>
> > Thanking You
> > Anshul Makkar
>
> Have you tried using the ' for your summerwinter variable too?
>
> summerwinter='192.168.2.1'
>
> Mahurshi Akilla
Yes , I tried that, but it did'nt work.
Anshul Makkar
Re: echo value of a variable formed by concatenation of string
am 07.09.2007 06:50:34 von anshul makkar
On Sep 6, 6:09 pm, Ed Morton wrote:
> anshul makkar wrote:
> > Hi,
>
> > I am writing a simple script where I need to concatenate two strings
> > to form a new variable and print its value
>
> > summerwinter=192.168.2.1
> > season1='summer'
> > season2='winter'
> > echo "[${season1}] + [${season2}] = $[${season1}${season2}]"
>
> > This script gives the error :
> > 192.168.2.1: syntax error in expression (error token is ".168.2.1")
>
> > If instead of summerwinter=192.18.2.1 , I give summerwinter=100, then
> > script runs correctly.
>
> > I know that error is due to misinterpretation of "." but I am not able
> > to figure out the alternative.
>
> No, it's because of you putting a "$" outside of the "[...]". I think
> what you meant to do was this:
>
> eval echo "[${season1}] + [${season2}] = [\$${season1}${season2}]"
>
> Regards,
>
> Ed.
Yes , it worked. Thanks a lot.
Anshul Makkar
Re: echo value of a variable formed by concatenation of string
am 08.09.2007 09:42:09 von Cyrus Kriticos
anshul makkar wrote:
>
> I am writing a simple script where I need to concatenate two strings
> to form a new variable and print its value
>
> summerwinter=192.168.2.1
> season1='summer'
> season2='winter'
> echo "[${season1}] + [${season2}] = $[${season1}${season2}]"
>
> This script gives the error :
> 192.168.2.1: syntax error in expression (error token is ".168.2.1")
>
> If instead of summerwinter=192.18.2.1 , I give summerwinter=100, then
> script runs correctly.
[bash]
$ summerwinter=192.168.2.1
$ season1='summer'
$ season2='winter'
$ foo="${season1}${season2}"
$ echo ${!foo}
192.168.2.1
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide