Search code examples
linuxbashpasswordsecho

"echo password | sudo -S" with exclamation mark


What I want to do is

$ echo password!p | sudo -S [command]

in a linux terminal (in my case, ubuntu20.04). If you type it as it is, the exclamation mark will refer to the history of the command line, and if !p=pwd, it will be a string like passwordpwd and will not send the password!p correctly. I have tried every way I could find to escape the exclamation, like 'password!p', 'password!p', '\''password!p'\'', etc. The last one behaves the same as 'echo |' but did not result in using the command. Is there any way to get through this?


Solution

  • Could try turning off history expansion.

    set +o histexpand
    echo -n "password!p" | sudo -S command
    set -o histexpand # if you want to restore the capability after