Can't use sudo as an argument in a cap shell command #251

ghost opened this Issue Aug 15, 2012 · 2 comments

2 participants


When I try to run a cap shell command that uses sudo as an argument rather than a command, substitution that is only relevant for sudo when it is used as a command is performed.


$ cap shell
cap> rpm -q sudo

That fails since shell.rb contains

command = command.gsub(/\bsudo\b/, "sudo -p '#{configuration.sudo_prompt}'")

which mangles the command into

rpm -q sudo -p '\\''sudo password: '\\''

The rpm command expects a package file when it encounters the -p option. There is no package so it fails.

Similar issues occur when I try to run grep sudo, etc. from cap shell.

In lib/capistrano/shell.rb

command = command.gsub(/\bsudo\b/, "sudo -p '#{configuration.sudo_prompt}'")

should be:

command = command.gsub(/^sudo\b/, "sudo -p '#{configuration.sudo_prompt}'")

to only match sudo if it comes at the start of the line.


@nutznboltz Thank you for the thorough legwork! Would you mind submitting a pull request for review?


Thinking about this some more, sudo could come after pipe, semicolon, ampersand, double ampersand && and double pipe ||. There might be more cases than those.


cat foo | sudo bar

The gsub should be:

      command = command.gsub(/^(\s*)sudo\b|([|;&])\s*sudo\b/, "\\0 -p '#{configuration.sudo_prompt}'")
@leehambley leehambley closed this Apr 2, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment