CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
  • Week 1
    • 1.Operating Systems and the Kernel: What Linux Does
    • 2.The Birth of Linux: From Unix to Linus
    • 3.Free Software and Open Source: What a License Decides
    • 4.Distributions: The Unit That Bundles the Kernel and Tools for Sale
    • 5.Week 1 Review: The Identity Behind the Name Linux
  • Week 2
  • Week 3
  • Week 4
  • Week 5
    • 1.What Is a Shell?
    • 2.Shell Variables and Environment Variables
    • 3.Environment Configuration Files
    • 4.Redirection · Aliases · History
    • 5.Week 5 Comprehensive Review: The Shell on One Page
  • Week 6
  • Week 7
  • Week 8
  • Week 9
  • Week 10
  • Week 11
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← Linux Master 2/Week 5/Day 5

Based on the Linux Master 2 exam syllabus·Report an error

Linux Master 2· AssociateWeek 5 · Day 5~7 min read

Day 5 - Week 5 Comprehensive Review: The Shell on One Page

📌 Key Points

  • There are only two families of shells: the Bourne family (sh, ksh, bash, zsh, dash) and the C family (csh, tcsh). Only the two with a c in their name are C family.
  • A shell variable reaches only the current shell; an environment variable reaches children. The promotion command is export.
  • A login shell reads .bash_profile, and a non-login shell reads .bashrc.
  • > overwrites and >> appends, and errors use channel 2.
  • To apply settings immediately use source, and to remove an alias use unalias.

The Shell on One Page

   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  history

From Login to Logout — The Week as One Flow

What 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
StepDay learnedQuestions that come from that step
①Day 1Where is the login shell written, and how do you change it?
②, ⑨Day 3Which files are read, and in what order?
③, ④, ⑧Day 2Shell variables and environment variables, PATH, PS1, $?
⑤, ⑥Day 4Redirection, pipes, wildcards, aliases
⑦Day 1, 2Builtin and external commands, PATH search order
  • In ⑥, the shell is what resolves aliases, wildcards, and variables. ls receives only the fully resolved result.
  • In ②, .bashrc and /etc/bashrc are not read by bash automatically; one line inside the preceding file calls them (on the Red Hat family).

Finding the Cause from the Symptom

SymptomCauseFix
You added a variable to /etc/profile but a user who is already connected cannot see itConfiguration files are read at loginLog in again or source /etc/profile
You created MYDIR=/data but it is empty inside bash backup.shIt is a shell variable, so it did not go to the childexport MYDIR
After ls > list.txt, yesterday's contents are gone> empties first and then writesUse >>
find / -name core > r.txt but Permission denied floods the screenErrors are on channel 2, so > does not catch themAdd 2> /dev/null
You ran chsh -s /bin/zsh but echo $SHELL is unchangedIt applies from the next loginLog in again
You became root with su but the location and environment are unchangedNo hyphen, so it is a non-login shellsu -
You wrote an alias in ~/.bashrc but get command not foundThe current shell read the file before it was editedsource ~/.bashrc
run.sh is right in front of you but typing only its name gives command not foundThe current directory is not in PATH./run.sh
After editing PATH, even ls failsYou overwrote it without $PATHRevive with export PATH=/usr/bin:/bin
echo '$HOME' prints $HOME as isSingle quotes do not expand variablesChange to double quotes

Reading Redirection Practice

ls /etc /nodir is a command that produces both results and errors. Follow what goes where for each notation.

CommandWhat you see on screenWhat is put in the file
ls /etc /nodir > aErrorResults
ls /etc /nodir 2> aResultsError
ls /etc /nodir > a 2> bNothingResults in a, error in b
ls /etc /nodir &> aNothingResults and error
ls /etc /nodir > a 2>&1NothingResults and error
ls /etc /nodir 2>&1 > aErrorResults
ls /etc /nodir 2> /dev/nullResultsNothing (the error is discarded)
ls /etc /nodir | wc -lError and the line count— (a pipe passes only 1)

Symbols at a Glance

SymbolMeaningSymbolMeaning
" "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

Contrasting Easily Confused Pairs

