The shell
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
The shell is the program that reads what you type in the terminal and runs it. On Ubuntu, Debian and Fedora it is called bash. Follow one line, cat *.txt, through the shell's loop: it splits the line into words, expands the * wildcard, searches PATH for the cat program, asks the kernel to start it, and shows a new prompt when cat hands back its exit status. Then see why cd is built into the shell itself.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01What is the shell?
The terminal is the window; the shell (for example bash) is the program inside it that reads each line you type and runs it. The kernel is the core that drives the hardware.
02In the folder are notes.txt, photo.jpg and todo.txt. You type cat *.txt. Which words does cat actually receive?
The shell expands *.txt before cat runs: it swaps the pattern for the matching names, sorted A to Z. photo.jpg does not end in .txt, so it is left out. cat never sees the star.
03PATH is /usr/local/bin:/usr/bin:/bin and cat is in /usr/bin and /bin. Which cat does the shell run?
The shell checks the PATH folders left to right and stops at the first one that holds cat. /usr/local/bin has none, so /usr/bin/cat wins.
04A program finishes with exit status 0. What does that mean?
0 means success; any other number means something went wrong. Right after a command, echo $? prints its exit status.
05Why is cd built into the shell instead of being a separate program?
Every program has its own current folder, and chdir changes only the caller's. A separate cd program would change its own folder and then exit, leaving the shell where it was. So the shell runs cd itself.
المسرد
- Shell
- The program that reads the commands you type and runs them. bash is the default on Ubuntu, Debian and Fedora.
- Prompt
- The text the shell prints when it is ready for a command, for example alex@lab:~$ (user, computer, folder, then $).
- Argument
- A word after the command name that gives the command extra information, such as which files to use.
- Wildcard
- A character that stands for other characters in file names. * matches any run of characters, so *.txt means every name ending in .txt.
- PATH
- A setting that lists the folders the shell searches, in order, to find a program by its name.
- Exit status
- The number a program hands back to the shell when it ends: 0 means success, anything else means a failure.
- Builtin
- A command the shell runs itself, without starting a program from the disk. cd, echo and type are builtins in bash.
- Home folder
- Your own folder on the disk, for example /home/alex. The shell writes it as ~ for short.
الملاحظات والمصادر
- The prompt shown is the default shape on Ubuntu and Debian, for a user named alex on a computer named lab. Fedora's looks like [alex@lab ~]$. The shell builds it from a setting called PS1, and the root user (the administrator) sees # instead of $.
- The PATH on the card is shortened; a real one lists more folders (see yours with echo $PATH). On Ubuntu, Debian and Fedora, /bin is a link to /usr/bin, so cat is in both. bash also remembers where it found a command, so the next cat skips the search.
- To keep a space or a * as plain text, put the word in quotes, for example cat 'my notes.txt'. Quotes stop both the splitting and the wildcard.
- If nothing matches, bash leaves the word as it is, so cat would look for a file really named *.txt and report an error. Names that start with a dot (hidden files) are skipped by *.
- Strictly, the shell forks a copy of itself and the copy calls execve to become cat; on Linux, glibc's fork() uses the clone system call. The shell waits with wait (waitpid).
- The shell runs exactly what you type, and there is no undo. Read a command before you press Enter, above all one with a * in it.
- GNU Bash Reference Manual: Shell Operation
- GNU Bash Reference Manual: Filename Expansion
- GNU Bash Reference Manual: Command Search and Execution
- GNU Bash Reference Manual: Exit Status
- GNU Bash Reference Manual: Controlling the Prompt
- GNU Bash Reference Manual: Bourne Shell Builtins (cd)
- Linux man-pages: bash(1)
- Linux man-pages: cat(1)
- Linux man-pages: glob(7), wildcard matching of file names
- Linux man-pages: environ(7), the PATH variable
- Linux man-pages: fork(2)
- Linux man-pages: execve(2)
- Linux man-pages: wait(2)
- Linux man-pages: chdir(2)
- Ubuntu tutorials: The Linux command line for beginners
- Debian wiki: UsrMerge (/bin is a link to /usr/bin)
- Fedora Project wiki: Features/UsrMove
اختبار يتاح بعد الخطوة الأخيرة.