今天在执行shell命令时发现使用sudo后命令一直找不到,google了一下,解决了问题,顺便学习了一下相关的知识。

以下的内容来自StackOverflow上的回答。

To understand what's going on here, you need to understand a little background information about how shells (bash in this case) are run.

  • When you open a terminal emulator (gnome-terminal for example), you are executing what is known as an interactive, non-login shell.
  • When you log into your machine from the command line, via ssh, or run a command such as su - username, you are running an interactive login shell.
  • When you log in graphically, you are running something completely different, the details will depend on your system and graphical environment but in general it is the graphical shell that deals with your login. While many graphical shells (including the Ubuntu default) will read /etc/profile not all of them do.

Finally, when you run a shell script, it is run in a non-interactive, non-login shell.

The various configuration files are read sequentially. Specifically, for login shells, the order is:

/etc/profile -> /etc/profile.d/* (in alphabetical order) -> ~/.profile

This means that any setting in ~/.profile will overwrite anything set in the previous files.