This smells a lot like an XY problem. If you want oldcommand
to treat all relative file names as relative to a different directory, change the current directory!
(cd path && oldcommand -nonfileoption1 x1 -fileoption2 x2 -fileoption3 x3 -nonfileoption4 x4)
The parentheses ensure that the directory change is local: the next command will be executed in the original directory.
If you want to package this in a script:
#!/bin/sh
cd "$1" && shift && "$@"
Usage:
run-in path oldcommand -nonfileoption1 x1 -fileoption2 x2 -fileoption3 x3 -nonfileoption4 x4
For command line usage, though, a temporary directory change might be more convenient. It requires a smidgen more typing, but it allows filename completion to work out of the box. (You can make completion work for the wrapper script method, but you'll need to define a completion function for the run-in
script.)
cd path
oldcommand -nonfileoption1 x1 -fileoption2 x2 -fileoption3 x3 -nonfileoption4 x4
cd -
or
pushd path
oldcommand -nonfileoption1 x1 -fileoption2 x2 -fileoption3 x3 -nonfileoption4 x4
popd
Compared to what you're asking, this makes all relative file names relative to path
, not just the ones passed on the command line but any relative file name that oldcommand
uses internally. This also assumes that oldcommand
doesn't itself change to a different directory. Both assumptions are met by many commands.
newcommand
know which arguments ofoldcommand
needed prefixing withpath
? You would have to writenewcommand
yourself (or get someone to write it for you).oldcommand
.