Say that you have a string of bytes generated via os.urandom(24)
,
b'\x1b\xba\x94(\xae\xd0\xb2\xa6\xf2f\xf6\x1fI\xed\xbao$\xc6D\x08\xba\x81\x96v'
and you'd like to store that in an environment variable,
export FOO='\x1b\xba\x94(\xae\xd0\xb2\xa6\xf2f\xf6\x1fI\xed\xbao$\xc6D\x08\xba\x81\x96v'
and retrieve the value from within a Python program using os.environ
.
foo = os.environ['FOO']
The problem is that, here, foo
has the string literal value '\\x1b\\xba\\x94...
instead of the byte sequence b'\x1b\xba\x94...
.
What is the proper export
value to use, or means of using os.environ
to treat FOO
as a string of bytes?
The easiest option is to simply set it as binary data in Bash. This uses ANSI string quoting and avoids the need for any sort of conversion on the Python side.
export FOO=$'\x1b\xba\x94(\xae\xd0\xb2\xa6\xf2f\xf6\x1fI\xed\xbao$\xc6D\x08\xba\x81\x96v'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With