Страницы

Поиск по вопросам

пятница, 16 ноября 2018 г.

Sudo: command not found на некоторых командах

Подскажите почему может не работать команды с sudo, если sudo -s работает?
$ sudo ll sudo: ll: command not found


Ответ

Это может происходить, если ll — псевдоним. Например:
$ which ll ll: aliased to ls -lh
В таком случае sudo не сработает, потому что псевдоним не распознаётся. Выход такой: добавьте в ваш ~/.bashrc (~/.zshrc) строку
alias sudo='sudo '
Теперь сработает:
$ sudo ll Password: total 1240 ...
Дело в том, что для распознавания псевдонима необходимо, чтобы первое слово в выполняемой команде было псевдонимом. Если последний символ значения псевдонима — пробел, то следующее за первым псевдонимом слово также будет проверяться как псевдоним.
Из bash manual
Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands. The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The characters ‘/’, ‘$’, ‘`’, ‘=’ and any of the shell metacharacters or quoting characters listed above may not appear in an alias name. The replacement text may contain any valid shell input, including shell metacharacters. The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to "ls -F", for instance, and Bash does not try to recursively expand the replacement text. If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.
Решение и цитата взяты из этого ответа на Ask Ubuntu: Aliases not available when using sudo
Кстати, не вижу большого смысла применять sudo к команде ls. Это сработает точно так же:
$ sudo ls $ ls -A

Комментариев нет:

Отправить комментарий