0

I have created this alias to replace rm with gio trash ...

alias rm='function move2trash(){
    if hash gio &> /dev/null; then
        gio trash "$@" && \
        echo "File(s) moved to trash instead." && \
        echo "See \`man gio\` for more information."
    else
        echo "Removing file(s) is a bad habbit, instead you should move or rename them."
        echo "If you really have to remove some thing(s) you can use the full path i.e. \`/bin/rm\`."
    fi
}; move2trash'

But files get lost when running sudo in front of it ...

[~] touch test.file
[~] sudo rm test.file 
[~] sudo ls /root/.local/share/Trash/files/test.file
/root/.local/share/Trash/files/test.file
[~] touch test.file2
[~] sudo rm test.file2 
[~] sudo ls /root/.local/share/Trash/files/test.file2
ls: cannot access '/root/.local/share/Trash/files/test.file2': No such file or directory
[~, ERR:2] 

Also I get errors when running options or variables with rm ...

[~] touch test.file3
[~] LC_ALL=C rm test.file3 
bash: Syntaxfehler beim unerwarteten Symbol »(«
[~, ERR:2] rm -i test.file3 
gio: Unbekannte Option -i

Aufruf:
  gio trash [OPTION …] [ORT…]

Dateien oder Ordner in den Papierkorb verschieben/wiederherstellen.

Optionen:
  -f, --force     Nicht vorhandene Dateien ignorieren und niemals nachfragen
  --empty         Den Papierkorb leeren
  --list          Dateien im Papierkorb mit ihren ursprünglichen Orten auflisten
  --restore       Eine Datei aus dem Papierkorb am ursprünglichen Ort wiederherstellen (erstellt unter Umständen wieder den Ordner)

Hinweis: Wenn der ursprüngliche Ort der gelöschten Datei bereits existiert, wird er mit der Option »--restore« nicht überschrieben, außer »--force« ist gesetzt.

Hope you guys have some advice to make this alias more reliable, or at least explain to me why this alias doesn't work with sudo and how to remove options.

Mario
  • 679
  • 6
  • 10
  • Why would you make an alias that first defines a function and then runs it, instead of _just making a function (instead of an alias) in the first place_? – Charles Duffy Dec 20 '21 at 16:47
  • Mind, aliases _never_ worked with sudo in the first place -- never have, never will. They're simple prefix substitution, and they're part of process-local state; when you invoke an `execve()` call to invoke a different executable (like sudo), the original shell's state is replaced with a clean image of the `sudo` executable. – Charles Duffy Dec 20 '21 at 16:47
  • ...one thing you _can_ do with functions (but not aliases) is export them through the environment; but whether that makes them survive sudo depends on the details of your `/etc/sudoers` file -- if sudo is configured to discard environment variables, exported functions won't make it through either. And you need to have a shell on the other side of the sudo to execute that function, even if it _is_ exported -- by default, sudo just directly invokes the requested executable with no shell in the way. – Charles Duffy Dec 20 '21 at 16:51
  • So what you'd typically end up with is needing something like `sudo_move2trash() { sudo bash -c "$(declare -f move2trash); "'move2trash "$@"' _ "$@"; }` (and then to use `sudo_move2trash` instead of `sudo move2trash`) _in addition to_ a bare function declaration of `move2trash`. – Charles Duffy Dec 20 '21 at 16:52
  • Also -- see https://wiki.bash-hackers.org/scripting/obsolete about the `function` keyword. `function funcname() {` combines the POSIX syntax `funcname() {` and the legacy ksh syntax `function funcname {` in a way that's incompatible with _both_ POSIX sh and legacy ksh. – Charles Duffy Dec 20 '21 at 16:52

0 Answers0