Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 3
    If the command used quotes, this isn't necessarily equivalent. Consider echo "foo bar" which would become sh -c "echo "foo bar"" which would just print foo instead of foo bar. Commented Jun 20, 2014 at 15:24
  • I tried sudo sh -c '!!' instead if your command but that doesn't work either Commented Jun 20, 2014 at 15:29
  • Then what if your command used both single and double quotes? Or double quotes and variable expansions? If the user did: a=b and then echo "$a" >file and then sudo sh -c '!!' it would expand to sudo sh -c 'echo $a >file' which would print an empty line instead of b into file. Commented Jun 20, 2014 at 15:34
  • So maybe there is an even better solution somehow? Commented Jun 20, 2014 at 15:40
  • 1
    I don't think there's any solution that will work 100% of the time without you ever needing to modify the command. Using sudo to get a root shell and then copying and pasting the command into it would be closest, but fails if the command was expected to use variables that were set in your non-root shell. Using sudo sh -c "!!" works fine for simple commands, but is fraught for complex ones. I think that the best advice is to not expect that there is some command that will work 100% of the time - learn some possible solutions and apply each when it best fits. Commented Jun 20, 2014 at 15:48