sudo的时候用alias

有时会在sudo的时候用上别名,那么有些什么坑呢,感谢站长指导

先说解决方法

一句命令

1
debian@debian:~$ alias sudo='sudo '

或者加到 ~/.bashrc 等地方

再说过程

比如,我目前是普通用户 debian,然后我定义了一个别名 me

1
debian@debian:~$ alias me='whoami'

1
2
debian@debian:~$ me
debian

这时,我们在终端输 sudo me,肯定是不行的

1
2
debian@debian:~$ sudo me
sudo: me: command not found

在站长的帮助下,输入以下命令,就解决了

1
debian@debian:~$ alias sudo='sudo '

然后我们试一下

1
2
3
4
debian@debian:~$ me
debian
debian@debian:~$ sudo me
root

原理

先看下文档

先看下文档

Free Software Foundation, Inc.alias

或者直接在终端敲

1
alias --help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
NAME
alias - Define or display aliases.

SYNOPSIS
alias [-p] [name[=value] ... ]

DESCRIPTION
Define or display aliases.

Without arguments, `alias' prints the list of aliases in the reusable
form `alias NAME=VALUE' on standard output.

Otherwise, an alias is defined for each NAME whose VALUE is given.
A trailing space in VALUE causes the next word to be checked for
alias substitution when the alias is expanded.

Options:
-p Print all defined aliases in a reusable format

Exit Status:
alias returns true unless a NAME is supplied for which no alias has been
defined.

SEE ALSO
bash(1)

IMPLEMENTATION
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later

其中有一句

1
A trailing space in VALUE causes the next word to be checked for alias substitution when the alias is expanded.

大致意思是,如果右边的VALUE以空格结尾,则会检查空格后的那个单词是否定义别名

那么还是以上面的 sudo me 为例

也就是说在不设设置 alias='sudo '的时候,bash只会检查第一个单词sudo是否定义别名,后面me的不会作为别名去检查

加了后当然就正确了

0%