2

Say we used the command "wget www.myurl.com", right after clicking enter, does the terminal check somewhere for an installed program named wget and then check for the arguments, and check somewhere about how the program is supposed to run?

At which areas does it check and how does it validate arguments?

Harsha
  • 508

2 Answers2

3

The terminal doesn't actually do any of that, instead it's the program running in the terminal that handles commands, by default Bash (a shell).

When you run a command, Bash does a bunch of things, but a lot of them are irrelevant to this example so I'll skip them (e.g. redirections, most expansions, keywords, and builtins).

When you run wget www.myurl.com, Bash:

  1. Splits it into words and treats the first word as a command name, i.e. wget
  2. Checks if the command name is an alias, is a function, and/or has a hashed path
    • Let's assume not
  3. Checks each directory in the PATH environment variable for an executable with that name
    • In my case wget is at /usr/bin/wget. Run type wget to see for yourself.
  4. Runs the exectuable, /usr/bin/wget, with the arguments wget (the command name) and www.myurl.com (the command line argument you supplied)
    • Bash does not validate the arguments itself
  5. Waits for the command to complete and returns its exit status

For more details see Shell Operation in the Bash manual, and the links there.

wjandrea
  • 14,236
  • 4
  • 48
  • 98
2

Assuming you are using bash as a shell, it will search all the directories set via the PATH environment variable.

Note that aliases and functions take precedence to scripts/binaries. You can use echo "$PATH" to list the directories (colon used as separator).

wjandrea
  • 14,236
  • 4
  • 48
  • 98
zmb
  • 21