0

I know that it's easy to escape $ in python if you want to print it:

>>> char='$'
>>> print(f'\{char}')
\$

but that's not what I need. I want to save it in a variable; however, i'll always have double backslashes instead of single backslash.

>>> f'\\{char}'
'\\$'
>>> f'\{char}'
'\\$'
>>> f'\\{char}'
'\\$'
>>> repr(f'\{char}')
"'\\\\$'"
>>> repr(f'\\{char}')
"'\\\\$'"

Therefore my_escaped_string = f'\{char}' will be \\$ instead of \$. As this will be used to escape some special characters in a password, which later on will be sent to bash, bash will have real problems with double backslashes.

I've also tried escaped_str = "\" + char, and so on. However, I can't manage to append just a single backslash in from of the escaped character.

Does somebody have enough inspiration to solve it? ChatGPT insists on dumb solutions.

i have my_pass = '1234$5678'and I want to transform it in my_escaped_pass => '1234\\$5678' instead of '1234\\\\$5678'

Later edit: I will do something like that:

command=f"echo {password} | sudo passwd monitoringuser --stdin"
ssh.execute_command(command)

At the moment I execute the command, if the password is 1234$5678 or 1234\\$5678 it's bad. It has to be 1234\$5678

7
  • 2
    You don't need to escape $ in Python. Just write it directly. Commented Dec 14, 2023 at 16:18
  • f'{my_pass[:4]}${my_pass[4:]}' Commented Dec 14, 2023 at 16:20
  • 1
    "however, i'll always have double backslashes instead of single backslash." No there is only a single backslash in that string, it is merely escaped in the string repr, there is no problem here Commented Dec 14, 2023 at 16:24
  • Thanks. I know you don't have to escape it in python, but that string will go to bash, and i have to do the escaping before sending the string to bash. If bash receives $ unescaped, it doesn't handle it well: ``` ~$ ssh -q $myserver "echo '3deBJ#$K0Sh7*8*-$Mva'" 3deBJ#*8*- ~$ ssh -q $myserver "echo '3deBJ#\$K0Sh7*8*-\$Mva'" 3deBJ#$K0Sh7*8*-$Mva ``` Commented Dec 14, 2023 at 16:31
  • 1
    Simply using '\$' in the Python code should be sufficient. Don't get distracted by the double backslashes. Commented Dec 14, 2023 at 16:32

1 Answer 1

2

You're getting confused between a string and the representation of a string.

❯ s = "\$"

❯ print(s)
\$

❯ s
'\\$'

If when you pipe this to bash, you want a \ before the $, just put one there.

i ❯ my_pass = "abcdef"

i ❯ val = f'{my_pass[:3]}\${my_pass[3:]}'

i ❯ print(val)
abc\$def

i ❯ val
'abc\\$def'

In response to the comment...

I don't think you're right about this. When you run print(), you are effectively writing to a file called /dev/stdout (on Mac and Linux), so doing print(x), is no different from writing x to a file.

i ❯ with open("foo", "w") as f:
...     f.write(val)
...

i ❯ cat foo
abc\$def

As you can see, even if I dump val to a file, it doesn't have two slashes.

In the repr, \\ means a literal slash.

Here are some other options which might shed more light:

Escaping string termination

\" is a single character and it means the literal " character. Imaging if A was the literal " character, "A is an unterminated string.

i ❯ "\"
  Input In [32]
    "\"
    ^
SyntaxError: unterminated string literal (detected at line 1)

Literal \

As \ usually means "escape the next character", if the next character is usually escapable AND we actually want a \ character, we have to indicate that we literally want that character, and not for it to perform its usual job.

So \\ is a single \ character. The repr shows its escaped state, but if we print it, we see it's just a single character.

i ❯ "\\"
'\\'
i ❯ print("\\")
\
Sign up to request clarification or add additional context in comments.

5 Comments

I know print() will print 1 backslash, because the backslash is an escape character in python too. The first backslash escapes the second one, that's why the second one gets printed. But if you try to use the val for anything else than printing, execute a bash command in my case, you'll have two backslashes there up to the point when bash will give an error because there are two backslashes instead of one in that variable.
@USER_RANDOM_010101NR There are NOT two backslashes in the string! repr displays a backlash as escaped. It is a display representation only!
blueteeth, thank you for clarification. That brought some light, thank you. Probably I've put the wrong question trying to oversimplify it. It is possible that whenever you want to add a backslash to a variable, python store that as double backslash so that it can be escaped "at the moment of use" like print, write to file etc.? part 1/2
It looks to me that when I try to use that as a part of a command which will be executed on an ssh server with paramiko library, the $ gets interpreted by the bash on the remote server. To avoid that, I escape it in python using f'\$' but then it will get executed on the ssh server as \\$ part2/2
i've figured out. I was confused about a few things which distracted me from the real problem. I was escaping another character(!) which in my case should not be escaped. Because of escaping !, I have actually just modified my password so the post-check comparison failed. Indeed, the answers I got were good, it was just a bit harder for me to shift my point of view. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.