Is there a command to run an arbitrary command in a specific directory?
In Debian 10, I could simply use env --chdir
. However, env --chdir
is not available on my Debian 9 install.
Part of the command I want to run is composed using shell variables, which could contain spaces or other special characters. For example, imagine the command I want to run is:
strace -- "$@"
Therefore, the following would not work:
sudo -i sh -c 'cd my-directory && exec strace -- "$@"'
uh, why do you want to do this?
I want to use sudo -i
/ sudo --login
to run a command. But I want to run the command in a specific directory, not the home directory of the root user. Although sudo
can set environment variables, it does not appear to have an option to change directory.
I am using sudo -i
because: 1) I want the program I am running (my Ansible playbook) to have HOME=/root
, which will avoid the program creating root-owned files in my home directory. 2) This sounded like an example of a family of problems with sudo
without -i
; an entirely fresh environment sounded like the most principled way to solve my problem.
exec
andsudo
. – ctrl-alt-delor Aug 16 '19 at 15:58sudo
part, I want to implementenv --chdir
in shell, becauseenv --chdir
is not available. – sourcejedi Aug 16 '19 at 16:01env
does not have that option. – ctrl-alt-delor Aug 16 '19 at 16:02