Premium lesson

Generating Dynamic Pathnames

Shell·2 min read·Jan 1, 2025

The shell expansion refers to the process by which the shell interprets certain special characters or character sequences and expands them into treatable command parameters before executing a command.

In Bash, there are essentially three expansions for dynamically generating pathnames based on patterns:

  1. The tilde expansion

  2. The wildcard expansion

  3. The brace expansion

The tilde expansion

In Bash, the tilde character ~ is a shortcut for the path to the home directory.

It is used to write relative pathnames, starting from the home directory, regardless of the current working directory.

Example

For example, assuming the current user account is johndoe, the following command will change the current working directory to /home/johndoe/Documents on Linux and /Users/johndoe/Documents on macOS:

$ cd ~/Documents
icon light bulb key

Unlock the CLI & Scripting with Bash module

Learn how to gain advanced control over the OS and automate complex routine tasks prone to manual errors with the CLI and Bash scripting.

You get immediate access to:

  • 34 focused lessons across the CLI and Bash
  • 4 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module