Base

path_exists

Test whether a path exists (any type).

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path exists
  • 1: Path does not exist

path_is_file

Test whether a path is a regular file.

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path is a regular file
  • 1: Path is not a regular file

path_is_directory

Test whether a path is a directory.

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path is a directory
  • 1: Path is not a directory

Test whether a path has more than one hard link (i.e. shares an inode with at least one other name). Uses stat; tries GNU format first, then BSD format.

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path has a link count greater than 1
  • 1: Path does not, is not a regular file, or stat is unavailable

Test whether a path is a symbolic link.

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path is a symlink
  • 1: Path is not a symlink

path_is_readable

Test whether a path is readable by the current process.

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path is readable
  • 1: Path is not readable

path_is_writeable

Test whether a path is writeable by the current process.

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path is writeable
  • 1: Path is not writeable

path_is_executable

Test whether a path is executable by the current process.

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path is executable
  • 1: Path is not executable

path_is_absolute

Test whether a path is absolute (starts with /).

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path is absolute
  • 1: Path is not absolute

path_is_relative

Test whether a path is relative (does not start with /).

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path is relative
  • 1: Path is not relative

path_is_empty_dir

Test whether a path is a directory that contains no files.

Arguments

  • $1 (string): Path to test

Exit codes

  • 0: Path is an empty directory
  • 1: Path is not an empty directory or does not exist

path_is_gitdir

Test whether a path is inside a git repository. Defaults to the current directory if no path is given.

Arguments

  • $1 (string): Path to test (default: current directory)

Exit codes

  • 0: Path is inside a git repository
  • 1: Path is not inside a git repository

path_absolute

Convert a relative path to an absolute path without using readlink -f. Works for both files and directories that exist on disk. Returns 1 if the path does not exist. Temporarily clears CDPATH to avoid interference.

Arguments

  • $1 (string): Relative or absolute file/directory path

Exit codes

  • 0: Success
  • 1: Path does not exist

Output on stdout

  • Absolute path

path_basename

Strip the leading directory path from a filename. Pure parameter expansion equivalent of basename(1). Does not support the suffix-stripping second argument.

Arguments

  • $1 (string): File path

Exit codes

  • 0: Always

Output on stdout

  • Filename component only

path_dirname

Strip the filename component, leaving the directory path. Pure parameter expansion equivalent of dirname(1). Does not handle dotfiles, tilde, or other edge cases.

Arguments

  • $1 (string): File path

Exit codes

  • 0: Always

Output on stdout

  • Directory component of the path

path_extension

Get the file extension from a path (without the leading dot). Pure parameter expansion — no subshells.

Example

path_extension "/foo/bar.txt"        # => "txt"
path_extension "/foo/archive.tar.gz" # => "gz"
path_extension "/foo/noext"          # => exit 1

Arguments

  • $1 (string): File path

Exit codes

  • 0: Success; 1 No extension found; 2 Missing argument

Output on stdout

  • Extension string, e.g. "sh", "txt"

path_stem

Get the filename without its extension from a path (the stem). Pure parameter expansion — no subshells.

Example

path_stem "/foo/bar.txt"   # => "bar"
path_stem "/foo/bar"       # => "bar"

Arguments

  • $1 (string): File path

Exit codes

  • 0: Success; 2 Missing argument

Output on stdout

  • Filename without extension

path_strip_extension

Remove the file extension from a path (returns full path minus .ext). Pure parameter expansion — no subshells.

Example

path_strip_extension "/foo/bar.txt"        # => "/foo/bar"
path_strip_extension "/foo/archive.tar.gz" # => "/foo/archive.tar"
path_strip_extension "/foo/noext"          # => "/foo/noext"

Arguments

  • $1 (string): File path

Exit codes

  • 0: Success; 2 Missing argument

Output on stdout

  • Path without final extension

path_replace_extension

Replace the file extension of a path. The replacement extension should include the leading dot.

Example

path_replace_extension "/foo/bar.txt" ".md"    # => "/foo/bar.md"
path_replace_extension "/foo/bar"     ".sh"    # => "/foo/bar.sh"

Arguments

  • $1 (string): File path
  • $2 (string): New extension (with leading dot, e.g. ".sh")

Exit codes

  • 0: Success; 2 Missing argument

Output on stdout

  • Path with replaced extension

path_normalize

Normalize a path by resolving . and .. components purely as a string operation — the path does not need to exist on disk. Multiple consecutive slashes are collapsed to one. A trailing slash is preserved only for the root "/".

Example

path_normalize "/foo/bar/../baz"      # => /foo/baz
path_normalize "/foo/./bar"           # => /foo/bar
path_normalize "foo//bar"             # => foo/bar
path_normalize "/a/b/c/../../d"       # => /a/d

Arguments

  • $1 (string): Path to normalize

Exit codes

  • 0: Success; 2 Missing argument

Output on stdout

  • Normalized path

path_relative

Compute the relative path from a base directory to a target path. Both arguments are normalized before comparison (no readlink, works on non-existent paths). Both must be absolute or both must be relative.

Example

path_relative /foo/bar /foo/bar/baz    # => baz
path_relative /foo/bar /foo/qux        # => ../qux
path_relative /a/b/c   /x/y           # => ../../../x/y

Arguments

  • $1 (string): Base directory (from)
  • $2 (string): Target path (to)

Exit codes

  • 0: Success; 2 Missing argument

Output on stdout

  • Relative path

path_is_newer

Test whether path A is newer than path B (mtime comparison).

Example

path_is_newer /etc/passwd /etc/shadow

Arguments

  • $1 (string): Path A
  • $2 (string): Path B

Exit codes

  • 0: A is newer than B
  • 1: A is not newer than B, or either path does not exist

path_is_older

Test whether path A is older than path B (mtime comparison).

Example

path_is_older /etc/shadow /etc/passwd

Arguments

  • $1 (string): Path A
  • $2 (string): Path B

Exit codes

  • 0: A is older than B
  • 1: A is not older than B, or either path does not exist

path_is_same_file

Test whether two paths refer to the same file (same device and inode). Returns true for hard links to the same underlying inode.

Example

ln /etc/passwd /tmp/passwd_link
path_is_same_file /etc/passwd /tmp/passwd_link   # => exit 0

Arguments

  • $1 (string): First path
  • $2 (string): Second path

Exit codes

  • 0: Both paths refer to the same file
  • 1: Paths differ, or either path does not exist