Sample Header Ad - 728x90

Is there a POSIX (or at least a popular) utility to set the current working directory when invoking a program?

23 votes
4 answers
3555 views
We have env(1) to modify the environment of the command we want to run (for example env MANPAGER=more man dtrace). Is there something similar but for modifying the directory that the command is going to be started in? Ideally, I would like it to look like this: theMagicCommand /new/cwd myProgram This way it could be "chained" with other env(1)-like commands, e.g., daemon -p /tmp/pid env VAR=value theMagicCommand /new/cwd myProgram ---- So far I can think of the following solution, which unfortunately does not have the same *interface* as env(1): cd /new/cwd && myProgram Also, I can just create a simple shell script like this: #! /bin/sh - cd "${1:?Missing the new working directory}" || exit 1 shift exec "${@:?Missing the command to run}" but I am looking for something that already exists (at least on macOS and FreeBSD). myProgram is not necessarily a desktop application (in which case I could just use the *Path* key in a .desktop file ).
Asked by Mateusz Piotrowski (4983 rep)
Nov 9, 2018, 03:01 PM
Last activity: Nov 11, 2018, 12:36 PM