« first day (800 days earlier)      last day (1646 days later) » 

11:46 AM
@EliahKagan when I run your command, it only opens the file in nano
 
 
6 hours later…
5:47 PM
@Zanna Which command?
sudo SUDO_EDITOR='sed -i 1s/^/#/' visudo -f /etc/sudoers.d/keep-home
?
 
yes
 
And it's empty in nano?
 
no
since I had given it the same contents as yours
 
And it showed those contents correctly?
 
yes
 
5:52 PM
What Ubuntu release is this on?
 
18.04
 
I ran the command on 19.10. I don't know why it doesn't work on 18.04.
 
I am wondering what silly thing I'm doing
 
Nothing?
I assume the command just doesn't work on 18.04, having to do with what works as an editor for sudo -e, sudoedit, and visudo in what versions of sudo.
That wouldn't change the behavior of sudo on 18.04, since before 19.10 it already preserves $HOME, but that doesn't address why sed -i 1s/^/#/ couldn't be used as an "editor."
Oh.
Is SUDO_EDITOR not used until recent versions of sudo?
That appears to be the case for visudo specifically.
 
I don't know, but it's not set
 
5:59 PM
What happens if you use EDITOR or VISUAL instead of SUDO_EDITOR?
 
zanna@xubi:~$ sudo EDITOR='sed -i 1s/^/#/' visudo -f /etc/sudoers.d/keep-home
zanna@xubi:~$ cat $_
#Defaults	env_keep+="HOME"
works
 
On my 16.04 and 18.04 systems, the manpage for visudo doesn't mention SUDO_EDITOR. It appears that, until recently, SUDO_EDITOR has been used only by sudo -e and sudoedit, not by visudo. I also just got the same behavior you saw--where my default interactive editor opens--when I tried the command on a 16.04 system.
 
I don't understand why adding a space is a problem
@EliahKagan I guess it's another upgrade :)
 
@Zanna I do consider having visudo use SUDO_EDITOR to be an improvement.
@Zanna sed -i 1s/^/#/ is parsed as three arguments, like we want.
Adding a space after # causes it to be parsed as four arguments.
 
why is that? A quoting issue?
 
6:03 PM
Not a quoting issue in the shell.
sudo -e/sudoedit and visudo are not using the value of EDITOR, VISUAL, or SUDO_EDITOR as a command name. If they were, this wouldn't work at all; there is no command called sed -i 1s/^/#/. What allows that to work is that the value is being used as a command name followed by zero or more arguments to the command. The filename of the temporary file is passed as a final argument after that.
The variable's value is split into arguments (or, if one doesn't consider the command name itself to be an argument, then split into a command name and arguments) at the spaces.
The parsing of sed -i 1s/^/#/ is done by sudo -e/sudoedit or visudo (or something they call). It's independent of the shell that's used to run them with an environment variable set to that value, and it doesn't observe the same rules as the shell does.
I don't know of any way to prevent it from being split on spaces.
 
this is very interesting!
 
Yeah, it's cool. :)
In particular, preceding the space with a backslash does not work.
This situation is similar to how hashbang lines are parsed, except that in this situation it can be split into many arguments.
With GNU sed, I should be able to give a hex or octal code for the space character.
I could also make a temporary script and run it.
Neither of those solutions is especially appealing, though.
None of this is in any way necessary or even helpful for my sudo / $HOME answer.
@Zanna ...but you can see why I've allowed myself to be distracted by it. :)
 
@EliahKagan haha yes definitely
 

« first day (800 days earlier)      last day (1646 days later) »