Functions

The zip sources these before your code. Names start with a dot. Bash and zsh ship the same names.

Do not call .tmpdir, .tmpfile, .on_exit, or .lock from a substitution. An EXIT trap of your own replaces the one these helpers use. Call .cleanup from that trap if the paths should still be removed.

The readme in the zip lists these too. This page does not paste the function file.

Stop and continue

NameWhat it does
.diePrints its arguments to stderr and exits 1.
.warnPrints its arguments to stderr and continues.
.needExits 1 when a command is missing and names that command.
.haveReturns 0 when a command exists and 1 when it does not.
.need_fileExits 1 when the path is not a file and names that path.
.need_dirExits 1 when the path is not a directory and names that path.
.strictStops on the first failed command, including a pipeline, and on an unset variable.
.quietRuns a command with no output and returns its status.
.retryRuns a command until it returns 0, at most N times. N must be a positive whole number.
.confirmReads a line and returns 0 for y or yes.
.quotePrints one word quoted for the shell.
.logWrites a log line. The first argument is the level. --verbose raises the level.

Temporary paths

NameWhat it does
.tmpdirCreates a directory, stores the path in the name you pass, and removes it when the script exits.
.tmpfileDoes the same for a file.
.on_exitRuns its command when the script exits.
.cleanupRemoves those paths now.
.lockCreates a directory and removes it when the script exits. A second call prints locked: and returns 1.

Text

NameWhat it does
.trimPrints the string without leading or trailing whitespace.
.blankReturns 0 when the trimmed string is empty.
.lowerLowercases ASCII letters.
.upperUppercases ASCII letters.
.lenPrints the length.
.containsReturns 0 when the string contains the text.
.prefixReturns 0 when the string starts with the text.
.suffixReturns 0 when the string ends with the text.
.strip_prefixRemoves a matching prefix and returns 1 when it is absent.
.strip_suffixRemoves a matching suffix and returns 1 when it is absent.
.replaceReplaces every copy of a literal string. An empty search string returns 1.
.joinPrints the later arguments separated by the first argument.
.repeatPrints the string a given number of times and returns 1 when that count is not a whole number.
.contains_wordMatches one word separated by whitespace.
.matchesCompares a string to a glob.

Lines

NameWhat it does
.foreachRuns a command once per line and passes the line as the last argument. The line is not split on spaces.
.count_linesPrints the line count.
.first_linePrints the first line and returns 1 when the file is empty.
.last_linePrints the last line and returns 1 when the file is empty.
.indentPrefixes each line with the number of spaces you pass.
.unique_linesPrints the first copy of each line.
.sort_linesPrints the lines in byte order. A missing file returns 1.

Paths

NameWhat it does
.abspathPrints the physical path. An empty argument returns 1.
.ensure_dirCreates a directory and prints that path. It returns 1 when the path is a file.
.existsReturns 0 for a file, a directory, or a symlink.
.readableReturns 0 when the path is readable.
.writableReturns 0 when the path is writable.
.backupCopies a file to a sibling whose name ends in .bak. The next copy uses .bak.1.
.extensionPrints the suffix after the last dot. A name with no suffix prints an empty line.
.stripextRemoves that suffix and keeps the directory.
.empty_dirReturns 0 when the directory has no entries and 1 when it is missing or not empty.
.is_fileReturns 0 or 1 and does not exit. It follows a symlink.
.is_dirReturns 0 or 1 and does not exit.
.is_linkReturns 0 or 1 and does not exit. It does not follow the symlink.
.is_execReturns 0 or 1 and does not exit.
.basenamePrints the last path part. A name that starts with a dash stays a name.
.dirnamePrints the directory part. An empty argument returns 1.
.list_dirPrints one entry name per line, including names that start with a dot.
.sameReturns 0 when two files have the same bytes.
.filesizePrints the byte count.
.sha256Prints the checksum and nothing else.

Numbers and URLs

NameWhat it does
.is_intReturns 0 for a whole number. A leading plus or minus is allowed.
.leastPrints the smaller of two whole numbers.
.greatestPrints the larger of two whole numbers.
.clampKeeps a number inside a low and high bound. A reversed bound returns 1.
.urlencodePercent-encodes every character except letters, digits, period, tilde, underscore, and hyphen.
.urldecodeTurns a percent-encoded pair back into a character. A bad pair stays as text.