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









up vote
15
down vote

favorite
2












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).










share|improve this question



















  • 6




    Why does cd /new/cwd && env VAR=value myProgram not meet your critera?
    – jpaugh
    yesterday











  • That's already in the question, where M. Piotrowski explained that that is not the same interface as env. Look at env. Compare it to rtprio, idprio, numactl, jexec, chrt, and indeed the commands in the toolsets mentioned in the answers. There's a pattern, and it is chain loading.
    – JdeBP
    yesterday







  • 2




    What do you mean by “interface”? And why not use (cd the/cwd; cmd)?
    – Konrad Rudolph
    13 hours ago






  • 1




    @KonradRudolph For example, you cannot easily pass (cd the/cwd; cmd) to env(1) without wrapping it with sh(1).
    – Mateusz Piotrowski
    3 hours ago














up vote
15
down vote

favorite
2












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).










share|improve this question



















  • 6




    Why does cd /new/cwd && env VAR=value myProgram not meet your critera?
    – jpaugh
    yesterday











  • That's already in the question, where M. Piotrowski explained that that is not the same interface as env. Look at env. Compare it to rtprio, idprio, numactl, jexec, chrt, and indeed the commands in the toolsets mentioned in the answers. There's a pattern, and it is chain loading.
    – JdeBP
    yesterday







  • 2




    What do you mean by “interface”? And why not use (cd the/cwd; cmd)?
    – Konrad Rudolph
    13 hours ago






  • 1




    @KonradRudolph For example, you cannot easily pass (cd the/cwd; cmd) to env(1) without wrapping it with sh(1).
    – Mateusz Piotrowski
    3 hours ago












up vote
15
down vote

favorite
2









up vote
15
down vote

favorite
2






2





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).










share|improve this question















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).







shell freebsd cwd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 13 hours ago

























asked yesterday









Mateusz Piotrowski

1,84321540




1,84321540







  • 6




    Why does cd /new/cwd && env VAR=value myProgram not meet your critera?
    – jpaugh
    yesterday











  • That's already in the question, where M. Piotrowski explained that that is not the same interface as env. Look at env. Compare it to rtprio, idprio, numactl, jexec, chrt, and indeed the commands in the toolsets mentioned in the answers. There's a pattern, and it is chain loading.
    – JdeBP
    yesterday







  • 2




    What do you mean by “interface”? And why not use (cd the/cwd; cmd)?
    – Konrad Rudolph
    13 hours ago






  • 1




    @KonradRudolph For example, you cannot easily pass (cd the/cwd; cmd) to env(1) without wrapping it with sh(1).
    – Mateusz Piotrowski
    3 hours ago












  • 6




    Why does cd /new/cwd && env VAR=value myProgram not meet your critera?
    – jpaugh
    yesterday











  • That's already in the question, where M. Piotrowski explained that that is not the same interface as env. Look at env. Compare it to rtprio, idprio, numactl, jexec, chrt, and indeed the commands in the toolsets mentioned in the answers. There's a pattern, and it is chain loading.
    – JdeBP
    yesterday







  • 2




    What do you mean by “interface”? And why not use (cd the/cwd; cmd)?
    – Konrad Rudolph
    13 hours ago






  • 1




    @KonradRudolph For example, you cannot easily pass (cd the/cwd; cmd) to env(1) without wrapping it with sh(1).
    – Mateusz Piotrowski
    3 hours ago







6




6




Why does cd /new/cwd && env VAR=value myProgram not meet your critera?
– jpaugh
yesterday





Why does cd /new/cwd && env VAR=value myProgram not meet your critera?
– jpaugh
yesterday













That's already in the question, where M. Piotrowski explained that that is not the same interface as env. Look at env. Compare it to rtprio, idprio, numactl, jexec, chrt, and indeed the commands in the toolsets mentioned in the answers. There's a pattern, and it is chain loading.
– JdeBP
yesterday





That's already in the question, where M. Piotrowski explained that that is not the same interface as env. Look at env. Compare it to rtprio, idprio, numactl, jexec, chrt, and indeed the commands in the toolsets mentioned in the answers. There's a pattern, and it is chain loading.
– JdeBP
yesterday





2




2




What do you mean by “interface”? And why not use (cd the/cwd; cmd)?
– Konrad Rudolph
13 hours ago