CategoryABDistinguishing criterion
Bourne vs C familysh, ksh, bash, zsh, dashcsh, tcshDoes the name have a c?
Shell variable vs environment variableUp to the current shellDown to childrenWas it exported?
set vs envEverythingEnvironment variables onlyScope
.bash_profile vs .bashrcLogin shellNon-login shellWhether the login process happened
.bash_profile vs .profilebash-specificUsed instead when absentPriority
su vs su -Non-loginLogin shellPresence of hyphen
> vs >>OverwritesAppendsPreserving existing contents
> vs 2>Standard outputStandard errorChannel number
> vs |To a fileTo the next commandWhat it passes to
* vs ?No limit on number of charactersOne characterCount
source vs bashApplies to the current shellEnds in a new shellWhere it runs
alias vs unaliasCreateRemove—
$SHELL vs $0Login shellThe shell currently runningPoint in time

Commonly Missed Points

  1. zsh is C family → No. It is Bourne family. The C family is only csh and tcsh.
  2. NAME = linux → You must not put spaces around the equals sign. NAME=linux.
  3. You add $ when creating a variable too → No. Only when retrieving it.
  4. env shows everything → No. set shows everything; env shows only environment variables.
  5. The PATH separator is a semicolon → It is the colon (:). The semicolon is Windows.
  6. export PATH=/opt/bin → The existing paths are lost. Always include $PATH:.
  7. $? being 1 means success → 0 is success.
  8. A new terminal window reads /etc/profile → It does not. A non-login shell reads only .bashrc.
  9. Edited settings apply immediately → No. You must re-read them with source or log in again.
  10. > saves errors too → No. Errors are redirected separately with 2>.
  11. You remove an alias with unset → It is unalias. unset is for variables.
  12. $SHELL is the shell currently running → It is the login shell. For the current one, look at echo $0 or ps -p $$.
  13. The first shell to add history and aliases was bash → It was csh. bash inherited them.
  14. > a 2>&1 and 2>&1 > a are the same → They differ. With the latter, the error stays on the screen.
  15. .bash_logout runs every time you close a terminal window → Only when leaving a login shell.
  16. . file and ./file are the same → The former reads in the current shell (source), the latter executes.
  17. /sbin/nologin means the account was deleted → The account stays, and only login is blocked.

Values That Appear on the Exam as They Are

ValueWhat it is
0 / 1 / 2Standard input / standard output / standard error
:The separator in PATH
7thThe field in /etc/passwd that holds the login shell
/etc/shellsThe file listing available shells
/dev/nullWhere things are thrown away
0The value meaning success in $?
ShellCreator
shStephen Bourne
cshBill Joy
kshDavid Korn
bashBrian Fox

Files at a Glance

FileOne-line role
/etc/passwdAccount information. The 7th field is the login shell
/etc/shellsThe list of shells usable as a login shell
/etc/profileGlobal, login settings
/etc/profile.d/Directory holding global login settings split into files
/etc/bashrcGlobal, when a shell is launched settings (/etc/bash.bashrc on the Debian family)
~/.bash_profilePersonal, login settings. Holds environment variables
~/.bashrcPersonal, when a shell is launched settings. Holds aliases
~/.bash_logoutExecuted at logout
~/.bash_historyStores command history
/etc/skel/Default configuration files copied to a new account's home
/dev/nullWhere things are thrown away

Commands at a Glance

CommandOne-line role
chsh -sChange the login shell
exportShell variable → environment variable
set / envView all variables / only environment variables
unsetDelete a variable
source (.)Read a file in the current shell
alias / unaliasCreate / remove an alias
historyView command history
type / whichWhat 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 ps and kill.

📖 Glossary

  • Bourne family / C family : The two branches that divide shells. The C family is only csh and tcsh.
  • export : The command that promotes a shell variable to an environment variable.
  • Login shell / non-login shell : The configuration files read differ depending on whether the login process was gone through.
  • source : The command that re-reads a configuration file in the current shell. Same as the dot (.).
  • File descriptor : The number attached to an input/output channel. 0, 1, 2 are standard input, output, and error.
  • /dev/null : A special file that discards any data sent to it.

📝 Practice Questions

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?

PreviousRedirection · Aliases · HistoryWeek 5 · Day 4Next The Three Modes of the vi EditorWeek 6 · Day 1

Official references

  • Linux Master 2 official exam syllabus(opens in a new tab)

On this page

  • Key Points
  • The Shell on One Page
  • From Login to Logout — The Week as One Flow
  • Finding the Cause from the Symptom
  • Reading Redirection Practice
  • Symbols at a Glance
  • Contrasting Easily Confused Pairs
  • Commonly Missed Points
  • Values That Appear on the Exam as They Are
  • Files at a Glance
  • Commands at a Glance
  • Glossary
  • Practice Questions