kristofer revised this gist . Go to revision
1 file changed, 64 insertions
PATHinBash.md(file created)
@@ -0,0 +1,64 @@ | |||
1 | + | The **`PATH`** shell variable in the Bash shell is a special environment variable that tells your | |
2 | + | system **where to look for executable programs** when you type a command. | |
3 | + | ||
4 | + | ### Why is it needed? | |
5 | + | When you type a command like `ls` or `python`, the system doesn’t magically know where that | |
6 | + | program or script is stored. Instead, it searches through a list of directories (folders) defined | |
7 | + | in the **`PATH`** variable to find the executable file. | |
8 | + | ||
9 | + | --- | |
10 | + | ||
11 | + | ### How does `PATH` work? | |
12 | + | 1. **The `PATH` is a list of directories**: | |
13 | + | The `PATH` variable contains multiple directory paths separated by colons `:`. For example: | |
14 | + | ``` | |
15 | + | /usr/local/bin:/usr/bin:/bin:/home/user/bin | |
16 | + | ``` | |
17 | + | 2. **Command Search**: | |
18 | + | When you type a command, Bash searches through each directory in the `PATH` one by one (in order) to see if the command's executable file exists there. | |
19 | + | ||
20 | + | 3. **If Found**: It runs the program. | |
21 | + | **If Not Found**: Bash returns a "command not found" error. | |
22 | + | ||
23 | + | --- | |
24 | + | ||
25 | + | ### Example for Beginners | |
26 | + | - Suppose your `PATH` looks like this: | |
27 | + | ``` | |
28 | + | /usr/local/bin:/usr/bin:/bin | |
29 | + | ``` | |
30 | + | - You type `python`. | |
31 | + | - Bash will look for an executable named `python` in: | |
32 | + | - `/usr/local/bin` | |
33 | + | - `/usr/bin` | |
34 | + | - `/bin` | |
35 | + | - If it finds `python` in `/usr/bin`, it stops searching and runs it. | |
36 | + | ||
37 | + | --- | |
38 | + | ||
39 | + | ### Why is `PATH` Useful? | |
40 | + | - It allows you to **run commands without typing the full path** to the executable. | |
41 | + | For example, instead of: | |
42 | + | ``` | |
43 | + | /usr/bin/python3 | |
44 | + | ``` | |
45 | + | You can simply type: | |
46 | + | ``` | |
47 | + | python3 | |
48 | + | ``` | |
49 | + | - It lets you add custom directories to prioritize or include your own scripts or programs. | |
50 | + | ||
51 | + | --- | |
52 | + | ||
53 | + | ### Adding a Directory to `PATH` | |
54 | + | To add a directory (e.g., `/home/user/scripts`) to your `PATH`: | |
55 | + | ```bash | |
56 | + | export PATH=$PATH:/home/user/scripts | |
57 | + | ``` | |
58 | + | Now, any executable in `/home/user/scripts` can be run directly by typing its name. | |
59 | + | ||
60 | + | --- | |
61 | + | ||
62 | + | In short, the `PATH` variable makes it convenient and efficient to execute programs without | |
63 | + | needing to type out their full file locations every time. It's like a **"search roadmap"** | |
64 | + | for your system to find commands. |
Newer
Older