How does bash execute commands

codedcosmos

This is more of a general question I have been curious about but put simply: how does bash execute commands given to it via a script or terminal?

It would be possible, I guess, to have a bunch of if statements checking all commands like so (Pseudocode):

if (command == "pwd") pwd();
else if (command == "echo") echo();
...

But this would create problems as you would have to recompile the code every time you add a new command, like one started for a program like firefox or gedit.

Then I remembered the which command, which (no pun intended) points to the directory of a given command, making me assume that bash simply looks for a file and grabs it with an iostream to execute it.

Is this the case, and if so, how does it know what method to call, or are they simply generic executables?

Time4Tea

Basically, some commands are built in to the bash shell program itself (e.g. echo, set), in which case, bash already has the code compiled into it to run those commands internally, in response to a user calling them from the command line. If you look at the manual in man bash or info bash, it has a list of the 'builtins'.

If a command is not found in the builtins, then the shell searches the directories listed in the $PATH environment variable (in the order listed), to see if it can find an external command there. If not, then it will report an error that the command can't be found.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to execute git commands from bash script?

From Dev

How to execute bash commands from C?

From Java

How to execute a group of commands as another user in Bash?

From Dev

How to execute mongo commands from bash?

From Dev

How to execute the following commands in a bash script?

From Dev

How to execute multiple commands in Bash, some in the background

From Dev

How to execute sudo commands with Expect & send commands in bash script?

From Dev

How to execute certain commands if a file does NOT exist?

From Dev

Bash script does not execute commands until after delay

From Dev

How does bash get commands into the operating system?

From Dev

How does pipe work in bash commands?

From Dev

How to open putty using batch and login then execute list of commands on bash

From Dev

how to execute frequently run bash commands with less keystrokes?

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

How to execute commands in docker container as part of bash shell script

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

How to edit-and-execute last two commands with `fc` in bash

From Dev

How does bash execute an ELF file?

From Dev

Bash tries to execute commands in heredoc

From Dev

Execute gcloud commands in a bash script

From Dev

Execute Bash commands Python way

From Dev

Bash script to spawn and execute commands

From Dev

Execute bash commands in a curses window

From Dev

How to execute parallel commands

From Dev

Does maven execute chained commands

From Dev

bash: PROMPT_COMMAND does not execute multiple commands, single command works

From Dev

Cannot execute shell commands in bash script

From Dev

bash script execute commands after ssh

Related Related

  1. 1

    How to execute git commands from bash script?

  2. 2

    How to execute bash commands from C?

  3. 3

    How to execute a group of commands as another user in Bash?

  4. 4

    How to execute mongo commands from bash?

  5. 5

    How to execute the following commands in a bash script?

  6. 6

    How to execute multiple commands in Bash, some in the background

  7. 7

    How to execute sudo commands with Expect & send commands in bash script?

  8. 8

    How to execute certain commands if a file does NOT exist?

  9. 9

    Bash script does not execute commands until after delay

  10. 10

    How does bash get commands into the operating system?

  11. 11

    How does pipe work in bash commands?

  12. 12

    How to open putty using batch and login then execute list of commands on bash

  13. 13

    how to execute frequently run bash commands with less keystrokes?

  14. 14

    Bash script: How to execute commands consecutively without waiting for the previous one?

  15. 15

    How to execute commands in docker container as part of bash shell script

  16. 16

    Bash script: How to execute commands consecutively without waiting for the previous one?

  17. 17

    How to execute shell commands in a loop from within a bash script?

  18. 18

    How to edit-and-execute last two commands with `fc` in bash

  19. 19

    How does bash execute an ELF file?

  20. 20

    Bash tries to execute commands in heredoc

  21. 21

    Execute gcloud commands in a bash script

  22. 22

    Execute Bash commands Python way

  23. 23

    Bash script to spawn and execute commands

  24. 24

    Execute bash commands in a curses window

  25. 25

    How to execute parallel commands

  26. 26

    Does maven execute chained commands

  27. 27

    bash: PROMPT_COMMAND does not execute multiple commands, single command works

  28. 28

    Cannot execute shell commands in bash script

  29. 29

    bash script execute commands after ssh

HotTag

Archive