-1

python/bash how to escape character '!'

I am try to run in Python this

c.run('sqlcmd -U nrv -P PP!asword -Q "select top 3 * from sys.databases"', pty = True)

code in brackets - bash code

it works in terminal in that manner:

sqlcmd -U nrv -P PP\!asword -Q "select top 3 * from sys.databases"

in genereal i try nothing meaningful

Rusteme
  • 1
  • 2

2 Answers2

0

Have you tried simply prefixing the "!" with "\" inside your single-quotes in the first form of the equation ? Single-quotes are meant to be taken as literals, so the backslash will be passed thru, exactly as your command-line attempt.

Eric Marceau
  • 1,601
  • 1
  • 8
  • 11
  • I tried. It doesn't work. In error message i see, that resulted password was PP\!asword PP\\!asword – Rusteme Dec 17 '22 at 09:52
0

has answer! c.sudo(r'''sqlcmd -U nrv -P PP!asword -Q "select top 3 * from sys.databases"''') (in that case I've changed run to sudo because sufficient privileges)

so, I newbie in Python, and heared about "sсreened" strings - and it simply works.it's even strange that a backslash was not required, because command in brackets - it is a bash, and it requires.

Rusteme
  • 1
  • 2
  • There is a well regarded answer already provided at Stack regarding escape characters, including!. The exhausting list of answers to bash escape characters are found at: https://stackoverflow.com/questions/15783701/which-characters-need-to-be-escaped-when-using-bash – Gray Dec 19 '22 at 22:35