Built in shell commands in linux

what are shell built-in commands in linux?

I have just started using Linux and I am curious how shell built-in commands such as cd are defined. Also, I’d appreciate if someone could explain how they are implemented and executed.

6 Answers 6

If you want to see how bash builtins are defined then you just need to look at Section 4 of The Bash Man Page.

If, however, you want to know how bash bultins are implemented, you’ll need to look at the Bash source code because these commands are compiled into the bash executable.

One fast and easy way to see whether or not a command is a bash builtin is to use the help command. Example, help cd will show you how the bash builtin of ‘cd’ is defined. Similarly for help echo .

The actual set of built-ins varies from shell to shell. There are:

  • Special built-in utilities, which must be built-in, because they have some special properties
  • Regular built-in utilities, which are almost always built-in, because of the performance or other considerations
  • Any standard utility can be also built-in if a shell implementer wishes.

You can find out whether the utility is built in using the type command, which is supported by most shells (although its output is not standardized). An example from dash :

$ type ls ls is /bin/ls $ type cd cd is a shell builtin $ type exit exit is a special shell builtin 

Re cd utility, theoretically there’s nothing preventing a shell implementer to implement it as external command. cd cannot change the shell’s current directory directly, but, for instance, cd could communicate new directory to the shell process via a socket. But nobody does so because there’s no point. Except very old shells (where there was not a notion of built-ins), where cd used some dirty system hack to do its job.

How is cd implemented inside the shell? The basic algorithm is described here. It can also do some work to support shell’s extra features.

Manjari, Check the source code of bash shell from ftp://ftp.gnu.org/gnu/bash/bash-2.05b.tar.gz You will find that the definition of shell built-in commands in not in a separate binary executable but its within the shell binary itself (the name shell built-in clearly suggests this).

Читайте также:  Open udp ports linux

Every Unix shell has at least some builtin commands. These builtin commands are part of the shell, and are implemented as part of the shell’s source code. The shell recognizes that the command that it was asked to execute was one of its builtins, and it performs that action on its own, without calling out to a separate executable. Different shells have different builtins, though there will be a whole lot of overlap in the basic set.

Sometimes, builtins are builtin for performance reasons. In this case, there’s often also a version of that command in $PATH (possibly with a different feature set, different set of recognized command line arguments, etc), but the shell decided to implement the command as a builtin as well so that it could save the work of spawning off a short-lived process to do some work that it could do itself. That’s the case for bash and printf, for example:

$ type printf printf is a shell builtin $ which printf /usr/bin/printf $ printf printf: usage: printf [-v var] format [arguments] $ /usr/bin/printf /usr/bin/printf: missing operand Try `/usr/bin/printf --help' for more information. 

Note that in the above example, printf is both a shell builtin (implemented as part of bash itself), as well as an external command (located at /usr/bin/printf). Note that they behave differently as well — when called with no arguments, the builtin version and the command version print different error messages. Note also the -v var option (store the results of this printf into a shell variable named var ) can only be done as part of the shell — subprocesses like /usr/bin/printf have no access to the variables of the shell that executed them.

And that brings us to the 2nd part of the story: some commands are builtin because they need to be. Some commands, like chmod , are thin wrappers around system calls. When you run /bin/chmod 777 foo , the shell forks, execs /bin/chmod (passing «777» and «foo») as arguments, and the new chmod process runs the C code chmod(«foo», 777); and then returns control to the shell. This wouldn’t work for the cd command, though. Even though cd looks like the same case as chmod , it has to behave differently: if the shell spawned another process to execute the chdir system call, it would change the directory only for that newly spawned process, not the shell. Then, when the process returned, the shell would be left sitting in the same directory as it had been in all along — therefore cd needs to be implemented as a shell builtin.

Читайте также:  Linux unsupported locale setting

Источник

built-in commands

This subchapter looks at built-in commands in a UNIX (and Linux) BASH shell.

free book on UNIX/Linux System Administration

free computer programming text book project

table of contents
If you like the idea of this project,
then please donate some money.
more information on donating

built-in commands

This subchapter looks at built-in commands in a UNIX (and Linux) BASH shell.

tools

Most of the commands in the UNIX or Linux shell are actually programs. If you look at the usr/bin directory, you will see the actual programs. Most of these programs were written in the C programming language.

There is a common core of tools/commands that will be available on almost every UNIX or Linux machine, but exactly how many and which commands/tools are available varies widely.

The good news is that if a command or tool is missing from your system, you can go out and get the source code and recompile it for your local computer.

built-in

Many of the shells have special built-in commands. These are not separate programs, but are part of the code for the shell itself.

One example would be the shell command cd that you just saw in the previous quick tour chapter.

There are some built-in commands are only available in selected shells and these can make your scripts shell-dependent.

Some examples of built-in commands include the history command in the C shell, and the export command in the Bourne shell. The cd command is built-in in both bash and csh.

echo is an example of a command that is built into both bash and csh, but also exists externally as a utility.

External tools can vary, but generally have enough in common to be considered shell-independent, so your scripts will run in any shell.

overriding built-in commands

You can override any built-in commands by giving the full path name to an external command or utility. If bash finds a slash character (

comments, suggestions, corrections, criticisms

free music player coding example

Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

Читайте также:  Изменить атрибуты папки в линукс

View music player in action: www.musicinpublic.com/.

Create your own copy from the original source code/ (presented for learning programming).

Work on this project is very slow because I am homeless. I am available for work if someone can provide an indoor place to work in Costa Mesa, California, electricity, internet connections, a flat raised working surface (such as a table or desk), a sitting device (such as a chair or stool), and a fully functional reasonably modern used computer. I’m already homeless, so you don’t need to pay me (and I understand how much business people hate the minimum wage law). Just give me a chance to work.

view text book
HTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

free book on UNIX/Linux System Administration

free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,
then please donate some money.

send donations to:
Milo
PO Box 1361
Tustin, California 92781

At the time I am homeless. This greatly interferes with my ability to create this project, which can help nearly 20 million U.S. college students and more than 150 million students world-wide. I am looking for 30 rich people or corporations willing to donate $10 a month to my church so that the church can provide a place indoors for me to continue work. If you want to donate, please see help project. Thanks much.

Supporting the entire project:

If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

Some or all of the material on this web page appears in the
free downloadable college text book on computer programming.

This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

†UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

Names and logos of various OSs are trademarks of their respective owners.

Last Updated: September 2, 2012

Источник

Оцените статью
Adblock
detector