Lesson 1: The terminal
A shell named with a pun
The shell you type into has a pun for a name. GNU calls bash the Bourne Again SHell: an sh-compatible shell, following the older Bourne shell, with useful features from the Korn shell and the C shell. Its original author was Brian Fox, and it was first released on 8 June 1989. Its developer today is Chet Ramey.
Why it fits here: It names this topic's usual shell: bash, born in 1989 as a free shell that runs the commands of the older sh.
Sources: GNU Bash; Wikipedia: Bash (Unix shell)
Topic: The terminal, the shell and the prompt · history, words
$ for you, # for root
The last character of the prompt says who you are. The bash manual explains the prompt code \$: it shows # if the user's ID is 0, and $ otherwise. On Unix-like systems, the user with ID 0 is the superuser, whatever the account is called, so a prompt ending in # warns that every command can change the whole system.
Why it fits here: It answers this topic's question: a prompt ending in # belongs to the superuser, the account that may change anything.
Sources: GNU Bash Reference Manual: Controlling the Prompt; Wikipedia: Superuser
Topic: The terminal, the shell and the prompt · tools, mistakes
One dash, by the standard
The rule that options begin with a dash is written in a standard. POSIX, the standard that Unix-like systems share, sets out guidelines for how utilities take their options, and its Guideline 4 says that all options should be preceded by the - delimiter character. That shared rule is why ls -l, grep -i and sort -r all look alike.
Why it fits here: It answers this topic's question: the single dash before an option comes from the POSIX standard.
Source: The Open Group Base Specifications: Utility Conventions
Topic: The shape of a command · standards, tools
Two dashes from GNU
Long options with two dashes were GNU's addition. The GNU Coding Standards ask programmers to define long-named options that are equivalent to the single-letter Unix-style options, and give the reason in one line: we hope to make GNU more user friendly this way. So --all can be read as a word, where -a must be remembered.
Why it fits here: It explains this topic's two kinds of option: short ones from Unix, and long readable ones added by GNU.
Source: GNU Coding Standards
Topic: The shape of a command · history, words
The manual's numbered sections
The manual is split into numbered sections. The Linux man-pages project lists them: 1 for user commands, 2 for system calls, 3 for library calls, and more. A name can live in two sections at once: printf is a command that formats and prints data, in section 1, and a family of C functions, in section 3. So man 1 printf asks for the command.
Why it fits here: It answers this topic's question: man searches numbered sections, and printf has a page in two of them.
Sources: Linux man-pages: man-pages(7); Linux man-pages: printf(1); Linux man-pages: printf(3)
Topic: Getting help from the commands themselves · tools, standards
Manuals made short
When a manual page is too long, a shorter one exists. The tldr pages describe themselves as simplified and community-driven man pages: a community effort to simplify the beloved man pages with practical examples. Each page shows a few common uses of a command, so it suits a moment when the full manual is more than you need.
Why it fits here: It adds a friendlier helper to this topic's --help and man: short pages of examples, written by users.
Source: tldr pages
Topic: Getting help from the commands themselves · tools, internet
127 means not found
Some exit statuses have fixed meanings in bash. Its manual says that if a command is not found, the process made to run it returns a status of 127, and if a command is found but is not executable, the status is 126. So echo $? after a typo shows 127, a clue that the shell could not find what you typed.
Why it fits here: It answers this topic's question: a command that does not exist leaves the exit status 127.
Source: GNU Bash Reference Manual: Exit Status
Topic: Reading errors and exit statuses · tools, numbers
130 after Ctrl+C
A command stopped by a signal leaves a status that names the signal. The bash manual says that when a command ends on a fatal signal numbered N, bash uses 128+N as its exit status. Ctrl+C sends SIGINT, the interrupt from the keyboard, which is signal 2, so a command stopped with Ctrl+C leaves the status 130.
Why it fits here: It ties this topic's exit statuses to the keys: a status above 128 means a signal stopped the command.
Sources: GNU Bash Reference Manual: Exit Status; Linux man-pages: signal(7)
Topic: Reading errors and exit statuses · tools, numbers
Keys from Emacs
The shortcuts Ctrl+A and Ctrl+E come from a text editor. The bash manual says that by default its line editing commands are similar to those of Emacs, the editor whose keys they copy, and that a vi-style interface is also available. The command set -o vi switches bash to the keys of vi, and set -o emacs switches it back.
Why it fits here: It answers this topic's question: Ctrl+A and Ctrl+E are Emacs keys, which bash uses unless told otherwise.
Source: GNU Bash Reference Manual: Command Line Editing
Topic: The keys that save typing · tools, history
Ctrl+D ends it all
Ctrl+D does two jobs. In the middle of a line it deletes the character at the cursor. On an empty line, the bash manual says, it is read as the end of input, and bash quits when input ends; its IGNOREEOF variable exists only to make it wait for several in a row. So Ctrl+D on an empty prompt closes the shell, much as exit does.
Why it fits here: It adds one more key to this topic's list: Ctrl+D on an empty line ends input, and so ends the shell.
Source: GNU Bash Reference Manual: Commands For Changing Text
Topic: The keys that save typing · tools, tricks
GNU, announced in 1983
The GNU in a Linux system began with a newsgroup post. On 27 September 1983 Richard Stallman announced: starting this Thanksgiving I am going to write a complete Unix-compatible software system called GNU, for Gnu's Not Unix, and give it away. The shell and basic tools that this lesson types into come from that project.
Why it fits here: It explains the GNU in this topic's layers: the shell and the basic tools come from a project announced in 1983.
Source: GNU Project: Initial Announcement
Topic: Linux, the kernel and distributions · history, people
Just a hobby
Linux began as a hobby, in its author's own words. On 25 August 1991 Linus Torvalds, at the University of Helsinki, posted to the comp.os.minix newsgroup: I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. Wikipedia gives Torvalds's age at the time as 21.
Why it fits here: It answers this topic's question: the Linux kernel began as a student's hobby, announced in 1991.
Sources: Wikipedia: History of Linux; Carnegie Mellon University: Linux history (the original posts)
Topic: Linux, the kernel and distributions · history, people
Lesson 2: The file system
A tree since 1974
The one tree is as old as Unix. In The UNIX Time-Sharing System, published in July 1974, Dennis Ritchie and Ken Thompson wrote that the system keeps several directories for its own use, and that one of these is the root directory. A name that begins with a slash, they explained, starts its search at the root, just as /home does today.
Why it fits here: It shows how old this topic's tree is: a root directory, with paths that begin at a slash, was already there in 1974.
Source: Dennis M. Ritchie and Ken Thompson: The UNIX Time-Sharing System
Topic: One tree from one root · history, standards
One big tree, many disks
A second disk does not start a second tree. The mount manual explains that all files in a Unix system are arranged in one big tree rooted at /, though they can be spread over several devices. The mount command attaches the files of a device to a folder in that tree, and the hier manual names /media as the place for removable media such as USB sticks.
Why it fits here: It answers this topic's question: a second disk is attached at a folder inside the one tree, often under /media.
Sources: Linux man-pages: mount(8); Linux man-pages: hier(7)
Topic: One tree from one root · tools, standards
Two entries in every folder
The . and .. of a path are real entries in every folder. The 1974 paper on Unix says that each directory always has at least two entries: . names the directory itself, so a program can read the current directory without knowing its full path, and .. refers to its parent. The shell does not invent them; the file system holds them.
Why it fits here: It answers this topic's question: . and .. are entries that every folder really carries.
Source: Dennis M. Ritchie and Ken Thompson: The UNIX Time-Sharing System
Topic: Paths: absolute and relative · history, standards
4096 characters at most
A path cannot be endless. Linux's own source code sets PATH_MAX to 4096, described there as the number of characters in a path name, including the final null character. The path_resolution manual adds that a path that is too long gives the error ENAMETOOLONG, Filename too long. Deep folders with long names can reach that limit.
Why it fits here: It gives this topic's paths a limit: on Linux, a whole path may be at most 4096 characters.
Sources: Linux source code: include/uapi/linux/limits.h; Linux man-pages: path_resolution(7)
Topic: Paths: absolute and relative · numbers, tools
Why cd is built in
cd cannot be an ordinary program. The bash manual explains that a command other than a builtin runs in a separate execution environment, with its own copy of the current working directory, so a program that changed its folder would not move the shell. The manual adds that builtins exist for exactly such jobs, impossible or inconvenient for separate programs. So type cd answers: cd is a shell builtin.
Why it fits here: It answers this topic's question: only the shell can change its own folder, so cd must be built into it.
Source: GNU Bash Reference Manual: Command Execution Environment
Topic: Moving around with cd · tools, standards
cd - remembers
cd - works because the shell remembers the last folder. The bash manual says that a directory of - is converted to $OLDPWD before the change is attempted, and OLDPWD holds the folder you were in before the last cd. So cd - twice in a row brings you back to where you started, like a switch between two rooms.
Why it fits here: It explains this topic's cd -: the shell keeps the last folder in OLDPWD, and cd - swaps back to it.
Source: GNU Bash Reference Manual: Bourne Shell Builtins
Topic: Moving around with cd · tools, tricks
A year for old files
ls shows a time for recent files and a year for older ones. The GNU manual explains that timestamps use a date and time, like Mar 30 23:45, for recent files, and a date with the year, like Mar 30 2020, for the rest. A timestamp counts as recent if it is less than six months old and not in the future.
Why it fits here: It answers this topic's question: ls -l shows the year instead of the time for anything older than six months.
Source: GNU Coreutils: Formatting file timestamps
Topic: Reading what ls -l tells you · tools, numbers
Which K?
With -h, ls counts in powers of 1024. The GNU manual says that there K stands for 1024, not 1000, and M for 10,48,576 bytes. Those who prefer powers of 1000 can use the --si option instead. So one file can show slightly different sizes under the two options, and both are right.
Why it fits here: It explains the sizes of this topic's ls -h: a K there is 1024 bytes, not 1000.
Source: GNU Coreutils: What information is listed
Topic: Reading what ls -l tells you · numbers, tools
Why /usr
The name /usr comes from a disk that filled up. Rob Landley tells the story: around 1971 Unix ran on a PDP-11 with two RK05 disk packs of 1.5 megabytes each. When the system grew too big for the first, it leaked into the second, which held the users' home folders, and that is why the second disk was mounted at /usr.
Why it fits here: It answers this topic's question: /usr was once the users' disk, and programs moved there when the first disk was full.
Source: Rob Landley: Understanding the bin, sbin, usr/bin, usr/sbin split (busybox mailing list)
Topic: What the main folders hold · history, machines
/tmp may vanish
Linux's own description of its folders warns about one of them. The hier manual says that /tmp contains temporary files which may be deleted with no notice, such as by a regular job or at system boot up. So /tmp suits files needed for a few minutes, and never anything worth keeping.
Why it fits here: It sharpens this topic's /tmp: files there can disappear at any time, even at the next start.
Source: Linux man-pages: hier(7)
Topic: What the main folders hold · tools, mistakes
Hidden by a shortcut
Hidden files began as an accident. Rob Pike wrote in 2012 that when . and .. appeared in Unix, ls showed them, so Ken Thompson or Dennis Ritchie added a test that skipped every name starting with a dot. The test was a little shorter than it should have been, and, in Pike's words, the idea of a hidden or dot file was created.
Why it fits here: It answers this topic's question: names that start with a dot are hidden because of a shortcut in an early ls.
Sources: Rob Pike: A lesson in shortcuts (archived); Wikipedia: Hidden file and hidden directory
Topic: Names, spaces and hidden files · history, mistakes
65 safe characters
Some characters are safe in a file name everywhere. POSIX defines a portable filename character set: the letters A to Z and a to z, the digits 0 to 9, and the full stop, the underscore and the hyphen, 65 in all. It also advises against a hyphen as the first character, since such names may cause problems when passed as command line arguments.
Why it fits here: It backs this topic's advice on names: small letters, digits and hyphens work everywhere, as long as a name does not begin with a hyphen.
Source: The Open Group Base Specifications: Definitions
Topic: Names, spaces and hidden files · standards, tricks
Lesson 3: Files and folders
Full, with space to spare
A disk can refuse a new file while it still has free space. Each file has an inode, a record of its owner, permissions, times and place on the disk, so an empty file made by touch needs one too. On the ext4 file system, mke2fs makes one inode for every so many bytes when the disk is formatted. That ratio cannot be changed afterwards, and df -i shows how many inodes are left.
Why it fits here: It answers this topic's question: every file, even an empty one, needs an inode, and a disk holds a fixed share of them.
Sources: Linux man-pages: inode(7); GNU Coreutils manual: df invocation; Linux man-pages: mke2fs(8)
Topic: Making files and folders · numbers, tools
A date parser from 1990
touch can set a file's time from plain words. The GNU manual says that its -d option can read month names, time zones, am and pm, and even the word yesterday. The code that reads such dates began as getdate, written by Steven M. Bellovin at the University of North Carolina at Chapel Hill. Rich $alz and Jim Berets overhauled it in August 1990.
Why it fits here: It shows how this topic's touch reads a date: plain words like yesterday work, through a parser that dates from 1990.
Sources: GNU Coreutils manual: touch invocation; GNU Coreutils manual: Relative items in date strings; GNU Coreutils manual: Authors of parse_datetime
Topic: Making files and folders · history, tools
A backwards more
The name less began as a joke. Mark Nudelman, its author, explains that in 1983 a company called Integrated Office Systems had enormous log files, and the pager more could not move backwards through them. The team said it needed a backwards more, and someone named it less. Nudelman wrote the first version in late 1983 and posted it to the newsgroup net.sources in May 1985.
Why it fits here: It answers this topic's question: less was named as a backwards more, a pager that could go back through long logs.
Source: Mark Nudelman: Frequently asked questions about less
Topic: Looking inside files · history, people
Six characters, 18 bytes
wc can give two sizes for one word. The GNU manual says wc -c counts bytes and wc -m counts characters. The Hindi word नमस्ते is made of six Unicode characters, counting the small mark called the halant, and all six come from the Devanagari block, U+0900 to U+097F. UTF-8 stores each character from U+0800 to U+FFFF in three bytes, so wc -c counts 18 and wc -m counts 6.
Why it fits here: It shows that this topic's wc -c counts bytes, not letters: each character of a Hindi word takes three bytes.
Sources: GNU Coreutils manual: wc invocation; The Unicode Standard: Devanagari code chart; RFC 3629: UTF-8, a transformation format of ISO 10646
Topic: Looking inside files · numbers, languages
Why a big move can be instant
A move that stays inside one file system copies nothing. The GNU manual says that mv normally renames the file, and the Linux rename call moves the name from one folder to another, so the size of the file does not matter. Across two file systems the rename fails with the error EXDEV. Then mv falls back on copying, as cp -a would, and removes the original only after the copy has worked.
Why it fits here: It answers this topic's question: mv only renames inside one file system, and copies only when it has to cross to another.
Sources: GNU Coreutils manual: mv invocation; Linux man-pages: rename(2)
Topic: Copying and moving · tools, numbers
A name from magnetic tape
The name tar comes from tape. The GNU tar manual says it came from the phrase Tape ARchive, though archives these days rarely sit on tapes. A tar command appeared in Seventh Edition Unix, released in January 1979. GNU tar was first written by John Gilmore, and many people have changed it since.
Why it fits here: It explains the name of this topic's tar: an archiver made for magnetic tape, in Unix since 1979.
Sources: GNU tar manual: Introduction; FreeBSD manual: tar(1); Wikipedia: tar (computing); GNU tar manual: GNU tar Authors
Topic: Copying and moving · history, words
A second or two, 300 GB
Removing on the wrong machine is a classic mistake. GitLab's own report says that on 31 January 2017 an engineer meant to empty the data folder on the standby database server, a spare copy, but the command ran on the main server instead. The engineer stopped it a second or two later, yet around 300 GB was already gone. About 5000 projects, 5000 comments and 700 new user accounts were affected.
Why it fits here: It shows why this topic says look before you remove: check the names, and check which machine you are on.
Source: GitLab: Postmortem of database outage of January 31
Topic: Removing, with care · history, mistakes
The bin is a hidden folder
The desktop's bin is an ordinary folder. The freedesktop.org Trash Specification gives trash programs one common way to store files, and it puts each user's bin at $XDG_DATA_HOME/Trash, which is ~/.local/share/Trash unless set otherwise. A trashed file is moved into its files folder. A note ending in .trashinfo records the file's original path and the date it was trashed, and that note is how a file goes back.
Why it fits here: It answers this topic's question: the desktop's bin is a folder in your home, which the shell's rm never uses.
Sources: freedesktop.org: Trash Specification; freedesktop.org: XDG Base Directory Specification
Topic: Removing, with care · standards, tools
Glob was once a program
Pattern matching was once a separate program. The Linux manual for glob says that long ago, in Unix V6, a program called /etc/glob expanded wildcard patterns. Soon afterwards the job became part of the shell itself. Today there is also a library routine, glob, that does the same work for any program, and the old name is still the word for patterns like these.
Why it fits here: It shows where this topic's patterns came from: a separate program in Unix V6, later built into the shell.
Source: Linux man-pages: glob(7)
Topic: Patterns: one word for many names · history, words
On a Mac, no match is an error
Not every shell passes on a pattern that matches nothing. Apple says that starting with macOS 10.15, a Mac uses zsh as its default shell. The zsh manual says its NOMATCH option, on by default in zsh, prints an error when a pattern has no matches, instead of leaving the pattern unchanged. So ls *.log in a folder with no logs gets an error from zsh, where bash would hand the star to ls.
Why it fits here: It shows that this topic's rule for a pattern that matches nothing is bash's rule: zsh, the Mac's shell, gives an error instead.
Sources: Apple Support: Use zsh as the default shell on Mac; zsh manual source: options
Topic: Patterns: one word for many names · tools, standards
Order matters for -delete
In a find command, the order of the parts matters. The find manual warns that find reads its command line as an expression, so putting -delete first makes find try to delete everything below the starting points. Two parts in a row are joined by and, and the second is not tried when the first is false. Written last, -delete removes only what the tests before it have matched.
Why it fits here: It answers this topic's question: -delete written first removes everything below the start, and written last only the matches.
Source: Linux man-pages: find(1)
Topic: Finding files · tools, mistakes
Ages counted in whole days
find's test of age rounds down. The find manual says -mtime counts in periods of 24 hours, and any fractional part is ignored. So -mtime +1 does not mean more than one day old: a file must be at least two days old to match. A file changed 47 hours ago counts as one whole day, and one is not more than one.
Why it fits here: It sharpens this topic's -mtime test: a file's age is counted in whole periods of 24 hours, rounded down.
Source: Linux man-pages: find(1)
Topic: Finding files · numbers, tools
Lesson 4: Redirection and pipes
Errors once shared the main outlet
Standard error was not there at the start. The 1974 paper on Unix says that programs began with two open files, and those that wrote diagnostic information ordinarily used file 1, standard output. Doug McIlroy recalls that this became intolerable once output was sent into pipes. People put up with it through the Sixth Edition, and then Dennis Ritchie introduced the standard error file.
Why it fits here: It answers this topic's question: errors first went to standard output, until pipes made a second outlet necessary.
Sources: Dennis M. Ritchie and Ken Thompson: The UNIX Time-Sharing System; M. Douglas McIlroy: A Research UNIX Reader
Topic: Where output goes · history, people
A lock on the arrow
The shell can stop > from emptying a file by mistake. The bash manual says that once the noclobber option is turned on, with set -o noclobber or set -C, a redirection with > fails if the file already exists as a regular file. When you do mean to replace it, the operator >| goes ahead anyway. The option stays off until you turn it on.
Why it fits here: It adds a guard to this topic's >, which empties a file without asking: with noclobber on, bash refuses.
Source: GNU Bash manual
Topic: Where output goes · tools, mistakes
A garden hose in 1964
Pipes were asked for long before Unix. In a Bell Labs memo of 1964, Doug McIlroy wrote that there should be ways of connecting programs like a garden hose, screwing in another segment when data needs another kind of work. Dennis Ritchie says pipes reached Unix in 1972 at McIlroy's suggestion, or perhaps insistence. McIlroy's own history adds that Ken Thompson built them in one feverish night.
Why it fits here: It answers this topic's question: Doug McIlroy wished for programs joined like a garden hose in 1964, and pipes came in 1972.
Sources: Dennis M. Ritchie: Prophetic Petroglyphs; Dennis M. Ritchie: The Evolution of the Unix Time-sharing System; M. Douglas McIlroy: A Research UNIX Reader
Topic: Pipes: from each to the next · history, people
A pipe holds 64 KiB
A pipe is a small store, not an endless tube. The Linux manual says that since Linux 2.6.11 a pipe holds 65,536 bytes on a system with 4096-byte pages, and a command that writes to a full pipe waits until the next one reads. When the reader closes its end, as head does after its lines, the writer gets the signal SIGPIPE and stops. That is why yes | head -n 3 ends at once, though yes prints forever until it is killed.
Why it fits here: It shows how this topic's two commands run at once: the pipe is a small store, and each side waits for the other.
Sources: Linux man-pages: pipe(7); Linux man-pages: signal(7); GNU Coreutils manual: yes invocation
Topic: Pipes: from each to the next · numbers, tools
grep skips most of the bytes
GNU grep is fast because it does not look at every byte. Mike Haertel, its original author, explained in August 2010 that it uses the Boyer-Moore method, which looks first for the last letter of the word sought. A lookup table then says how far ahead it can skip whenever a letter does not match. Its second trick, Haertel wrote, is to run very few instructions for each byte it does look at.
Why it fits here: It answers this topic's question: grep skips ahead through a file instead of reading every byte.
Source: Mike Haertel: why GNU grep is fast (freebsd-current mailing list)
Topic: Finding lines: grep · tools, numbers
Two old names for grep
grep -E and grep -F were once separate commands. The GNU grep manual explains that Seventh Edition Unix had egrep and fgrep, which suited the small computers of the 1970s. POSIX called them obsolescent in 1992 and removed them in 2001. GNU grep deprecated them in 2007, and since version 3.8 in 2022 they print a warning. The manual plans to remove them entirely, so write grep -E.
Why it fits here: It explains the -E of this topic's grep: it replaces the old command egrep, which now prints a warning.
Source: GNU grep manual: What happened to egrep and fgrep?
Topic: Finding lines: grep · history, tools
Six commands and a famous review
The pipe sort, uniq -c, sort -rn has a famous ancestor. In June 1986, Jon Bentley's column in Communications of the ACM asked for a program that prints the k most common words in a text. Don Knuth answered with a program in his WEB system for literate programming. Doug McIlroy of Bell Labs reviewed it with a script of six commands: tr twice, then sort, uniq -c, sort -rn and sed. It was written on the spot and worked on the first try.
Why it fits here: It shows the reach of this topic's pipe sort, uniq -c, sort -rn: six small commands answered a problem set for a whole program.
Source: Jon Bentley, Don Knuth and Doug McIlroy: A Literate Program (Communications of the ACM)
Topic: Sorting and counting · history, people
One file, two orders
The same file can sort in two orders on two computers. The GNU coreutils FAQ explains that with LANG set to en_US.UTF-8, sort folds case and ignores punctuation, because that locale, the computer's language setting, asks for it. To get the standard order, set LC_ALL to C. Then sort uses the traditional tables for US-ASCII, and case is no longer folded.
Why it fits here: It explains why this topic's sort may mix or separate capitals: the computer's locale decides, and LC_ALL=C gives the standard order.
Source: GNU Coreutils FAQ: Sort does not sort in normal order!
Topic: Sorting and counting · tools, languages
Made for files too big to edit
sed was built for files too big for an editor. In a manual dated 15 August 1978, Lee E. McMahon of Bell Laboratories called it a non-interactive editor, and the first use the manual named was for files too large for comfortable editing by hand. Only a few lines of input sit in memory at a time, and no temporary files are used. So the only limit is that the input and the output fit on the disk together.
Why it fits here: It answers this topic's question: sed was first meant for files too large to edit by hand, which is why it works line by line.
Source: Lee E. McMahon: SED, A Non-interactive Text Editor
Topic: Cutting and changing text · history, tools
Any character can be the slash
The slashes of sed's s command are not fixed. The GNU sed manual says they may be replaced by any other single character within one s command. That helps with paths: s|/usr/local|/opt| changes a path with no backslashes, while the slash form needs s/\/usr\/local/\/opt/. Inside the pattern, the chosen character needs a backslash only where it appears itself.
Why it fits here: It adds a trick to this topic's s/old/new/: with another separator, paths full of slashes need no backslashes.
Source: GNU sed manual
Topic: Cutting and changing text · tools, tricks
Argument list too long
A command line has a size limit, and xargs is built to stay under it. The Linux manual for execve says that on most machines the arguments and environment together may take at most a quarter of the stack size limit, and one argument at most 32 pages. Past that, a program cannot start: execve fails with the error E2BIG, whose message is Argument list too long. xargs builds each command line only up to the system's limit, and runs the command as many times as needed.
Why it fits here: It answers this topic's question: a million names can overflow one command line, and xargs splits the list to fit.
Sources: Linux man-pages: execve(2); Linux man-pages: errno(3); Linux man-pages: xargs(1)
Topic: Lines into arguments: xargs · numbers, tools
Several commands at once
xargs can run commands side by side. The xargs manual says its -P option runs up to the given number of processes at a time, where the default is 1, and -P 0 runs as many as possible. It adds a warning: use -n or -L with -P, or chances are that only one command will run. So xargs -n 1 -P 4 gzip keeps four copies of gzip working at once, each on its own file.
Why it fits here: It adds speed to this topic's xargs: with -P it runs several copies of a command at the same time.
Source: Linux man-pages: xargs(1)
Topic: Lines into arguments: xargs · tools, tricks
Lesson 5: Permissions
sudo began around 1980
sudo is older than Linux. The sudo project's history says it was first written by Bob Coggeshall and Cliff Spencer around 1980, in the Department of Computer Science at SUNY Buffalo, on a VAX-11/750 running 4.1BSD. An updated version was posted to the Usenet group net.sources in December 1985. Its manual still sums up its job: it lets a permitted user run a command as the superuser or as another user.
Why it fits here: It shows how old this topic's sudo is: one command at a time as root, an idea from about 1980.
Sources: sudo project: A Brief History of Sudo; sudo manual: sudo(8)
Topic: Users, groups and root · history, people
A file named passwd with no passwords
The file /etc/passwd lists every user, but on many systems today it holds no passwords. Its manual explains that with the shadow password suite, /etc/passwd has an x in the password field, and the encrypted passwords sit in /etc/shadow, which only the superuser can read. The list itself stays readable by all, because tools such as ls use it to turn user numbers into names.
Why it fits here: It answers this topic's question: the user list is open to all, while the passwords moved to a file only root can read.
Source: Linux man-pages: passwd(5)
Topic: Users, groups and root · tools, standards
Once there were two sets
Unix permissions once came in two sets, not three. The 1974 paper on Unix says each new file was given seven protection bits. Six of them gave read, write and execute permission separately for the owner of the file and for all other users. The seventh made a program run with the user identity of the file's creator. The group's set of three came later.
Why it fits here: It answers this topic's question: the Unix of 1974 gave rights to the owner and to everyone else, with no group set.
Source: Dennis M. Ritchie and Ken Thompson: The UNIX Time-Sharing System
Topic: Reading a permission string · history, standards
An eleventh character
A long listing can show an eleventh character after the ten. The GNU ls manual says it tells whether an alternate access method, such as an access control list, applies to the file. A space means none, and a dot means a security context alone. A plus means anything else, such as an access control list, a list of extra rules for named users and groups. The command getfacl shows such a list.
Why it fits here: It completes this topic's ten characters: a plus after them means extra rules, such as an access control list, also apply.
Sources: GNU Coreutils manual: What information is listed; Linux man-pages: acl(5); Linux man-pages: getfacl(1)
Topic: Reading a permission string · tools, standards
The t at the end of /tmp
/tmp is open for everyone to write in, yet people cannot delete each other's files there. The reason is the restricted deletion flag, or sticky bit. The GNU manual says it stops users from removing or renaming a file in a folder unless they own the file or the folder, and that it is common on folders like /tmp. A long listing shows it as a t in the very last place.
Why it fits here: It answers this topic's question: on /tmp the sticky bit adds a rule, so only a file's owner may remove it.
Sources: GNU Coreutils manual: The Structure of File Mode Bits; GNU Coreutils manual: What information is listed
Topic: What read, write and run mean · tools, standards
rm asks, but the folder decides
A file without w can still be removed, because removal needs w on the folder. The GNU rm manual says that when a file is unwritable and you are typing at a terminal, rm asks before removing it, unless -f is given. That question is a courtesy, not a lock: answer no and the file is skipped, answer yes and it goes. The folder's rights decide, not the file's.
Why it fits here: It sharpens this topic's rule: removing needs the folder's w, and rm only asks first when the file itself lacks w.
Source: GNU Coreutils manual: rm invocation
Topic: What read, write and run mean · tools, mistakes
A capital X for folders
chmod has a capital X as well as a small x. The GNU manual says X changes execute or search permission only for folders, and for files that already had execute permission. So chmod -R a+X shared lets everyone enter every folder under shared, while plain text files stay as they were. A small x there would make every file executable.
Why it fits here: It answers this topic's question: chmod -R with a capital X opens the folders and leaves ordinary files alone.
Source: GNU Coreutils manual: Conditional Executability
Topic: Changing permissions: chmod · tools, tricks
Links ignore chmod
chmod never changes a symbolic link, a name that points to another file. The GNU manual says that for each link named on the command line, chmod changes the file it points to instead. The Linux manual for links adds that a link's own permissions are not used at all: they are always 0777 and cannot be changed. That is why every link in a long listing shows lrwxrwxrwx.
Why it fits here: It shows a limit of this topic's chmod: on a link it changes the file pointed to, since a link's own rights are fixed.
Sources: GNU Coreutils manual: chmod invocation; Linux man-pages: symlink(7)
Topic: Changing permissions: chmod · tools, standards
A fourth digit in front
A mode can have a fourth digit, written in front. The GNU manual explains that three special bits share one more octal digit: 4000 is set user ID, 2000 is set group ID and 1000 is the sticky bit. Set user ID makes a program run with the user ID of its file's owner, whoever starts it. So chmod 4755 does that, and ls shows an s where the owner's x would be.
Why it fits here: It answers this topic's question: a digit in front of the three holds the special bits, such as 4 for set user ID.
Sources: GNU Coreutils manual: Numeric Modes; GNU Coreutils manual: The Structure of File Mode Bits; GNU Coreutils manual: What information is listed
Topic: Permissions as numbers · numbers, tools
Octal in program code
Octal also hides in program code. In C, a number that starts with a zero, such as 0644, is octal. Python followed that rule before version 3.0, but its manual now forbids leading zeros in a decimal number: 0123 is not valid, to avoid mixing it up with C's octal. Python writes octal with the prefix 0o instead, so a mode in Python reads os.chmod(path, 0o644).
Why it fits here: It carries this topic's three digits into code: Python writes the mode 644 as 0o644, so it is read as octal.
Sources: cppreference: Integer constant (C); Python Language Reference: Lexical analysis; Python Library Reference: os
Topic: Permissions as numbers · numbers, languages
Why a mask can be 002
A mask of 002 depends on a private group. The login.defs manual says the mask starts at 022 when nothing else is set, and that useradd can give each new user a group with the user's own name. The pam_umask manual adds an option for such users: when the user name matches the main group's name, the group bits copy the owner bits, so 022 becomes 002.
Why it fits here: It answers this topic's question: when your group is yours alone, the mask can give the group the same rights as you, turning 022 into 002.
Sources: Linux man-pages: login.defs(5); Linux man-pages: pam_umask(8)
Topic: Running scripts, and the defaults · tools, numbers
When x is not enough
The x right is not always the last word. A whole file system can be mounted with the option noexec, which the mount manual says does not permit direct execution of any binaries on it. The execve manual lists the error for that case: execution is denied because the file system is mounted noexec. A script with x on such a disk is refused, while bash script.sh still works, as bash only reads the file.
Why it fits here: It adds a limit to this topic's x right: on a disk mounted noexec, ./script.sh is refused even with x.
Sources: Linux man-pages: mount(8); Linux man-pages: execve(2)
Topic: Running scripts, and the defaults · tools, mistakes
Lesson 6: Processes
Every process has a folder
The kernel shows each running process as a folder. The Linux manual says /proc is a pseudo-filesystem, an interface to the kernel's own data. It holds a numbered folder for each running process, named by its PID. So ls /proc/$$ lists files that describe your own shell, though no disk stores them.
Why it fits here: It answers this topic's question: every running process has a folder in /proc, named by its PID.
Sources: Linux man-pages: proc(5); Linux man-pages: proc_pid(5)
Topic: Programs that are running · tools, standards
PIDs wrap around
PIDs do not grow forever. The Linux manual says the file /proc/sys/kernel/pid_max holds the value at which PIDs wrap around, one more than the largest PID. Its default, 32,768, keeps the range of older kernels. On 64-bit systems it can be raised to 222, which is 41,94,304, and it also limits how many processes and threads can exist at once.
Why it fits here: It shows how far this topic's PIDs go: by default they wrap around after 32,768.
Source: Linux man-pages: proc_sys_kernel(5)
Topic: Programs that are running · numbers, tools
& in the 1974 paper
Background jobs are as old as the Unix shell. The 1974 paper on Unix says that when a command is followed by &, the shell does not wait for it to finish before prompting again, and is ready at once for a new command. The & may be used several times in a line, so two commands can run side by side.
Why it fits here: It shows how old this topic's & is: the 1974 description of the Unix shell already ran commands in the background.
Source: Dennis M. Ritchie and Ken Thompson: The UNIX Time-Sharing System
Topic: Running in the background · history, standards
A zombie until someone waits
A finished process does not vanish at once. The wait manual says a child that ends but has not been waited for becomes a zombie. The kernel keeps its PID, its exit status and how much it used, so that the parent can still collect them. Until a wait removes it, the zombie takes a slot in the process table, and ps marks it Z.
Why it fits here: It answers this topic's question: a finished job stays a zombie until its parent collects its status with a wait.
Sources: Linux man-pages: wait(2); Linux man-pages: ps(1)
Topic: Running in the background · tools, words
Job control from Austria
Job control began in the C shell. The tcsh manual credits J. E. Kulp of IIASA, in Laxenburg in Austria, with its job control features. Wikipedia adds that Jim Kulp's was the first job control, built on features of the 4.1BSD kernel. The KornShell, made at Bell Labs, adopted it, and the Bourne shell of SVR4 later did too.
Why it fits here: It shows where this topic's jobs, fg and bg came from: the C shell, with job control written at IIASA in Austria.
Sources: FreeBSD manual: tcsh(1); Wikipedia: Job control (Unix)
Topic: Keeping track of jobs · history, people
A job named by its command
A job can be named by its command as well as its number. The bash manual says %ce means the job whose command begins with ce, and %?ce any job with ce anywhere in its command line. A job named alone comes to the foreground: typing %1 does the same as fg %1. Typing %1 & does the same as bg %1.
Why it fits here: It answers this topic's question: %ce names a job by the start of its command, and %1 alone brings job 1 forward.
Source: GNU Bash manual: Job Control Basics
Topic: Keeping track of jobs · tools, tricks
Ask, wait 90 seconds, then tell
Linux itself asks before it tells. When systemd, the system and service manager, stops a service, its manual says the processes are first sent SIGTERM. If any are still there once the stop timeout has passed, the request is repeated with SIGKILL. That timeout is 90 seconds unless set otherwise, so a stuck program can hold up a shutdown for a minute and a half.
Why it fits here: It answers this topic's question: at shutdown Linux asks with TERM, waits up to 90 seconds, and only then tells with KILL.
Sources: systemd manual: systemd(1); systemd manual: systemd.kill(5); systemd manual: systemd-system.conf(5)
Topic: Signals: asking and telling · tools, numbers
Names travel, numbers do not
Signal names are safer than numbers. The signal manual says many signals have different numbers on different kinds of processor. TSTP, the pause sent by Ctrl+Z, is 20 on x86 and ARM, 18 on Alpha and SPARC, and 24 on MIPS. So kill -TSTP is right on every machine, while kill -20 is right only on some.
Why it fits here: It adds care to this topic's kill: written by name, a signal means the same on every kind of machine.
Source: Linux man-pages: signal(7)
Topic: Signals: asking and telling · numbers, standards
A frozen screen is often Ctrl+S
A terminal that seems frozen may only be paused. The termios manual says Ctrl+S is the Stop character, which stops output until the Start character is typed, and that Ctrl+Q is the Start character. It lists them by their old ASCII names too, DC3 and DC1. So if typing seems to do nothing after an accidental Ctrl+S, press Ctrl+Q, and the output carries on.
Why it fits here: It answers this topic's question: Ctrl+S pauses a terminal's output, and Ctrl+Q lets it carry on.
Source: Linux man-pages: termios(3)
Topic: Pausing and resuming at the keyboard · tools, tricks
Ctrl+Z asks, STOP tells
Pausing has an ask and a tell too. The termios manual says Ctrl+Z sends SIGTSTP, a signal a program may catch and handle in its own way. The signal manual says SIGSTOP, like SIGKILL, cannot be caught, blocked or ignored. So kill -STOP pauses any process you may signal, even one that ignores Ctrl+Z, and kill -CONT lets it continue.
Why it fits here: It mirrors this topic's Ctrl+Z: TSTP asks a program to pause, while STOP pauses it whatever it does.
Sources: Linux man-pages: termios(3); Linux man-pages: signal(7)
Topic: Pausing and resuming at the keyboard · tools, standards
ps aux is not ps -aux
ps accepts three kinds of options: Unix ones with a dash, BSD ones without, and GNU long ones with two dashes. That is why ps aux works with no dash at all. The manual warns that ps -aux mixes two standards. By those standards it asks for processes with a terminal plus those of a user named x, and only if no such user exists does ps guess that ps aux was meant.
Why it fits here: It sharpens this topic's ps aux: without a dash it is a BSD option, and with one it can mean a user named x.
Source: Linux man-pages: ps(1)
Topic: Finding processes: ps, pgrep and top · tools, standards
A hang-up from the modem days
The signal HUP is named after hanging up a line. The signal manual still describes it as a hangup detected on the controlling terminal, or the death of the controlling process. The termios manual keeps a setting that lowers the modem control lines, a hang-up, when the last process closes the terminal. The command nohup runs a command immune to hangups, with that signal ignored.
Why it fits here: It answers this topic's question: HUP is a hang-up, named from terminals on modem lines, and nohup ignores it.
Sources: Linux man-pages: signal(7); Linux man-pages: termios(3); GNU Coreutils manual: nohup invocation
Topic: Finding processes: ps, pgrep and top · history, words
Lesson 7: Environment variables
Named variables from Stephen Bourne
Named variables came with the Bourne shell. In Seventh Edition Unix, released in 1979, it replaced the first shell, the Thompson shell, and took its name, sh. Stephen Bourne's guide to it says the shell provides string-valued variables, whose names begin with a letter and consist of letters, digits and underscores. His first example was user=fred.
Why it fits here: It shows where this topic's name=value comes from: Stephen Bourne's shell of 1979, with names made of letters, digits and underscores.
Sources: S. R. Bourne: An Introduction to the UNIX Shell; FreeBSD manual: sh(1); Wikipedia: Bourne shell
Topic: Shell variables · history, people
A checker for missing quotes
A program can catch a missing pair of quotes. ShellCheck, a checker for shell scripts, reports an unquoted variable as SC2086: double quote to prevent globbing and word splitting. Its wiki explains that echo $1 does not just print the first argument. It splits it at spaces, tabs and line feeds, and expands each piece as a pattern, so quoting is the fix it asks for.
Why it fits here: It backs this topic's rule to quote variables: ShellCheck flags every unquoted one, as warning SC2086.
Source: ShellCheck wiki: SC2086
Topic: Shell variables · tools, mistakes
Capitals for the system, small letters for you
The capitals in PATH and HOME follow a rule. POSIX, the standard for Unix systems, says the names of its environment variables are made of capital letters, digits and the underscore, and do not begin with a digit. It reserves names with small letters for applications, which can use them without changing how the standard tools behave. So your own names, such as backup_dir, cannot upset those tools.
Why it fits here: It answers this topic's question: POSIX keeps capital names for the system and leaves small-letter names to your own programs.
Source: POSIX.1-2024: Environment Variables
Topic: The environment, and export · standards, words
Web apps keep their settings here
The environment also carries the settings of web apps. The Twelve-Factor App, a guide written by Adam Wiggins from work on the Heroku platform, says such an app stores its config in environment variables. That includes credentials for outside services. Env vars are easy to change between deploys without changing any code, and are less likely than config files to be saved into the project's code by accident.
Why it fits here: It shows a real use of this topic's environment: web servers read their settings, credentials included, from it.
Sources: Adam Wiggins: The Twelve-Factor App; The Twelve-Factor App: III. Config
Topic: The environment, and export · tools, internet
One colon too many
An empty entry in PATH means the current folder. POSIX calls this a legacy feature: it appears as two colons side by side, a colon at the start, or a colon at the end. So a line like PATH=$PATH: quietly makes the shell look in whatever folder you are in. The standard asks programs that want the current folder to write it as a dot instead.
Why it fits here: It answers this topic's question: one stray colon in PATH adds the current folder to the search.
Source: POSIX.1-2024: Environment Variables (PATH)
Topic: How commands are found: PATH · standards, mistakes
bash remembers where it looked
bash does not search PATH every time. Its manual says it keeps a hash table of the full paths of programs it has found, and searches PATH only for a name that is not in the table. Unless the checkhash option is on, it does not check that a remembered program is still there. The command hash -r clears the table, and so does any change to PATH.
Why it fits here: It explains a surprise in this topic's search: bash remembers where it found each program, and hash -r makes it look again.
Source: GNU Bash manual: Command Search and Execution
Topic: How commands are found: PATH · tools, tricks
rc means run commands
The rc at the end of .bashrc is a fossil. Around 1963 Louis Pouzin wrote RUNCOM for the CTSS time-sharing system, a tool that ran a file of commands. Pouzin recalled that people could then go home in the evening and leave long runcoms working overnight. Brian Kernighan and Dennis Ritchie later wrote that such a file came to be called a runcom, and that rc in Unix is a fossil from that usage.
Why it fits here: It answers this topic's question: the rc of .bashrc means run commands, from Louis Pouzin's RUNCOM of about 1963.
Sources: Wikipedia: RUNCOM; Louis Pouzin: The Origin of the Shell
Topic: Settings that last: source and .bashrc · history, words
source came from the C shell
The word source comes from the C shell. Wikipedia notes that the dot command, which runs a file's commands in the current shell, is provided in the C shell as source, and that the C shell is where the command first appeared. POSIX names only the dot, as the command that executes commands in the current environment. bash accepts both, and its manual calls source a synonym for the dot.
Why it fits here: It explains this topic's two names for one job: the dot from the Bourne shell and POSIX, and source from the C shell.
Sources: Wikipedia: Dot (command); FreeBSD manual: tcsh(1); POSIX.1-2024: Shell Command Language; GNU Bash manual: Bash Builtin Commands
Topic: Settings that last: source and .bashrc · history, standards
ll is already there
On Ubuntu, ll works before you make any alias. The .bashrc that Ubuntu copies into each new user's home already defines three: ll as ls -alF, la as ls -A and l as ls -CF. The -a shows names that start with a dot, and -F marks each name with its kind, such as a slash after a folder. Typing alias in a new Ubuntu terminal shows them.
Why it fits here: It answers this topic's question: Ubuntu's own .bashrc already gives ll, la and l as aliases.
Sources: Ubuntu bash package: debian/skel.bashrc; Linux man-pages: useradd(8); GNU Coreutils manual: Which files are listed; GNU Coreutils manual: General output formatting
Topic: Short names: alias · tools, tricks
The manual prefers functions
The bash manual itself prefers functions to aliases. It says there is no way to use arguments inside an alias's replacement text, as there is in csh, and that a shell function should be used when arguments are needed. It ends its section with plain advice: for almost every purpose, shell functions are preferable to aliases.
Why it fits here: It points past this topic's aliases: when a short name needs arguments, the bash manual says to write a function.
Source: GNU Bash manual: Aliases
Topic: Short names: alias · tools, standards
RANDOM and its seed
Some variables change by themselves. The bash manual says each time RANDOM is read, it gives a random whole number from 0 to 32,767. Giving RANDOM a value seeds it, and seeding it with the same number produces the same sequence again. So RANDOM=42 at the top of a test script makes its random choices repeat exactly on every run.
Why it fits here: It answers this topic's question: RANDOM gives a new number each time it is read, from 0 to 32,767.
Source: GNU Bash manual: Bash Variables
Topic: Useful variables, and your prompt · numbers, tricks
One colon, two meanings
In ${name:-default}, the colon matters. The bash manual says that with the colon, the default is used when the variable is unset or empty. Without it, as in ${name-default}, the test is only for a variable that is unset, so an empty value stays empty. So when name holds empty text, ${name:-guest} gives guest, while ${name-guest} gives nothing.
Why it fits here: It sharpens this topic's ${name:-default}: the colon makes the default cover empty values as well as unset ones.
Source: GNU Bash manual: Shell Parameter Expansion
Topic: Useful variables, and your prompt · tools, mistakes
Lesson 8: Bash scripts
#! since January 1980
The #! line is younger than Unix. In a mail dated 10 January 1980, Dennis Ritchie announced that if a file being run begins with the magic characters #!, the rest of the line names the interpreter for it. The mail listed the gains: scripts become more like real programs, and ps shows a script's real name instead of sh. It seems the mark had no name at first.
Why it fits here: It answers this topic's question: Dennis Ritchie added the #! line to Unix in January 1980.
Sources: Wikipedia: Shebang (Unix); Sven Mascheck: The #! magic, details about the shebang mechanism
Topic: Your first script · history, people
The word shell, from 1964
The word shell was coined for this kind of program. Louis Pouzin, then on the computer centre staff at MIT, recalls writing a paper on how the command language of Multics could be designed. Pouzin coined the word shell to name it, at the end of 1964 or the start of 1965. Glenda Schroeder of MIT and an engineer from GE later wrote the first Multics shell.
Why it fits here: It names the program this topic's scripts run in: the word shell was coined by Louis Pouzin around the end of 1964.
Source: Louis Pouzin: The Origin of the Shell
Topic: Your first script · history, words
$10 is not the tenth
$10 does not give the tenth argument. The bash manual says a positional parameter with more than one digit must be written in braces, as ${10}. Without braces, a digit after the $ can only refer to $1 to $9 or to $0. So $10 is read as $1 followed by a plain 0: with the arguments a to j, it gives a0, not j.
Why it fits here: It answers this topic's question: $10 is $1 and a 0, and the tenth argument needs braces, ${10}.
Source: GNU Bash manual: Positional Parameters
Topic: Arguments: $1, $# and "$@" · tools, mistakes
Status 2 for a wrong use
The custom of ending with status 2 after a wrong use matches the shell's own tools. The bash manual says all of bash's builtins return an exit status of 2 to show incorrect usage, generally invalid options or missing arguments. So a script that exits with 2 when its arguments are wrong behaves like cd, read and the other builtins.
Why it fits here: It explains this topic's exit 2: bash's builtins use the same status for incorrect usage.
Source: GNU Bash manual: Exit Status
Topic: Arguments: $1, $# and "$@" · tools, standards
fi and esac from ALGOL 68
fi and esac are borrowed words. Wikipedia explains that Stephen Bourne had worked on an ALGOL 68C compiler at Cambridge. The shell reused ALGOL 68's if ... fi, case ... esac and do ... od, with done in place of od. Bourne even wrote the shell's C code with macros, so that it read like ALGOL: the Seventh Edition source defines IF as if( and FI as ;}.
Why it fits here: It answers this topic's question: if ends with fi and case with esac because Stephen Bourne borrowed them from ALGOL 68.
Sources: Wikipedia: Bourne shell; Seventh Edition Unix source: usr/src/cmd/sh/mac.h
Topic: Decisions: if, test and case · history, languages
[ is also a file
The [ of a test is a real program as well as a builtin. On Ubuntu, the coreutils package, which also holds ls and cp, installs a file named /usr/bin/[. The GNU manual calls [ another form of test, and says the brackets must be separate arguments, so [-d /] does not work. bash has its own built-in [, and it too needs each part as a separate word.
Why it fits here: It explains this topic's spaces inside [ ]: [ is a command, both a bash builtin and a program in /usr/bin.
Sources: Ubuntu packages: coreutils file list (noble, amd64); GNU Coreutils manual: test invocation; GNU Bash manual: Bourne Shell Builtins
Topic: Decisions: if, test and case · tools, mistakes
A for with no list
A for loop can leave out its list. The bash manual says that when in words is not written, the loop runs once for each positional parameter, as if in "$@" had been written. So for arg; do echo "$arg"; done in a script prints each of its arguments on its own line, with any spaces inside them kept.
Why it fits here: It answers this topic's question: a for loop with no list runs over the script's arguments, as if "$@" were written.
Source: GNU Bash manual: Looping Constructs
Topic: Loops: for and while · tools, tricks
Even true can fail
while true runs forever because true always succeeds, or nearly always. The GNU manual says true does nothing except return an exit status of 0, and that the shell's built-in colon can do the same, faster. Yet it adds that the program true can be made to fail: asked for --help or --version while its output is closed, it reports a write error and exits with a nonzero status.
Why it fits here: It adds a curiosity to this topic's while loops: true always succeeds, except when asked to print help with nowhere to print it.
Source: GNU Coreutils manual: true invocation
Topic: Loops: for and while · tools, words
return 256 means 0
A function's status is small. The bash manual says that when return is given a number, the status is its least significant 8 bits. So statuses run from 0 to 255, and return 256 gives 0, which means success. To hand back a bigger number or a word, a function prints it, and the caller keeps it with $(...).
Why it fits here: It answers this topic's question: return keeps only 8 bits, so return 256 is the same as return 0.
Source: GNU Bash manual: Bourne Shell Builtins
Topic: Functions · numbers, mistakes
$( ) nests, backquotes struggle
$(...) is the newer form of an older idea, the backquote. The bash manual says command substitutions may be nested, and that nesting the backquoted form needs a backslash before each inner backquote. So $(basename $(pwd)) reads cleanly, while the backquoted form is harder to write and read. Inside double quotes, the manual adds, the result is not split into words.
Why it fits here: It explains this topic's $(command): it nests inside itself with no backslashes, unlike the older backquotes.
Source: GNU Bash manual: Command Substitution
Topic: Functions · tools, history
set -e lets some failures through
set -e does not stop a script at every failure. The bash manual lists where a failed command is let through. Those places are the test of an if, the list after while or until, and any part of a && or || list except the last. They also include a pipeline's commands before the last, unless pipefail is on, and a command after a !. So if grep -q x file never stops the script, which is what an if needs.
Why it fits here: It answers this topic's question: set -e lets failures through inside if tests, while loops, && and || lists, and pipelines.
Source: GNU Bash manual: The Set Builtin
Topic: Safe scripts: set -euo pipefail and trap · tools, mistakes
pipefail joined POSIX in 2024
set -o pipefail is now a standard. POSIX.1-2024, the edition known as Issue 8, records that it added -o pipefail to the set command; bash had the option already. The standard also says a pipeline uses the pipefail setting in force when the pipeline begins, not the one in force when its status is set.
Why it fits here: It shows that this topic's pipefail is now part of the POSIX standard, added in its 2024 edition.
Sources: POSIX.1-2024: Shell Command Language; GNU Bash manual: The Set Builtin
Topic: Safe scripts: set -euo pipefail and trap · standards, history
No fact matches. Try a shorter word, or take the tag off.