Mastering Bash I/O Redirection, Conditionals, and Loops: 13 Essential Questions Explained
This article walks through the fundamentals of Bash scripting, covering file descriptors, input/output redirection, here‑documents, combining stdout and stderr, pipe lines, the tee command, and the three main loop constructs with practical examples and tips for effective shell programming.
Question 11: What’s the difference between > and <?
Understanding I/O redirection starts with file descriptors (FD). The three standard FDs are 0 STDIN (keyboard), 1 STDOUT (monitor), and 2 STDERR (monitor). Commands read from STDIN and write to STDOUT/STDERR unless redirected.
$ mail -s test root
this is a test mail.
please skip.
^dHere the mail program reads its input from STDIN (keyboard). You can redirect input from a file: $ cat /etc/passwd And you can redirect output to a file using >: $ cat < my.file Redirection symbols can be combined: < changes the input source, > changes the output destination. Using << creates a HERE Document, allowing multi‑line input until a delimiter is reached.
$ cat <<FINISH
first line here
second line there
third line nowhere
FINISHRedirecting specific file descriptors:
$ ls my.file no.such.file 1>file.out
$ ls my.file no.such.file 2>file.err
$ ls my.file no.such.file 1>file.out 2>file.errWhen both stdout and stderr are written to the same file, the later write overwrites the earlier. To merge them, use 2>&1 (stderr to stdout) or 1>&2 (stdout to stderr).
$ ls my.file no.such.file 1>file.both 2>&1Redirecting output to /dev/null discards it: $ ls my.file no.such.file 2>/dev/null Appending instead of overwriting uses >>: $ echo "3" >> file.out To prevent accidental overwriting, enable noclobber:
$ set -o noclobber
$ echo "4" > file.out # fails if file exists
$ set +o noclobber
$ echo "5" > file.out # succeedsTemporarily overriding noclobber can be done with >|:
$ set -o noclobber
$ echo "6" >| file.outQuestion 12: If or case?
Conditional execution can be expressed with && and || or with the more readable if…then…else construct. For multiple branches, elif can be used. When testing string values, case provides a cleaner syntax than multiple if tests.
if comd1
then
comd2
comd3
else
comd4
comd5
fi case "$1" in
start) start;;
stop) stop;;
status) rhstatus;;
restart|reload) restart;;
*) echo "Usage: $0 {start|stop|status|restart|reload}"; exit 1;;
esacQuestion 13: for what? while vs until?
Shell provides three primary loops: for, while, and until. for iterates over a list of values; while repeats while a test command returns true; until repeats until the test returns true (i.e., while it returns false).
for var in one two three; do
echo "$var"
done num=1
while [ "$num" -le 10 ]; do
echo "num is $num"
num=$((num+1))
done num=1
until [ "$num" -gt 10 ]; do
echo "num is $num"
num=$((num+1))
doneBoth break and continue control loop flow: break exits the current (or an outer) loop, while continue skips to the next iteration. They differ from return (exits a function) and exit (terminates the script).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
