LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-24-2006, 05:56 AM   #1
das_Schaf
LQ Newbie
 
Registered: Oct 2004
Posts: 15

Rep: Reputation: 0
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.
 
Old 06-24-2006, 06:10 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 06-24-2006 at 06:57 AM.
 
Old 06-24-2006, 06:39 AM   #3
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
In Bash, the following will work:
Code:
export foo=$'\x41\x42\x43\x44'
 
Old 06-24-2006, 06:52 AM   #4
das_Schaf
LQ Newbie
 
Registered: Oct 2004
Posts: 15

Original Poster
Rep: Reputation: 0
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!
 
Old 06-24-2006, 06:58 AM   #5
vls
Member
 
Registered: Jan 2005
Location: The grassy knoll
Distribution: Slackware,Debian
Posts: 192

Rep: Reputation: 31
A plain echo won't convert your hex. echo -e does.
Code:
$ xxx='\x41\x42\x43'
$ echo -e $xxx
ABC
 
Old 06-24-2006, 07:04 AM   #6
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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.]
 
Old 06-24-2006, 07:17 AM   #7
vls
Member
 
Registered: Jan 2005
Location: The grassy knoll
Distribution: Slackware,Debian
Posts: 192

Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Special Characters SimeonV SUSE / openSUSE 14 07-07-2006 01:29 PM
special characters greenbox Linux - Software 9 12-23-2005 07:33 PM
Special characters consty Programming 3 08-07-2005 05:53 AM
using special characters one_ro Mandriva 5 11-04-2004 08:52 AM
really easy question: why won't my environment variables export digitized_funk Linux - Newbie 3 10-22-2003 05:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration