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
$in Python. Just write it directly.f'{my_pass[:4]}${my_pass[4:]}'repr, there is no problem here'\$'in the Python code should be sufficient. Don't get distracted by the double backslashes.