433

Using a Linux shell, how do I start a program with a different working directory from the current working directory?

For example, I have a binary file helloworld that creates the file hello-world.txt in the current directory.

This file is inside of directory /a.

Currently, I am in the directory /b. I want to start my program running ../a/helloworld and get the hello-world.txt somewhere in a third directory /c.

3
  • 6
    I discovered the hard way that su resets the working directory to the home directory of user you specify before running any -c commands. This was very helpful to me. Commented Jan 16, 2014 at 19:31
  • 10
    It's been 12 years since I've posted this question and today I looked it up myself, cause I could not recall how to do that. Commented Dec 16, 2021 at 20:51
  • 3
    It's one thing to come back to a SO post with a:visited link and an upvote already on it. It's another thing when you're the asker, or even the answerer. There should be a badge for that... "Forgetful" Commented Dec 16, 2021 at 21:20

12 Answers 12

682

Call the program like this:

(cd /c; /a/helloworld)

The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to /c, then executes helloworld from /a. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.

Error handling: To avoid running the program without having changed the directory, e.g. when having misspelled /c, make the execution of helloworld conditional:

(cd /c && /a/helloworld)

Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call helloworld via exec:

(cd /c && exec /a/helloworld)

[Thanks to Josh and Juliano for giving tips on improving this answer!]

Sign up to request clarification or add additional context in comments.

7 Comments

Any way to pass arguments to this shell? As in $1, and $2?
@segfault: The subshell has complete access to the surrounding scope.
You can pass all arguments by doing $*, $@ or "$@" (if you want arguments to respect double quotes)
WIll it work if I add this line to /etc/rc.d/rc.local ?
the entire () construct will have the exitcode of the last command inside. You can check that either directly through chaining or by inspecting $?.
|
120

Similar to David Schmitt's answer, plus Josh's suggestion, but doesn't leave a shell process running:

(cd /c && exec /a/helloworld)

This way is more similar to how you usually run commands on the shell. To see the practical difference, you have to run ps ef from another shell with each solution.

2 Comments

Just say it here — exec replaces the shell process with the given command, without creating a new process.
39

Using pushd/popd and a sub shell:

(pushd SOME_PATH && run_stuff; popd)

If you don't want a subshell, you could run them as separate commands, but be aware that if any of them fail, your shell might not end up back in the original directory at the end:

pushd SOME_PATH
run_stuff
popd

Demo:

$ pwd
/home/abhijit
$ pushd /tmp # directory changed
$ pwd
/tmp
$ popd
$ pwd
/home/abhijit

11 Comments

A similar suggestion has been done below by Sahil. It does not work if the command fails. Consider pushd SOME_PATH && run_stuff && popd -- if run_stuff fails, than popd is not going to be executed.
Late reply, that depends on the settings of the bash file. Bash can continue executing commands even after a failed command (unlike using &&), but it can be set to not do that using set -e in the file and then it would fail to popd.
Still, I think pushd "${SOME_PATH}" && run_stuff; popd is better than the current answer, since the pushd/popd semantics were specifically designed for this situation of going into some directory and then coming back to the original one.
How does it work as defined as alias and I need to pass a param?
But ... the parentheses create a subshell.
|
22
sh -c 'cd /c && ../a/helloworld'

1 Comment

Used this for FreeBSD's jexec to execute a command inside jail in specified working directory.
16

Just change the last "&&" into ";" and it will cd back no matter if the command fails or succeeds:

cd SOME_PATH && run_some_command ; cd -

Comments

10

I always think UNIX tools should be written as filters, read input from stdin and write output to stdout. If possible you could change your helloworld binary to write the contents of the text file to stdout rather than a specific file. That way you can use the shell to write your file anywhere.

$ cd ~/b

$ ~/a/helloworld > ~/c/helloworld.txt

1 Comment

+1 for being right, although the answer is only peripherally an answer.
6

why not keep it simple

cd SOME_PATH && run_some_command && cd -

the last 'cd' command will take you back to the last pwd directory. This should work on all *nix systems.

2 Comments

You are write @mezhaka, should have considered that :)
Warning: If run_some_command fails, cd - won't get executed.
4

One way to do that is to create a wrapper shell script.

The shell script would change the current directory to /c, then run /a/helloworld. Once the shell script exits, the current directory reverts back to /b.

Here's a bash shell script example:

#!/bin/bash
cd /c
/a/helloworld

Comments

3

The following worked for my simple need to set up an alias to which I can append an argument, without having to put the instructions in a function; worked in Bash and ZSH:

$ CWD=/your/target/dir command args

My use case: I have a shell script named run-service that resolves paths to other files based on its own location. If I call it with cd ~/Code/company/scripts && run-service dev/some-service, it will expect to find a config file in ~/Code/company/services/dev/some-service. So instead of always having to cd into the directory and calling the script, I just did this:

# Alias definition
alias run-service="CWD=/your/target/dir run-service"

# Calling the alias
$ run-service foo

It's probably too simplistic to be of general use, but it works for my basic use-case.

Comments

2

If you always want it to go to /C, use an absolute path when you write the file.

Comments

0

If anyone is still searching for this nowadays, here is how I did it on Windows 10:

Set-Location -Path "<absolute path to your working directory>"; Start-Process "<absolute path to your file>"

Comments

-1

If you want to perform this inside your program then I would do something like:

#include <unistd.h>
int main()
{
  if(chdir("/c") < 0 )  
  {
     printf("Failed\n");
     return -1 ;
  }

  // rest of your program...

}

1 Comment

He wants to do that in a shell-script, and not in a C. Also, it would be a horrible idea to subprocess the binary file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.