LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to retrieve variable value having dollar "$" in value (https://www.linuxquestions.org/questions/linux-general-1/how-to-retrieve-variable-value-having-dollar-%24-in-value-909124/)

bharatlinux 10-20-2011 03:56 AM

How to retrieve variable value having dollar "$" in value
 
I am unable to retrieve complete value of variable having $ in middle of its name.

Eg:
name=SsoFilterProdModeHandler$HttpServletRequestWrapperWithActions.class

Now I want to retieve the variable "name" as complete value "SsoFilterProdModeHandler$HttpServletRequestWrapperWithActions.class"

But actually its retriving "SsoFilterProdModeHandler" only.

I know the shell treats the text after $ as an variable.

But is there any way to retrieve the complete value.

Thanks!!

colucix 10-20-2011 03:59 AM

You need to embed the whole value with single quotes or use the escape character \ before $.

bharatlinux 10-20-2011 04:05 AM

Thanks Colcuix!!

I tried using \\ escape character it works perfectly fine.

The issue is I need to handle this while the variable is retrieved and I dont have to insert \\ in variable value.

I am trying to use tr,sed,cut...

Can you suggest a solution on this??

velle frak 10-20-2011 04:06 AM

Try putting a backslash \ before the $.

*erm: some guys beat me to it. active forum here ;-) *

colucix 10-20-2011 04:10 AM

It depends on how do you retrieve the variable. For example if I do:
Code:

$ var1=$(echo 'something$somethingelse')
$ var2=$var1
$ echo $var2
something$somethingelse

it works flawlessly. Please, can you post the lines of code generating the string?

bharatlinux 10-20-2011 04:54 AM

Now here is the special case.

I cant include escape character nor the single quotes in the variable or variable value.

velle frak 10-20-2011 06:16 AM

Quote:

Originally Posted by bharatlinux (Post 4503277)
Now here is the special case.

I cant include escape character nor the single quotes in the variable or variable value.

Escape those too (e.g. \\ for \, \', etc.)?

David the H. 10-20-2011 12:54 PM

Would you mind showing us exactly what you're doing, rather than just vaguely describing it? Give us some detailed examples of how you're setting the variables, and how you are trying to use them later, along with the actual and expected output.

The details can be very important in matters such as this.


As one suggestion though, quotes can be combined. You can double-quote the part of the string that holds the variable, and single-quote the rest.

http://mywiki.wooledge.org/Quotes

Also, using the full expansion syntax can be used to separate variables embedded inside strings. Example:

Code:

$ line="foobar"

$ echo "thisisa$lineline"
thisisa

$ echo "thisisa${line}line"
thisisafoobarline

Oh, and please use [code][/code] tags around your code, to preserve formatting and to improve readability.


All times are GMT -5. The time now is 11:34 AM.