What do you mean by “interface”? And why not use (cd the/cwd; cmd)?
– Konrad Rudolph
13 hours ago




1




1




@KonradRudolph For example, you cannot easily pass (cd the/cwd; cmd) to env(1) without wrapping it with sh(1).
– Mateusz Piotrowski
3 hours ago




@KonradRudolph For example, you cannot easily pass (cd the/cwd; cmd) to env(1) without wrapping it with sh(1).
– Mateusz Piotrowski
3 hours ago










4 Answers
4






active

oldest

votes

















up vote
17
down vote



accepted










AFAIK, there is no such dedicated utility in the POSIX tool chest. But it's common to invoke sh to set up an environment (cwd, limits, stdout/in/err, umask...) before running a command as you do in your sh script.



But you don't have to write that script in a file, you can just inline it:



sh -c 'CDPATH= cd -P -- "$1" && shift && exec "$@"' sh /some/dir cmd args


(assuming the directory is not -). Adding CDPATH= (in case there's one in the environment) and -P for it to behave more like a straight chdir().



Alternatively, you could use perl whose chdir() does a straight chdir() out of the box.



perl -e 'chdir(shift@ARGV) or die "chdir: $!"; exec @ARGV or die "exec: $!"
' /some/dir cmd args





share|improve this answer






















  • Oh, yeah. This is indded a very handy variation of the script I included in my question. I was afraid that the utility I am looking for does not exist since its functionality is already available with a relatively short sh(1) oneliner.
    – Mateusz Piotrowski
    yesterday






  • 7




    If you're already running sh, you can also do (cd /wherever && exec /my/command). The () implicitly opens a subshell to run the wrapped commands, and of course, exec gets rid of the extra shell process as quickly as /my/command starts running.
    – jpaugh
    yesterday







  • 1




    That was suggested in a now-deleted answer. The questioner explained that that was not an answer to what xe asked.
    – JdeBP
    23 hours ago

















up vote
15
down vote













The toolsets used in the daemontools world, and elsewhere, have this and more besides; have had for many years; and are widely available.



  • Wayne Marshall's perp has runtool:
    runtool -c /new/cwd myProgram


  • Laurent Bercot's execline has cd:
    cd /new/cwd myProgram



  • my nosh toolset has chdir:
    chdir /new/cwd myProgram


All of these are chain-loading tools, designed to be used in exactly these sorts of chains. There is a wide selection of chain-loading tools in these toolkits for other purposes.



Further reading



  • https://unix.stackexchange.com/a/353698/5132

  • Laurent Bercot (2018-08-01). "Reference". execline. skarnet.org.

  • Laurent Bercot (2018-11-08). "Reference". s6. skarnet.org.

  • Jonathan de Boyne Pollard (2018). "Command and tool list". nosh Guide. Softwares.





share|improve this answer



























    up vote
    1
    down vote













    Certain programs have an option for this, like Git:




    -C <path>



    Run as if git was started in <path> instead of the current working
    directory.




    and Make:




    -C dir, --directory=dir



    Change to directory dir before reading the makefiles or doing anything else.




    and Tar:




    -C, --directory=DIR



    Change to DIR before performing any operations. This option is
    order-sensitive, i.e. it affects all options that follow.







    share|improve this answer






















    • Yes. Although my question is what if myProgram does not provide such an option... Thank you for your contribution but I'm afraid it does not answer my question at all.
      – Mateusz Piotrowski
      3 hours ago


















    up vote
    1
    down vote













    There is such a popular program. It is called ... hold onto your chair... drumroll... env. (The GNU version, since version 8.28, not POSIX).




    NAME
    env - run a program in a modified environment

    SYNOPSIS
    env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

    DESCRIPTION
    Set each NAME to VALUE in the environment and run COMMAND.

    Mandatory arguments to long options are mandatory for short options too.

    -i, --ignore-environment
    start with an empty environment

    -0, --null
    end each output line with NUL, not newline

    -u, --unset=NAME
    remove variable from the environment

    -C, --chdir=DIR
    change working directory to DIR

    --help display this help and exit

    --version
    output version information and exit

    A mere - implies -i. If no COMMAND, print the resulting environment.






    share|improve this answer






















    • why is this being downvoted?
      – Adrian
      1 hour ago










    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480776%2fis-there-a-posix-or-at-least-a-popular-utility-to-set-the-current-working-dire%23new-answer', 'question_page');

    );

    Post as a guest






























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    17
    down vote



    accepted










    AFAIK, there is no such dedicated utility in the POSIX tool chest. But it's common to invoke sh to set up an environment (cwd, limits, stdout/in/err, umask...) before running a command as you do in your sh script.



    But you don't have to write that script in a file, you can just inline it:



    sh -c 'CDPATH= cd -P -- "$1" && shift && exec "$@"' sh /some/dir cmd args


    (assuming the directory is not -). Adding CDPATH= (in case there's one in the environment) and -P for it to behave more like a straight chdir().



    Alternatively, you could use perl whose chdir() does a straight chdir() out of the box.



    perl -e 'chdir(shift@ARGV) or die "chdir: $!"; exec @ARGV or die "exec: $!"
    ' /some/dir cmd args





    share|improve this answer






















    • Oh, yeah. This is indded a very handy variation of the script I included in my question. I was afraid that the utility I am looking for does not exist since its functionality is already available with a relatively short sh(1) oneliner.
      – Mateusz Piotrowski
      yesterday






    • 7




      If you're already running sh, you can also do (cd /wherever && exec /my/command). The () implicitly opens a subshell to run the wrapped commands, and of course, exec gets rid of the extra shell process as quickly as /my/command starts running.
      – jpaugh
      yesterday







    • 1




      That was suggested in a now-deleted answer. The questioner explained that that was not an answer to what xe asked.
      – JdeBP
      23 hours ago














    up vote
    17
    down vote



    accepted










    AFAIK, there is no such dedicated utility in the POSIX tool chest. But it's common to invoke sh to set up an environment (cwd, limits, stdout/in/err, umask...) before running a command as you do in your sh script.



    But you don't have to write that script in a file, you can just inline it:



    sh -c 'CDPATH= cd -P -- "$1" && shift && exec "$@"' sh /some/dir cmd args


    (assuming the directory is not -). Adding CDPATH= (in case there's one in the environment) and -P for it to behave more like a straight chdir().



    Alternatively, you could use perl whose chdir() does a straight chdir() out of the box.



    perl -e 'chdir(shift@ARGV) or die "chdir: $!"; exec @ARGV or die "exec: $!"
    ' /some/dir cmd args





    share|improve this answer






















    • Oh, yeah. This is indded a very handy variation of the script I included in my question. I was afraid that the utility I am looking for does not exist since its functionality is already available with a relatively short sh(1) oneliner.
      – Mateusz Piotrowski
      yesterday






    • 7




      If you're already running sh, you can also do (cd /wherever && exec /my/command). The () implicitly opens a subshell to run the wrapped commands, and of course, exec gets rid of the extra shell process as quickly as /my/command starts running.
      – jpaugh
      yesterday







    • 1




      That was suggested in a now-deleted answer. The questioner explained that that was not an answer to what xe asked.
      – JdeBP
      23 hours ago












    up vote
    17
    down vote



    accepted







    up vote
    17
    down vote



    accepted






    AFAIK, there is no such dedicated utility in the POSIX tool chest. But it's common to invoke sh to set up an environment (cwd, limits, stdout/in/err, umask...) before running a command as you do in your sh script.



    But you don't have to write that script in a file, you can just inline it:



    sh -c 'CDPATH= cd -P -- "$1" && shift && exec "$@"' sh /some/dir cmd args


    (assuming the directory is not -). Adding CDPATH= (in case there's one in the environment) and -P for it to behave more like a straight chdir().



    Alternatively, you could use perl whose chdir() does a straight chdir() out of the box.



    perl -e 'chdir(shift@ARGV) or die "chdir: $!"; exec @ARGV or die "exec: $!"
    ' /some/dir cmd args





    share|improve this answer














    AFAIK, there is no such dedicated utility in the POSIX tool chest. But it's common to invoke sh to set up an environment (cwd, limits, stdout/in/err, umask...) before running a command as you do in your sh script.



    But you don't have to write that script in a file, you can just inline it:



    sh -c 'CDPATH= cd -P -- "$1" && shift && exec "$@"' sh /some/dir cmd args


    (assuming the directory is not -). Adding CDPATH= (in case there's one in the environment) and -P for it to behave more like a straight chdir().



    Alternatively, you could use perl whose chdir() does a straight chdir() out of the box.



    perl -e 'chdir(shift@ARGV) or die "chdir: $!"; exec @ARGV or die "exec: $!"
    ' /some/dir cmd args






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    Stéphane Chazelas

    292k54546886




    292k54546886











    • Oh, yeah. This is indded a very handy variation of the script I included in my question. I was afraid that the utility I am looking for does not exist since its functionality is already available with a relatively short sh(1) oneliner.
      – Mateusz Piotrowski
      yesterday






    • 7




      If you're already running sh, you can also do (cd /wherever && exec /my/command). The () implicitly opens a subshell to run the wrapped commands, and of course, exec gets rid of the extra shell process as quickly as /my/command starts running.
      – jpaugh
      yesterday







    • 1




      That was suggested in a now-deleted answer. The questioner explained that that was not an answer to what xe asked.
      – JdeBP
      23 hours ago
















    • Oh, yeah. This is indded a very handy variation of the script I included in my question. I was afraid that the utility I am looking for does not exist since its functionality is already available with a relatively short sh(1) oneliner.
      – Mateusz Piotrowski
      yesterday






    • 7




      If you're already running sh, you can also do (cd /wherever && exec /my/command). The () implicitly opens a subshell to run the wrapped commands, and of course, exec gets rid of the extra shell process as quickly as /my/command starts running.
      – jpaugh
      yesterday







    • 1




      That was suggested in a now-deleted answer. The questioner explained that that was not an answer to what xe asked.
      – JdeBP
      23 hours ago















    Oh, yeah. This is indded a very handy variation of the script I included in my question. I was afraid that the utility I am looking for does not exist since its functionality is already available with a relatively short sh(1) oneliner.
    – Mateusz Piotrowski
    yesterday




    Oh, yeah. This is indded a very handy variation of the script I included in my question. I was afraid that the utility I am looking for does not exist since its functionality is already available with a relatively short sh(1) oneliner.
    – Mateusz Piotrowski
    yesterday




    7




    7




    If you're already running sh, you can also do (cd /wherever && exec /my/command). The () implicitly opens a subshell to run the wrapped commands, and of course, exec gets rid of the extra shell process as quickly as /my/command starts running.
    – jpaugh
    yesterday





    If you're already running sh, you can also do (cd /wherever && exec /my/command). The () implicitly opens a subshell to run the wrapped commands, and of course, exec gets rid of the extra shell process as quickly as /my/command starts running.
    – jpaugh
    yesterday





    1




    1




    That was suggested in a now-deleted answer. The questioner explained that that was not an answer to what xe asked.
    – JdeBP
    23 hours ago




    That was suggested in a now-deleted answer. The questioner explained that that was not an answer to what xe asked.
    – JdeBP
    23 hours ago












    up vote
    15
    down vote













    The toolsets used in the daemontools world, and elsewhere, have this and more besides; have had for many years; and are widely available.



    • Wayne Marshall's perp has runtool:
      runtool -c /new/cwd myProgram


    • Laurent Bercot's execline has cd:
      cd /new/cwd myProgram



    • my nosh toolset has chdir:
      chdir /new/cwd myProgram


    All of these are chain-loading tools, designed to be used in exactly these sorts of chains. There is a wide selection of chain-loading tools in these toolkits for other purposes.



    Further reading



    • https://unix.stackexchange.com/a/353698/5132

    • Laurent Bercot (2018-08-01). "Reference". execline. skarnet.org.

    • Laurent Bercot (2018-11-08). "Reference". s6. skarnet.org.

    • Jonathan de Boyne Pollard (2018). "Command and tool list". nosh Guide. Softwares.





    share|improve this answer
























      up vote
      15
      down vote













      The toolsets used in the daemontools world, and elsewhere, have this and more besides; have had for many years; and are widely available.



      • Wayne Marshall's perp has runtool:
        runtool -c /new/cwd myProgram


      • Laurent Bercot's execline has cd:
        cd /new/cwd myProgram



      • my nosh toolset has chdir:
        chdir /new/cwd myProgram


      All of these are chain-loading tools, designed to be used in exactly these sorts of chains. There is a wide selection of chain-loading tools in these toolkits for other purposes.



      Further reading



      • https://unix.stackexchange.com/a/353698/5132

      • Laurent Bercot (2018-08-01). "Reference". execline. skarnet.org.

      • Laurent Bercot (2018-11-08). "Reference". s6. skarnet.org.

      • Jonathan de Boyne Pollard (2018). "Command and tool list". nosh Guide. Softwares.





      share|improve this answer






















        up vote
        15
        down vote










        up vote
        15
        down vote









        The toolsets used in the daemontools world, and elsewhere, have this and more besides; have had for many years; and are widely available.



        • Wayne Marshall's perp has runtool:
          runtool -c /new/cwd myProgram


        • Laurent Bercot's execline has cd:
          cd /new/cwd myProgram



        • my nosh toolset has chdir:
          chdir /new/cwd myProgram


        All of these are chain-loading tools, designed to be used in exactly these sorts of chains. There is a wide selection of chain-loading tools in these toolkits for other purposes.



        Further reading



        • https://unix.stackexchange.com/a/353698/5132

        • Laurent Bercot (2018-08-01). "Reference". execline. skarnet.org.

        • Laurent Bercot (2018-11-08). "Reference". s6. skarnet.org.

        • Jonathan de Boyne Pollard (2018). "Command and tool list". nosh Guide. Softwares.





        share|improve this answer












        The toolsets used in the daemontools world, and elsewhere, have this and more besides; have had for many years; and are widely available.



        • Wayne Marshall's perp has runtool:
          runtool -c /new/cwd myProgram


        • Laurent Bercot's execline has cd:
          cd /new/cwd myProgram



        • my nosh toolset has chdir:
          chdir /new/cwd myProgram


        All of these are chain-loading tools, designed to be used in exactly these sorts of chains. There is a wide selection of chain-loading tools in these toolkits for other purposes.



        Further reading



        • https://unix.stackexchange.com/a/353698/5132

        • Laurent Bercot (2018-08-01). "Reference". execline. skarnet.org.

        • Laurent Bercot (2018-11-08). "Reference". s6. skarnet.org.

        • Jonathan de Boyne Pollard (2018). "Command and tool list". nosh Guide. Softwares.






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        JdeBP

        31.3k466144




        31.3k466144




















            up vote
            1
            down vote













            Certain programs have an option for this, like Git:




            -C <path>



            Run as if git was started in <path> instead of the current working
            directory.




            and Make:




            -C dir, --directory=dir



            Change to directory dir before reading the makefiles or doing anything else.




            and Tar:




            -C, --directory=DIR



            Change to DIR before performing any operations. This option is
            order-sensitive, i.e. it affects all options that follow.







            share|improve this answer






















            • Yes. Although my question is what if myProgram does not provide such an option... Thank you for your contribution but I'm afraid it does not answer my question at all.
              – Mateusz Piotrowski
              3 hours ago















            up vote
            1
            down vote













            Certain programs have an option for this, like Git:




            -C <path>



            Run as if git was started in <path> instead of the current working
            directory.




            and Make:




            -C dir, --directory=dir



            Change to directory dir before reading the makefiles or doing anything else.




            and Tar:




            -C, --directory=DIR



            Change to DIR before performing any operations. This option is
            order-sensitive, i.e. it affects all options that follow.







            share|improve this answer






















            • Yes. Although my question is what if myProgram does not provide such an option... Thank you for your contribution but I'm afraid it does not answer my question at all.
              – Mateusz Piotrowski
              3 hours ago













            up vote
            1
            down vote










            up vote
            1
            down vote









            Certain programs have an option for this, like Git:




            -C <path>



            Run as if git was started in <path> instead of the current working
            directory.




            and Make:




            -C dir, --directory=dir



            Change to directory dir before reading the makefiles or doing anything else.




            and Tar:




            -C, --directory=DIR



            Change to DIR before performing any operations. This option is
            order-sensitive, i.e. it affects all options that follow.







            share|improve this answer














            Certain programs have an option for this, like Git:




            -C <path>



            Run as if git was started in <path> instead of the current working
            directory.




            and Make:




            -C dir, --directory=dir



            Change to directory dir before reading the makefiles or doing anything else.




            and Tar:




            -C, --directory=DIR



            Change to DIR before performing any operations. This option is
            order-sensitive, i.e. it affects all options that follow.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 23 hours ago

























            answered yesterday









            Steven Penny

            2,45121637




            2,45121637











            • Yes. Although my question is what if myProgram does not provide such an option... Thank you for your contribution but I'm afraid it does not answer my question at all.
              – Mateusz Piotrowski
              3 hours ago

















            • Yes. Although my question is what if myProgram does not provide such an option... Thank you for your contribution but I'm afraid it does not answer my question at all.
              – Mateusz Piotrowski
              3 hours ago
















            Yes. Although my question is what if myProgram does not provide such an option... Thank you for your contribution but I'm afraid it does not answer my question at all.
            – Mateusz Piotrowski
            3 hours ago





            Yes. Although my question is what if myProgram does not provide such an option... Thank you for your contribution but I'm afraid it does not answer my question at all.
            – Mateusz Piotrowski
            3 hours ago











            up vote
            1
            down vote













            There is such a popular program. It is called ... hold onto your chair... drumroll... env. (The GNU version, since version 8.28, not POSIX).




            NAME
            env - run a program in a modified environment

            SYNOPSIS
            env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

            DESCRIPTION
            Set each NAME to VALUE in the environment and run COMMAND.

            Mandatory arguments to long options are mandatory for short options too.

            -i, --ignore-environment
            start with an empty environment

            -0, --null
            end each output line with NUL, not newline

            -u, --unset=NAME
            remove variable from the environment

            -C, --chdir=DIR
            change working directory to DIR

            --help display this help and exit

            --version
            output version information and exit

            A mere - implies -i. If no COMMAND, print the resulting environment.






            share|improve this answer






















            • why is this being downvoted?
              – Adrian
              1 hour ago














            up vote
            1
            down vote













            There is such a popular program. It is called ... hold onto your chair... drumroll... env. (The GNU version, since version 8.28, not POSIX).




            NAME
            env - run a program in a modified environment

            SYNOPSIS
            env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

            DESCRIPTION
            Set each NAME to VALUE in the environment and run COMMAND.

            Mandatory arguments to long options are mandatory for short options too.

            -i, --ignore-environment
            start with an empty environment

            -0, --null
            end each output line with NUL, not newline

            -u, --unset=NAME
            remove variable from the environment

            -C, --chdir=DIR
            change working directory to DIR

            --help display this help and exit

            --version
            output version information and exit

            A mere - implies -i. If no COMMAND, print the resulting environment.






            share|improve this answer






















            • why is this being downvoted?
              – Adrian
              1 hour ago












            up vote
            1
            down vote










            up vote
            1
            down vote









            There is such a popular program. It is called ... hold onto your chair... drumroll... env. (The GNU version, since version 8.28, not POSIX).




            NAME
            env - run a program in a modified environment

            SYNOPSIS
            env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

            DESCRIPTION
            Set each NAME to VALUE in the environment and run COMMAND.

            Mandatory arguments to long options are mandatory for short options too.

            -i, --ignore-environment
            start with an empty environment

            -0, --null
            end each output line with NUL, not newline

            -u, --unset=NAME
            remove variable from the environment

            -C, --chdir=DIR
            change working directory to DIR

            --help display this help and exit

            --version
            output version information and exit

            A mere - implies -i. If no COMMAND, print the resulting environment.






            share|improve this answer














            There is such a popular program. It is called ... hold onto your chair... drumroll... env. (The GNU version, since version 8.28, not POSIX).




            NAME
            env - run a program in a modified environment

            SYNOPSIS
            env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

            DESCRIPTION
            Set each NAME to VALUE in the environment and run COMMAND.

            Mandatory arguments to long options are mandatory for short options too.

            -i, --ignore-environment
            start with an empty environment

            -0, --null
            end each output line with NUL, not newline

            -u, --unset=NAME
            remove variable from the environment

            -C, --chdir=DIR
            change working directory to DIR

            --help display this help and exit

            --version
            output version information and exit

            A mere - implies -i. If no COMMAND, print the resulting environment.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago









            Stéphane Chazelas

            292k54546886




            292k54546886










            answered 2 hours ago









            n.m.

            26215




            26215











            • why is this being downvoted?
              – Adrian
              1 hour ago
















            • why is this being downvoted?
              – Adrian
              1 hour ago















            why is this being downvoted?
            – Adrian
            1 hour ago




            why is this being downvoted?
            – Adrian
            1 hour ago

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480776%2fis-there-a-posix-or-at-least-a-popular-utility-to-set-the-current-working-dire%23new-answer', 'question_page');

            );

            Post as a guest














































































            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto