Based on the Linux Master 1 exam syllabusReport an error
exported, and configuration files differ between login shells (~/.bash_profile) and interactive shells (~/.bashrc).2>&1, the difference between glob * and regex *, and the three kinds of quotes are the heart of Day 2.[ ] conditions (string =, number -eq, spaces required) → if/case/for/while → functions.sh → ksh → bash → zsh) and the C family (csh → tcsh), and bash is the standard Linux login shell.export is inherited by child processes. Inheritance goes in one direction only, parent→child.| Command | Scope shown |
|---|---|
set | Shell variables + environment variables + functions (the widest) |
env | Environment variables only |
export -p | Exported variables only |
The read order of initialization files differs by shell type.
/etc/profile → (the first one of ~/.bash_profile → ~/.bash_login → ~/.profile) → ~/.bash_logout at logout/etc/bashrc → ~/.bashrcsource 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_profileonce at login,.bashrcevery time a shell is launched. So the convention is to put aliases and functions in.bashrcand environment variables in.bash_profile.
Every process has three standard channels — stdin (0), stdout (1), stderr (2). Redirection changes where these FDs are connected.
| Symbol | Meaning |
|---|---|
> / >> | Overwrite / append stdout |
2> | Redirect stderr only |
&> or > f 2>&1 | stdout+stderr together |
< | Take input |
<< / <<< | Here document / here string |
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.|) connects the stdout of the preceding command to the stdin of the following command (stderr is not passed by default).* (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.
| Quote | Variable expansion | Command substitution | Wildcards |
|---|---|---|---|
' ' 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-ooption or go through a temporary file.
~/.bashrc. Temporary bypasses are \command, command cmd, and the absolute path.!! (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).| Action | Behavior |
|---|---|
cmd & | Run in the background from the start |
Ctrl-Z | Suspend the foreground job |
Ctrl-C | Terminate the job |
jobs | Job list |
bg %n / fg %n | Resume in the background / return to the foreground |
nohup cmd & | Ignore SIGHUP (survive terminal closure) + background |
disown %n | Detach 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.
#!/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.$0 (name), $1~$9 (arguments), $# (count), $@ (all, separated), $? (exit status, 0=success), $$ (PID).=, !=, -z, -n; numbers use -eq, -ne, -gt, -lt; files use -f, -d, -e. Spaces are required inside the brackets and around operators.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.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.
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)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.
$SHELL is the default login shell; the current shell is echo $0/etc/profile → ~/.bash_profile; interactive: ~/.bashrc2>&1 matters* ≠ regex *Ctrl-Z = suspend, Ctrl-C = terminate / use nohup and & together=, number -eq, spaces required$? = exit status (0=success), a function's return is an exit status onlysh, and the tcsh group that branched from csh.~/.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.2>&1 : Notation that merges errors into the output. It must be written after > file for both to go to the file.*, ?, and [] with actual file names in advance. The meaning differs from regex.nohup.%n) : A job identification number that is meaningful only within the current shell. Used like fg %1.$? : 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.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 ```