Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Examendoelen in dit hoofdstuk
- Een shell openen en commando's met de juiste syntax typen
- Invoer- en uitvoeromleiding gebruiken (>, >>, |, 2>)
- De systeemdocumentatie vinden en gebruiken: man, info en /usr/share/doc
Know the three kinds of commands
A command can be an alias, a built-in of the shell, or a program on the disk. type tells you which one it is.
- An alias is a short name for a longer command, like
llforls -l --color=auto. - A built-in is part of Bash itself, like
cd,echoorhistory. - A program (external command) is a file on the disk, found through the folders in
$PATH.
type ll
ll is aliased to `ls -l --color=auto'
type cd
cd is a shell builtin
type passwd
passwd is /usr/bin/passwd
which passwd
/usr/bin/passwd
echo $PATH
/home/student/.local/bin:/home/student/bin:/usr/local/bin:/usr/binWhen you type a name, Bash looks in this order: alias → built-in → the folders of $PATH (left to right). The current folder is not in the path, so start your own script with ./:
./backup.shOnthoud
type NAME answers "what will run?". which NAME only searches $PATH.
Let op
An alias with the same name as a program wins. To run the real program once, start it with a backslash: \ls.
Type commands with options and arguments
A command line is: the command, then options (they change how it works), then arguments (what it works on).
- Short options have one dash and one letter (
-l). You can join them:-la. - Long options have two dashes and a word (
--all). They are easier to read in scripts. - Arguments are names of files, folders, users …
Join commands on one line:
| Sign | Meaning | Example |
|---|---|---|
; | Run one after the other | cd /tmp; ls |
&& | Run the next one only if the first one worked | dnf install -y httpd && systemctl start httpd |
|| | Run the next one only if the first one failed | ping -c1 server2 || echo "server2 is down" |
Onthoud
&& = "and then if OK", || = "or else".
Send output to a file (redirection)
Every command has 3 channels: 0 = input, 1 = output, 2 = errors. Redirection sends a channel to a file instead of the screen.
| Sign | What it does |
|---|---|
> | Output to a file. The file is emptied first. |
>> | Output added to the end of a file. |
2> | Errors to a file. |
2>&1 | Errors go to the same place as the output. |
&> | Output and errors to one file. |
< | Read the input from a file. |
/dev/null | A "black hole": everything sent there is gone. |
ls /etc > etc-list.txt
date >> etc-list.txt
find / -name "*.conf" 2> /dev/null
find / -name "*.conf" &> find-all.txt
mail -s "report" root < report.txtOnthoud
> = new, >> = add, 2> = errors.
Let op
> on an important file empties it at once. Use >> when you are not sure.
Connect commands with pipes
A pipe | sends the output of one command as input to the next one. Small tools together do big jobs.
ps aux | grep sshd
ls -l /etc | less
cat /var/log/messages | grep -i error | wc -l
dnf list installed | tee installed.txt | wc -ltee writes the data to a file and passes it on, so you can save a step in the middle of a pipe.
Op het werk
Build a pipe step by step. Run the first command, look at the output, then add the next |.
Use the command history
Bash remembers your commands in ~/.bash_history. Repeat or search them instead of typing again.
| Keys / command | What it does |
|---|---|
| ↑ and ↓ | Go through the last commands |
Ctrl+R then text | Search back in the history; press Ctrl+R again for the next match |
!42 | Run command number 42 again |
!! | Run the last command again (sudo !! = last command with sudo) |
!dnf | Run the last command that started with dnf |
history -c | Clear the history of this shell |
history -w | Write the current history to the file now |
The variable HISTSIZE sets how many commands Bash keeps in memory, HISTFILESIZE how many are kept in the file.
Let op
A password typed on the command line ends up in the history file. Remove it with history -d NUMBER, then history -w.
Save typing with Tab completion
Press Tab to complete a command, a file name or an option. Press Tab twice to see all possible choices.
Completion for options and sub-commands comes from the package bash-completion. It is installed on most systems.
Examentip
Tab completion is the fastest way to type long names without typing mistakes. Use it all the time in the exam.
Edit a file with vim
vim has two main modes. In command mode keys are commands. In insert mode keys are text. Esc always takes you back to command mode.
vim /etc/hosts| Key | What it does |
|---|---|
i / a / o | Insert before the cursor / after the cursor / on a new line |
Esc | Back to command mode |
:wq or ZZ | Save and quit |
:q! | Quit without saving |
dd / 5dd | Delete (cut) one line / 5 lines |
yy / p | Copy a line / paste it under the cursor |
u / Ctrl+R | Undo / redo |
/text then n | Search for text, then go to the next match |
gg / G | Go to the first line / to the last line |
:%s/old/new/g | Replace every "old" with "new" in the whole file |
:set number | Show line numbers |
Onthoud
Lost? Press Esc twice, then type :q! to leave without changes.
Op het werk
Run vimtutor once. It is a 30-minute lesson inside the terminal.
Edit a file with nano
nano shows its keys at the bottom of the screen. ^ means the Ctrl key.
nano notes.txt| Keys | What it does |
|---|---|
Ctrl+O then Enter | Save |
Ctrl+X | Quit (it asks to save) |
Ctrl+W | Search |
Ctrl+K / Ctrl+U | Cut a line / paste it |
Alt+U | Undo |
Examentip
nano is fine in the exam. Use the editor you know best; speed matters.
Use variables
A variable keeps a value under a name. Environment variables are passed on to programs you start from the shell.
NAME=valuemakes a shell variable (no spaces around=).export NAMEmakes it an environment variable: programs started from this shell see it.env(orprintenv) shows the environment variables;setshows all variables and functions.
Important variables: PATH (where to find programs), HOME (your home folder), USER, LANG (language), PS1 (the prompt), EDITOR (the editor that other tools open).
Onthoud
No spaces around =. export = give it to child programs.
Know the files that set up the shell
When you log in, Bash reads a few files that set variables and aliases. Some are for all users, some only for you.
| File | For whom | When |
|---|---|---|
/etc/profile and /etc/profile.d/*.sh | All users | At login |
/etc/bashrc | All users | Every new shell |
~/.bash_profile | One user | At login |
~/.bashrc | One user | Every new shell |
- Put your own settings in
~/.bashrc, for example an alias.echo "alias ll='ls -l --color=auto'" >> ~/.bashrc - Load the file in the current shell without logging out.
source ~/.bashrc
For all users, do not change /etc/profile: add a small file in /etc/profile.d/ instead. Updates of the system do not overwrite it.
Onthoud
login = profile files, every shell = bashrc files. source reads a file now.
Show a message at login
/etc/motd is shown after a user logs in. /etc/issue is shown before login on a text console.
Find help with --help and man
Help is on the system: --help for a quick list of options, man for the full manual, man -k to find the right command.
- Quick help:
useradd --help - The full manual. Search with
/word,nfor the next match,qto quit. - Do not know the command? Search the short descriptions of all manuals:
- If
man -ksays "nothing appropriate", build the search database: - More documentation, often with examples:
| Section | Contains |
|---|---|
| 1 | Commands for all users |
| 5 | Configuration files (like passwd, fstab) |
| 8 | Commands for root (system administration) |
Onthoud
1 = user commands, 5 = files, 8 = admin commands.
Ask the AI assistant (RHEL Lightspeed)
RHEL 10 has a command-line assistant: the c command sends a question to Red Hat's AI service and shows an answer.
Remember the main points
The most important facts of this chapter, in short.
- A command has three parts: the command, its options (
-l,--all) and its arguments (files, users …). - The shell finds commands as aliases, built-ins and programs in $PATH;
type NAMEshows which one runs. - Redirect output with
>(overwrite),>>(add), errors with2>, both with&>; read input with<. - A pipe
|sends the output of one command into the next command. history,!number,!!and Ctrl+R repeat old commands; Tab completes names.- vim has modes: Esc for command mode,
ito type,:wqto save and quit,:q!to quit without saving. - Variables hold values;
exportmakes them visible to child programs. Login settings live in/etc/profile,~/.bash_profileand~/.bashrc. - Help comes from
man,man -k(apropos),--help,infoand/usr/share/doc.
Learn the key terms
The words of this chapter in simple English.
| Term | Meaning |
|---|---|
| Shell | The program that reads your commands and starts them (Bash on RHEL). |
| Internal command | A command built into the shell, like cd or echo. |
| External command | A program file on disk, found through $PATH. |
| $PATH | The list of folders where the shell looks for programs. |
| STDIN / STDOUT / STDERR | Input (0), normal output (1) and error output (2) of a program. |
| Redirection | Sending input or output to or from a file. |
| Pipe | Connecting the output of one command to the input of the next. |
| Environment variable | A name with a value that programs can read, like LANG or HOME. |
| Alias | A short name for a longer command. |
| man page | The manual of a command, in sections (1 = user commands, 5 = file formats, 8 = admin commands). |
Check what you learned
Think about the answer first, then open it.
1How do you send only the error messages of a command to a file?
With 2>, for example find / -name x 2> errors.txt.
2What is the difference between > and >>?
> replaces the file; >> adds to the end of the file.
3How do you find which man page talks about passwords if you do not know the command?
man -k password (the same as apropos password). Run mandb first if the list is empty.
4How do you make a variable available to programs started from the shell?
With export NAME=value.
5Which file do you change to give one user an alias at every login?
~/.bashrc of that user.
6In vim, how do you save the file and quit?
Press Esc, then type :wq and Enter.
7How do you search the command history for an old command?
Press Ctrl+R and type part of the command.