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
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
  • Week 1
    • 1.Operating Systems and Linux: What the Kernel Does
    • 2.Free Software and Distributions: The Ecosystem Built by Licenses
    • 3.Installation and Booting: From the Moment You Power On
    • 4.Filesystem Structure and File Types: Everything Is a File
    • 5.Week 1 Comprehensive Review: Time to See Through the Concepts
  • Week 2
  • Week 3
    • 1.What a Shell Really Is: The Command Interpreter and the World of Variables
    • 2.The Art of Connecting and Bending Streams: Redirection, Pipes, Metacharacters
    • 3.Taming the Shell: Aliases, History, and Job Control
    • 4.Shell Scripts: The First Step in Weaving Commands into Code
    • 5.Week 3 Comprehensive Review: The Big Picture of the Shell and Scripts
  • Week 4
  • Week 5
  • Week 6
  • Week 7
  • Week 8
  • Week 9
  • Week 10
  • Week 11
  • Week 12
  • Week 13
  • Week 14
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 1/Week 3/Day 5

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

Linux Master 1· AssociateWeek 3 · Day 5~27 min read

Day 5 - Week 3 Comprehensive Review: The Big Picture of the Shell and Scripts

📌 Key Takeaways

  • This week converges on a single question — "how do we hand work we used to do by hand over to the shell in a repeatable way".
  • Shell families are Bourne (sh·ksh·bash·zsh) and C (csh·tcsh). The standard is bash, and csh introduced alias and history.
  • Variables are divided by whether they are exported, and configuration files differ between login shells (~/.bash_profile) and interactive shells (~/.bashrc).
  • FD 0·1·2 and the order of 2>&1, the difference between glob * and regex *, and the three kinds of quotes are the heart of Day 2.
  • Scripts build up in this order: shebang → positional parameters → [ ] conditions (string =, number -eq, spaces required) → if/case/for/while → functions.

Day 1 Review: The Shell, Variables, and Initialization Files

  • The shell is the command interpreter between the kernel and the user.
  • Types split into the Bourne family (sh → ksh → bash → zsh) and the C family (csh → tcsh), and bash is the standard Linux login shell.
  • It was csh that first introduced alias and history.
  • A shell variable is valid only in the current shell and is not inherited by children, while an environment variable marked with export is inherited by child processes. Inheritance goes in one direction only, parent→child.
CommandScope shown
setShell variables + environment variables + functions (the widest)
envEnvironment variables only
export -pExported variables only

The read order of initialization files differs by shell type.

  • Login shell: /etc/profile → (the first one of ~/.bash_profile → ~/.bash_login → ~/.profile) → ~/.bash_logout at logout
  • Non-login interactive shell: /etc/bashrc → ~/.bashrc
  • To apply a change immediately, use source or . (dot). bash file applies only in a new child shell, so it has no effect.

💡 Concept: A one-line summary of the key point — .bash_profile once at login, .bashrc every time a shell is launched. So the convention is to put aliases and functions in .bashrc and environment variables in .bash_profile.

Day 2 Review: Redirection, Pipes, Metacharacters

Every process has three standard channels — stdin (0), stdout (1), stderr (2). Redirection changes where these FDs are connected.

SymbolMeaning
> / >>Overwrite / append stdout
2>Redirect stderr only
&> or > f 2>&1stdout+stderr together
<Take input
<< / <<<Here document / here string
  • The order of 2>&1 decides the result. It must be > file 2>&1 (redirect 1 to the file first, then merge 2) for both to go to the file.
  • A pipe (|) connects the stdout of the preceding command to the stdin of the following command (stderr is not passed by default).
  • Combine filters (grep, sort, uniq, wc, cut, tee, etc.) to turn a compound job into one line.
  • Wildcards (globbing) are expanded by the shell into file names before the command runs: * (zero or more), ? (exactly one), [ ] (one of the characters in brackets). The meaning differs from * in regular expressions (repetition of the preceding character).

The behavioral differences among quotes are an item the exam asks about.

QuoteVariable expansionCommand substitutionWildcards
' ' single❌❌❌
" " double✅✅❌

⚠️ Trap: With sort file > file, > empties the file before the command runs, so the contents vanish. To write to the same file, use the tool's -o option or go through a temporary file.

Day 3 Review: Aliases, History, Job Control

  • alias gives a nickname to a long command, and to make it permanent you put it in ~/.bashrc. Temporary bypasses are \command, command cmd, and the absolute path.
  • history is reused with !! (the whole previous command), !n (number n), and !$ (the last argument of the previous command). Behavior is tuned with HISTSIZE (number kept in memory) and HISTCONTROL (duplicate and space control).
  • Job control is the most practical part of this week.
ActionBehavior
cmd &Run in the background from the start
Ctrl-ZSuspend the foreground job
Ctrl-CTerminate the job
jobsJob list
bg %n / fg %nResume in the background / return to the foreground
nohup cmd &Ignore SIGHUP (survive terminal closure) + background
disown %nDetach a running job from the shell

When the terminal closes, SIGHUP is delivered to the jobs inside and kills them. nohup makes them ignore this signal, but background execution is strictly &'s job, so you must use both together.

Day 4 Review: Shell Scripting Basics

  • The shebang (#!/bin/bash) on the first line of a script specifies the interpreter. ./script.sh runs in a child shell and source script.sh runs in the current shell.
  • Positional parameters: $0 (name), $1~$9 (arguments), $# (count), $@ (all, separated), $? (exit status, 0=success), $$ (PID).
  • Condition evaluation (test/[ ]): strings use =, !=, -z, -n; numbers use -eq, -ne, -gt, -lt; files use -f, -d, -e. Spaces are required inside the brackets and around operators.
  • Control structures: if-elif-else-fi, case-esac (patterns end with ;;, *) is the default), for-do-done, while-do-done, until. Control loops with break and continue.
  • Functions: return gives only an exit status of 0~255, and data is passed with echo+$(function). Localize variables inside functions with local.

