0

I am trying to figure out the different components of Linux and how they work together, and I have a terminology related question. The terminal runs the shell, which is usually Bash. One can also run Linux commands (e.g. ls, mkdir and cp) in terminal. But then I learned that not all Linux commands are part of bash (or shell). Does that mean that the terminal does not run shell only?

3 Answers3

0

The short answer is YES. Commands are both internal and external to the shell.

The shell is merely a user interface, and (ignoring various standards), it it is up to the shell if it wants to implement commands internally or externally. For example, the busybox shell tries to implement nearly all basic commands internally, so it is the only required executable on an embedded system.

If you read the man page for bash, you will find a section called SHELL BUILT IN COMMANDS that lists internal commands. Some commands would not work if implemented externally, such as cd, export, source.

External commands are searched for in directories listed in $PATH. Some commands are available both internally and externally, and the two are not necessarily identical.

One example of that is the pwd command. The built in version gives you bash's idea of the current directory. The external one can be run as /bin/pwd and will give a different answer if you cd through a symlink to a directory.

Technically speaking, the external commands not built into the shell are not run by terminal, but by the shell itself. Having said that, you can tell the terminal specifically to start with a command that isn't the shell, or is a different shell.

user10489
  • 6,740
0

It probably depends on the terminal, but those I've used have been able to run any command (but many commands terminate so quickly that it's pointless to start a terminal to run them, whether they produce output does not matter). I remember using terminals to run screen (and then let screen start the appropriate number of shells).

As described in the other answer you've gotten, shells have a number of built in commands (some shells have more than other, so check the manual for the one you use), to have access to those, and as shells generally stay running (until you exit them), you should just let the terminal start as shell unless you know what you're doing. (In a few cases you can save a process, but unless you have very peculiar needs it won't hurt to have one more process running).

0

The terminal doesn't run anything: it's just the user interface. Or, for the processes the operating system runs, it's a particular sort of a "file", something that can be read from and written to using the read() and write() system calls. (Plus some special ones for terminals in specific.)

When you login via (say) SSH, the SSH server launches a shell and gives it file descriptors connected to (the device node representing) the terminal. If you login via a GUI and start a terminal emulator, pretty much the same happens, just replace the SSH server with the terminal emulator.

Which ever way you go, the shell can now print the prompt to and read commands from the terminal, and launch external programs (e.g. ls or git or whatever), which can also access the terminal. Or it can run builtin commands (read, echo) similarly.

(Note that you can run non-interactive shell sessions too. Running a shell script is pretty much the same, the input to the shell just then comes from a file and not a terminal. What ever the shell launches, then also gets input from the file. The point is that the programs mostly don't need to care if they're connected to a terminal or something else.)

On a usual general-purpose system, most of the commands you'd run are not built in to the shell, but external programs. On some embedded systems with Busybox, the common stuff may well be implemented within Busybox itself. But even then, you probably have some external application that actually does whatever it is the system is supposed to do, even if that's just a Python or Lua interpreter.

See also: What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'?

ilkkachu
  • 138,973