0

I am using os.environ['my_key'] to read in a key from my .bashrc. For example, if my_key="123$abc" were in .bashrc then os.environ['my_key'] would return 123bc.

Is there a trick to read in the full key?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jason P
  • 15
  • 3
  • 1
    If you have the string `my_key="123$abc"` in your .bashrc, that will set `my_key` to the string `123` concatenated with whatever the value of the variable `$abc` is. – William Pursell Dec 15 '21 at 01:08
  • Thanks William! So there is no way to use a string with $ in my .bashrc? The key would have to be changed, correct? – Jason P Dec 15 '21 at 01:19
  • @JasonP, or just use single quotes instead of double quotes. `export my_key='123$abc'` would work fine. The mistake being made here has nothing to do with Python -- you could see the same problem if you ran `echo "$my_key"` in bash. – Charles Duffy Dec 15 '21 at 01:25
  • @CharlesDuffy This worked also! Thank you, I toyed with this earlier but it probably did not work because I was not reloading my .bashrc settings in my code editor, thus reading in old .bashrc settings (just figured this out) – Jason P Dec 15 '21 at 01:35

1 Answers1

1

Try to escape $.

my_key="123\$abc"
过过招
  • 3,722
  • 2
  • 4
  • 11