Based on the Linux Master 2 exam syllabusReport an error
export..bash_profile, and a non-login shell reads .bashrc.> overwrites and >> appends, and errors use channel 2.source, and to remove an alias use unalias. Shell families
Bourne family sh(Bourne) · ksh(Korn) · bash(Fox) · zsh · dash
C family csh(Joy) · tcsh ← only these two!
Variables
NAME=value shell variable → check with set (up to the current shell)
export NAME=value environment variable → check with env (down to children)
Configuration files
Login /etc/profile → ~/.bash_profile → (calls ~/.bashrc)
Non-login ~/.bashrc
Logout ~/.bash_logout
Apply now source file or . file
Input/output
0 stdin <
1 stdout > >> &> both
2 stderr 2> 2>> 2> /dev/null discard
Pipe command | command
Convenience
alias / unalias aliases
history · !! · !number historyWhat we learned separately over four days is really the order of what the shell does in a day.
① Connect Looks at the 7th field of /etc/passwd and launches /bin/bash as the login shell
② Read settings /etc/profile → ~/.bash_profile → ~/.bashrc → /etc/bashrc
③ Prepare vars Variables like PATH, HOME, PS1 are filled in
④ Prompt Shows [user@server1 ~]$ in the PS1 format and waits for input
⑤ Enter command ll *.log > out.txt 2> err.txt
⑥ Interpret ll → ls -l (alias) · *.log → file names (wildcard) · connect > and 2>
⑦ Execute Finds ls in PATH, runs it as a child process, and waits for it to finish
⑧ Result Stores the exit status in $? and shows the prompt again
⑨ Logout Runs ~/.bash_logout and writes history to ~/.bash_history| Step | Day learned | Questions that come from that step |
|---|---|---|
| ① | Day 1 | Where is the login shell written, and how do you change it? |
| ②, ⑨ | Day 3 | Which files are read, and in what order? |
| ③, ④, ⑧ | Day 2 | Shell variables and environment variables, PATH, PS1, $? |
| ⑤, ⑥ | Day 4 | Redirection, pipes, wildcards, aliases |
| ⑦ | Day 1, 2 | Builtin and external commands, PATH search order |
ls receives only the fully resolved result..bashrc and /etc/bashrc are not read by bash automatically; one line inside the preceding file calls them (on the Red Hat family).| Symptom | Cause | Fix |
|---|---|---|
You added a variable to /etc/profile but a user who is already connected cannot see it | Configuration files are read at login | Log in again or source /etc/profile |
You created MYDIR=/data but it is empty inside bash backup.sh | It is a shell variable, so it did not go to the child | export MYDIR |
After ls > list.txt, yesterday's contents are gone | > empties first and then writes | Use >> |
find / -name core > r.txt but Permission denied floods the screen | Errors are on channel 2, so > does not catch them | Add 2> /dev/null |
You ran chsh -s /bin/zsh but echo $SHELL is unchanged | It applies from the next login | Log in again |
You became root with su but the location and environment are unchanged | No hyphen, so it is a non-login shell | su - |
You wrote an alias in ~/.bashrc but get command not found | The current shell read the file before it was edited | source ~/.bashrc |
run.sh is right in front of you but typing only its name gives command not found | The current directory is not in PATH | ./run.sh |
After editing PATH, even ls fails | You overwrote it without $PATH | Revive with export PATH=/usr/bin:/bin |
echo '$HOME' prints $HOME as is | Single quotes do not expand variables | Change to double quotes |
ls /etc /nodir is a command that produces both results and errors. Follow what goes where for each notation.
| Command | What you see on screen | What is put in the file |
|---|---|---|
ls /etc /nodir > a | Error | Results |
ls /etc /nodir 2> a | Results | Error |
ls /etc /nodir > a 2> b | Nothing | Results in a, error in b |
ls /etc /nodir &> a | Nothing | Results and error |
ls /etc /nodir > a 2>&1 | Nothing | Results and error |
ls /etc /nodir 2>&1 > a | Error | Results |
ls /etc /nodir 2> /dev/null | Results | Nothing (the error is discarded) |
ls /etc /nodir | wc -l | Error and the line count | — (a pipe passes only 1) |
| Symbol | Meaning | Symbol | Meaning |
|---|---|---|---|
" " | Expands variables | ' ' | Does not expand variables |
` ` · $( ) | Result of running a command | \ | Removes the special meaning of the next character |
; | Continue unconditionally | && | If the previous one succeeded |
|| | If the previous one failed | & | To the background |
* | Any number of characters | ? | One character |
[abc] | One character from them | [!abc] | One character other than those |
!! | The whole previous command | !$ | The last argument of the previous command |
$? | Exit status of the previous command | $$ | PID of the current shell |
| Category | A | B | Distinguishing criterion |
|---|---|---|---|
| Bourne vs C family | sh, ksh, bash, zsh, dash | csh, tcsh | Does the name have a c? |
| Shell variable vs environment variable | Up to the current shell | Down to children | Was it exported? |
set vs env | Everything | Environment variables only | Scope |
.bash_profile vs .bashrc | Login shell | Non-login shell | Whether the login process happened |
.bash_profile vs .profile | bash-specific | Used instead when absent | Priority |
su vs su - | Non-login | Login shell | Presence of hyphen |
> vs >> | Overwrites | Appends | Preserving existing contents |
> vs 2> | Standard output | Standard error | Channel number |
> vs | | To a file | To the next command | What it passes to |
* vs ? | No limit on number of characters | One character | Count |
source vs bash | Applies to the current shell | Ends in a new shell | Where it runs |
alias vs unalias | Create | Remove | — |
$SHELL vs $0 | Login shell | The shell currently running | Point in time |
NAME = linux → You must not put spaces around the equals sign. NAME=linux.$ when creating a variable too → No. Only when retrieving it.env shows everything → No. set shows everything; env shows only environment variables.:). The semicolon is Windows.export PATH=/opt/bin → The existing paths are lost. Always include $PATH:.$? being 1 means success → 0 is success./etc/profile → It does not. A non-login shell reads only .bashrc.source or log in again.> saves errors too → No. Errors are redirected separately with 2>.unset → It is unalias. unset is for variables.$SHELL is the shell currently running → It is the login shell. For the current one, look at echo $0 or ps -p $$.> a 2>&1 and 2>&1 > a are the same → They differ. With the latter, the error stays on the screen..bash_logout runs every time you close a terminal window → Only when leaving a login shell.. file and ./file are the same → The former reads in the current shell (source), the latter executes./sbin/nologin means the account was deleted → The account stays, and only login is blocked.| Value | What it is |
|---|---|
| 0 / 1 / 2 | Standard input / standard output / standard error |
: | The separator in PATH |
| 7th | The field in /etc/passwd that holds the login shell |
/etc/shells | The file listing available shells |
/dev/null | Where things are thrown away |
| 0 | The value meaning success in $? |
| Shell | Creator |
|---|---|
| sh | Stephen Bourne |
| csh | Bill Joy |
| ksh | David Korn |
| bash | Brian Fox |
| File | One-line role |
|---|---|
/etc/passwd | Account information. The 7th field is the login shell |
/etc/shells | The list of shells usable as a login shell |
/etc/profile | Global, login settings |
/etc/profile.d/ | Directory holding global login settings split into files |
/etc/bashrc | Global, when a shell is launched settings (/etc/bash.bashrc on the Debian family) |
~/.bash_profile | Personal, login settings. Holds environment variables |
~/.bashrc | Personal, when a shell is launched settings. Holds aliases |
~/.bash_logout | Executed at logout |
~/.bash_history | Stores command history |
/etc/skel/ | Default configuration files copied to a new account's home |
/dev/null | Where things are thrown away |
| Command | One-line role |
|---|---|
chsh -s | Change the login shell |
export | Shell variable → environment variable |
set / env | View all variables / only environment variables |
unset | Delete a variable |
source (.) | Read a file in the current shell |
alias / unalias | Create / remove an alias |
history | View command history |
type / which | What a command is / where it was found in PATH |
Next week is editors and processes. We cover vi's three modes and process commands such as
psandkill.
export : The command that promotes a shell variable to an environment variable.source : The command that re-reads a configuration file in the current shell. Same as the dot (.)./dev/null : A special file that discards any data sent to it.Click a choice to reveal the answer and explanation.
Question 1
Which of the following shells does not belong to the Bourne shell family?
Question 2
Which of the following commands prints both shell variables and environment variables?
Question 3
Which of the following notations saves both the standard output and standard error of a command into a single file?
Question 4
Which of the following is the personal configuration file read when you connect remotely over SSH?
Question 5
Which of the following commands adds /opt/bin to the PATH environment variable while keeping the existing value?
Question 6
Which of the following gives the special variable that lets you check whether the previous command succeeded, and the value meaning success?