LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to export special characters into environment variables? (http://www.linuxquestions.org/questions/linux-general-1/how-to-export-special-characters-into-environment-variables-457813/)

das_Schaf 06-24-2006 05:56 AM

How to export special characters into environment variables?
 
Hi,

I need to export special characters into an environment variable.

As most of us know usually
export foo="ABCD"
can be used to store the string "ABCD" in foo.
Now I need to export everything in hexadecimal form. So my "ABCD" would become "\x41\x42\x43\x44".
Naturally I tried to simply
export foo="\x41\x42\x43\x44"
.

However export did not register the backslash and thus export every character as a single standard-char.

So I ended up with
"'\' 'x' '4' '1' '\' 'x' '4' '2' '\' 'x' '4' '3' '\' 'x' '4' '4'"
stored in my variable.

Is there any way to actually export hexadecimal characters into environmental variables?

Thanks a lot in advance!


P.S.
Of course "ABCD" was only an example for the purpose of clarification.

druuna 06-24-2006 06:10 AM

Hi,

Which shell/environment are you using? I ask because in a bash environment I get the same result:

$ export xxx="\x41\x42\x43"
$ echo $xxx
\x41\x42\x43

In general, if you use single quotes, the contents is not treated special. Using double quotes will treat the content special.

Try using: export yyy='\x41\x42\x43'

You can also 'escape' the special chars by adding a backslash in front of (any) special char:

$ export xxx="\\x41\\x42\\x43"
$ echo xxx
\x41\x42\x43

Hope this helps.

spirit receiver 06-24-2006 06:39 AM

In Bash, the following will work:
Code:

export foo=$'\x41\x42\x43\x44'

das_Schaf 06-24-2006 06:52 AM

Thanks for the help!

The code issued by spirit receiver worked for me.

I also found an alternative way. Im going to post it in case anyone ever needs it:

You can let perl convert the hexadecimal characters into normal chars and then store these in the variable.
e.g.

export foo=`perl -e 'print "\x50\x9a\x05\x40"'`

Anyway:
thanks!

vls 06-24-2006 06:58 AM

A plain echo won't convert your hex. echo -e does.
Code:

$ xxx='\x41\x42\x43'
$ echo -e $xxx
ABC


spirit receiver 06-24-2006 07:04 AM

Well, on my system, Perl weighs about 1.2M. That's slightly too much for this task, I think. Bash's built-in "echo -e" should do just the same.

[Sorry, too late.]

vls 06-24-2006 07:17 AM

Quote:

Originally Posted by spirit receiver
Well, on my system, Perl weighs about 1.2M. That's slightly too much for this task, I think. Bash's built-in "echo -e" should do just the same.

[Sorry, too late.]

But the $'foo' syntax you provided is the way to go. Less typing.

I forgot about that bit of shell magic since I never have a need for it.


All times are GMT -5. The time now is 12:17 AM.