if accepts not only [ ] but any command as a condition and branches on its exit status, as in if grep -q root /etc/passwd; then .... [ ] can be an if condition because it is itself a command called test.

Integrated Scenario: Where What You Learned Meets in One Script

Here we summarize how this week's concepts are used together in a real script.

#!/bin/bash
# Compress today's .log files in the log directory and record the result
 
LOG_DIR="/var/log/myapp"
DATE=$(date +%Y%m%d)              # command substitution (Day3)
REPORT="/tmp/compress_$DATE.txt"
 
if [ ! -d "$LOG_DIR" ]; then      # file test + conditional (Day4)
    echo "Directory not found: $LOG_DIR" >&2   # stderr redirection (Day2)
    exit 1
fi
 
count=0
for file in "$LOG_DIR"/*.log; do  # wildcard + for (Day2,4)
    [ -f "$file" ] || continue    # skip when nothing matches
    gzip "$file" 2>> "$REPORT"    # append errors only (Day2)
    count=$((count + 1))          # arithmetic expansion (Day3)
done
 
echo "Compression complete: $count files" | tee -a "$REPORT"   # pipe + tee (Day2)
  • This short script contains command substitution, conditionals, file tests, redirection, wildcards, loops, arithmetic expansion, pipes, and tee, all together.
  • It shows that what you learned this week is not isolated knowledge but parts that mesh to automate a single task.

A "log compression and cleanup" script like this is actually handled for you by a standard tool called logrotate. But logrotate's inner workings and custom cleanup logic ultimately stand on the principles of shell scripts like this one. Knowing the fundamentals also helps you understand what the standard tools are doing.

Final Checklist Right Before the Exam

  • Shell family classification: ksh is the Bourne family (don't be fooled by the name), and csh introduced alias and history
  • $SHELL is the default login shell; the current shell is echo $0
  • The difference between shell and environment variables is whether it is exported, and inheritance is one-way parent→child
  • Login shell: /etc/profile → ~/.bash_profile; interactive: ~/.bashrc
  • FD: stdin 0, stdout 1, stderr 2 / the order of 2>&1 matters
  • Glob * ≠ regex *
  • Single quotes = block everything, double quotes = expand variables only
  • Ctrl-Z = suspend, Ctrl-C = terminate / use nohup and & together
  • test: string =, number -eq, spaces required
  • $? = exit status (0=success), a function's return is an exit status only

📖 Glossary

  • Bourne family / C family : The group of bash, ksh, and zsh descended from sh, and the tcsh group that branched from csh.
  • Shell variable vs environment variable : A variable not passed to children because it is not exported, versus one that is passed.
  • ~/.bash_profile vs ~/.bashrc : A file read once at login, versus a file read every time a new shell is launched.
  • source (.) : A command that reads a file inside the current shell to apply settings immediately.
  • FD 0/1/2 : The numbers of standard input, standard output, and standard error. Redirection changes what these numbers are connected to.
  • 2>&1 : Notation that merges errors into the output. It must be written after > file for both to go to the file.
  • Globbing : The expansion in which the shell replaces *, ?, and [] with actual file names in advance. The meaning differs from regex.
  • SIGHUP : The signal delivered to jobs inside a terminal when it closes. It is ignored with nohup.
  • Job number (%n) : A job identification number that is meaningful only within the current shell. Used like fg %1.
  • shebang : The first line that specifies which interpreter should run the script.
  • $? : The exit status of the previous command. 0 is success, and conditionals branch on this value.
  • test/[ ] : A command that evaluates a condition and answers with an exit status. Strings use =, numbers use -eq, spaces required.
  • local : The keyword that declares a local variable that lives only inside a function.

📝 Practice Questions

Click a choice to reveal the answer and explanation.

Question 1

Which of the following shells is NOT in the Bourne shell family?

Question 2

What is the most accurate way to check the name of the currently running shell?

Question 3

Which correctly describes the difference between command > out.txt 2>&1 and command 2>&1 > out.txt?

Question 4

Which is a correct statement about wildcards (globbing)?

Question 5

Which of the following prints with the variable expanded? (with NAME=linux)

Question 6

Which command resumes, in the background, a job suspended with Ctrl-Z?

Question 7

You want a job not to end even when the terminal is closed. What is the most appropriate command to run?

Question 8

Which operator in the test command checks whether /etc/passwd is a directory?

Question 9

Which is a correct statement about the $? variable?

Question 10

Which pair correctly matches the command that makes a shell variable inherited by child processes and the command that applies a modified .bashrc to the current shell immediately?

Question 11

Which pair correctly matches the symbol that handles several patterns in a single block in a case statement and the symbol that ends each block?

Question 12

What is the result of running the following script? ```bash #!/bin/bash count=0 for f in a b c; do count=$((count + 1)) done echo $count ```

PreviousShell Scripts: The First Step in Weaving Commands into CodeWeek 3 · Day 4Next The Four Account Files: Anatomy of the Fields in /etc/passwd · /etc/shadow · /etc/group · /etc/gshadowWeek 4 · Day 1

Official references

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

On this page

  • Key Takeaways
  • Day 1 Review: The Shell, Variables, and Initialization Files
  • Day 2 Review: Redirection, Pipes, Metacharacters
  • Day 3 Review: Aliases, History, Job Control
  • Day 4 Review: Shell Scripting Basics
  • Integrated Scenario: Where What You Learned Meets in One Script
  • Final Checklist Right Before the Exam
  • Glossary
  • Practice Questions