425

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.
    – Patrick M
    Jan 16, 2014 at 19:31
  • 8
    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. Dec 16, 2021 at 20:51
  • 2
    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"
    – Patrick M
    Dec 16, 2021 at 21:20

11 Answers 11

671

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!]

7
  • 3
    Any way to pass arguments to this shell? As in $1, and $2?
    – finiteloop
    May 22, 2011 at 0:34
  • 2
    @segfault: The subshell has complete access to the surrounding scope. May 23, 2011 at 8:26
  • 2
    You can pass all arguments by doing $*, $@ or "$@" (if you want arguments to respect double quotes) Nov 29, 2016 at 1:38
  • 1
    WIll it work if I add this line to /etc/rc.d/rc.local ? May 15, 2018 at 8:12
  • 3
    the entire () construct will have the exitcode of the last command inside. You can check that either directly through chaining or by inspecting $?. May 7, 2020 at 15:53
117

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
38

An option which doesn't require a subshell and is built in to bash

(pushd SOME_PATH && run_stuff; popd)

Demo:

$ pwd
/home/abhijit
$ pushd /tmp # directory changed
$ pwd
/tmp
$ popd
$ pwd
/home/abhijit
8
  • 2
    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. Jul 15, 2015 at 16:58
  • 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.
    – Loren
    Apr 22, 2016 at 20:48
  • 9
    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. Nov 29, 2016 at 1:40
  • How does it work as defined as alias and I need to pass a param?
    – DrB
    Apr 7, 2017 at 8:37
  • 1
    But ... the parentheses create a subshell.
    – tripleee
    Apr 17 at 13:04
22
sh -c 'cd /c && ../a/helloworld'
1
  • 1
    Used this for FreeBSD's jexec to execute a command inside jail in specified working directory. Aug 15, 2017 at 5:31
14

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 -
9

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
  • 6
    +1 for being right, although the answer is only peripherally an answer. Apr 25, 2009 at 8:04
5

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
  • You are write @mezhaka, should have considered that :)
    – Sahil
    Jan 6, 2015 at 4:55
  • 3
    Warning: If run_some_command fails, cd - won't get executed. Mar 12, 2020 at 12:19
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
2

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

1

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.

-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
  • 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.
    – user689383
    Jan 10, 2015 at 3:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.