The Working Directory of a Shell Command

A relative path is generally resolved from the process's current working directory. Commands started by a shell normally inherit that directory unless their launcher changes it.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a nested path
  2. Make one caller's assumption visible
  3. Resolve an unchanged argument in two process states
  4. Observe the current directory in the child when debugging

Trace a nested path

A command run from a project root can open data/report.csv. Running it from the data directory would normally look for another nested data directory. See relative path resolution.

Make one caller's assumption visible

Provide an explicit working directory or build a known path when automation expects a particular base. An auditable reproduction records the base alongside the command.[1]

Resolve an unchanged argument in two process states

A command accepting inputs/jobs.txt resolves that relative name from its working directory. Calling it from the parent directory and then from tools can refer to two different paths. Pass a known working directory or use a path relative to the intended resource base. See absolute and relative paths.

With working directory /work/project → /work/project/inputs/jobs.txt
With /work/project/tools → /work/project/tools/inputs/jobs.txt

Observe the current directory in the child when debugging

A script path and the working directory are separate inputs. A wrapper changing directories can alter where output appears too. Test from a clean unrelated directory to confirm the intended behavior.

References

  1. The Open Group, POSIX Shell Command Language ↩