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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












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



























    up vote
    4
    down vote

    favorite












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

























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      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 do 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 do 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 43 mins ago

























      asked 1 hour ago









      Mateusz Piotrowski

      1,78421537




      1,78421537




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote













          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
            1 hour ago

















          up vote
          1
          down vote













          You could use a subshell:



          $ pwd
          /home/user
          $ (cd /bin && pwd)
          /bin
          $ pwd
          /home/user


          A possible downside of this approach is that the values of the variables are not preserved in the parent shell, but it may be useful in certain cases:



          $ var=foo; echo "$var"
          foo
          $ (var=bar; echo "$var")
          bar
          $ echo "$var"
          foo





          share|improve this answer




















          • A subshell is not a solution. It is not a utility and unfortunately it cannot be chained the way you can chain env(1)-like variables.
            – Mateusz Piotrowski
            42 mins ago










          • @MateuszPiotrowski Would this approach be valid if you put the entire chain of commands within the subshell? If I understand correctly, it will be basically the same as having a separate script or explicitly calling another interpreter, at least in a shell context.
            – nxnev
            32 mins ago










          • In a way, yes. In order to use a subshell we have to be in a shell of some sort. In case you'd like to feed this command to execve you'd have to wrap your subshell in sh -c anyway.
            – Mateusz Piotrowski
            21 mins ago


















          up vote
          0
          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




















            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






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            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
              1 hour ago














            up vote
            4
            down vote













            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
              1 hour ago












            up vote
            4
            down vote










            up vote
            4
            down vote









            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 1 hour ago

























            answered 1 hour ago









            Stéphane Chazelas

            292k54544884




            292k54544884











            • 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
              1 hour 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
              1 hour 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
            1 hour 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
            1 hour ago












            up vote
            1
            down vote













            You could use a subshell:



            $ pwd
            /home/user
            $ (cd /bin && pwd)
            /bin
            $ pwd
            /home/user


            A possible downside of this approach is that the values of the variables are not preserved in the parent shell, but it may be useful in certain cases:



            $ var=foo; echo "$var"
            foo
            $ (var=bar; echo "$var")
            bar
            $ echo "$var"
            foo





            share|improve this answer




















            • A subshell is not a solution. It is not a utility and unfortunately it cannot be chained the way you can chain env(1)-like variables.
              – Mateusz Piotrowski
              42 mins ago










            • @MateuszPiotrowski Would this approach be valid if you put the entire chain of commands within the subshell? If I understand correctly, it will be basically the same as having a separate script or explicitly calling another interpreter, at least in a shell context.
              – nxnev
              32 mins ago










            • In a way, yes. In order to use a subshell we have to be in a shell of some sort. In case you'd like to feed this command to execve you'd have to wrap your subshell in sh -c anyway.
              – Mateusz Piotrowski
              21 mins ago















            up vote
            1
            down vote













            You could use a subshell:



            $ pwd
            /home/user
            $ (cd /bin && pwd)
            /bin
            $ pwd
            /home/user


            A possible downside of this approach is that the values of the variables are not preserved in the parent shell, but it may be useful in certain cases:



            $ var=foo; echo "$var"
            foo
            $ (var=bar; echo "$var")
            bar
            $ echo "$var"
            foo





            share|improve this answer




















            • A subshell is not a solution. It is not a utility and unfortunately it cannot be chained the way you can chain env(1)-like variables.
              – Mateusz Piotrowski
              42 mins ago










            • @MateuszPiotrowski Would this approach be valid if you put the entire chain of commands within the subshell? If I understand correctly, it will be basically the same as having a separate script or explicitly calling another interpreter, at least in a shell context.
              – nxnev
              32 mins ago










            • In a way, yes. In order to use a subshell we have to be in a shell of some sort. In case you'd like to feed this command to execve you'd have to wrap your subshell in sh -c anyway.
              – Mateusz Piotrowski
              21 mins ago













            up vote
            1
            down vote










            up vote
            1
            down vote









            You could use a subshell:



            $ pwd
            /home/user
            $ (cd /bin && pwd)
            /bin
            $ pwd
            /home/user


            A possible downside of this approach is that the values of the variables are not preserved in the parent shell, but it may be useful in certain cases:



            $ var=foo; echo "$var"
            foo
            $ (var=bar; echo "$var")
            bar
            $ echo "$var"
            foo





            share|improve this answer












            You could use a subshell:



            $ pwd
            /home/user
            $ (cd /bin && pwd)
            /bin
            $ pwd
            /home/user


            A possible downside of this approach is that the values of the variables are not preserved in the parent shell, but it may be useful in certain cases:



            $ var=foo; echo "$var"
            foo
            $ (var=bar; echo "$var")
            bar
            $ echo "$var"
            foo






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 53 mins ago









            nxnev

            2,5482423




            2,5482423











            • A subshell is not a solution. It is not a utility and unfortunately it cannot be chained the way you can chain env(1)-like variables.
              – Mateusz Piotrowski
              42 mins ago










            • @MateuszPiotrowski Would this approach be valid if you put the entire chain of commands within the subshell? If I understand correctly, it will be basically the same as having a separate script or explicitly calling another interpreter, at least in a shell context.
              – nxnev
              32 mins ago










            • In a way, yes. In order to use a subshell we have to be in a shell of some sort. In case you'd like to feed this command to execve you'd have to wrap your subshell in sh -c anyway.
              – Mateusz Piotrowski
              21 mins ago

















            • A subshell is not a solution. It is not a utility and unfortunately it cannot be chained the way you can chain env(1)-like variables.
              – Mateusz Piotrowski
              42 mins ago










            • @MateuszPiotrowski Would this approach be valid if you put the entire chain of commands within the subshell? If I understand correctly, it will be basically the same as having a separate script or explicitly calling another interpreter, at least in a shell context.
              – nxnev
              32 mins ago










            • In a way, yes. In order to use a subshell we have to be in a shell of some sort. In case you'd like to feed this command to execve you'd have to wrap your subshell in sh -c anyway.
              – Mateusz Piotrowski
              21 mins ago
















            A subshell is not a solution. It is not a utility and unfortunately it cannot be chained the way you can chain env(1)-like variables.
            – Mateusz Piotrowski
            42 mins ago




            A subshell is not a solution. It is not a utility and unfortunately it cannot be chained the way you can chain env(1)-like variables.
            – Mateusz Piotrowski
            42 mins ago












            @MateuszPiotrowski Would this approach be valid if you put the entire chain of commands within the subshell? If I understand correctly, it will be basically the same as having a separate script or explicitly calling another interpreter, at least in a shell context.
            – nxnev
            32 mins ago




            @MateuszPiotrowski Would this approach be valid if you put the entire chain of commands within the subshell? If I understand correctly, it will be basically the same as having a separate script or explicitly calling another interpreter, at least in a shell context.
            – nxnev
            32 mins ago












            In a way, yes. In order to use a subshell we have to be in a shell of some sort. In case you'd like to feed this command to execve you'd have to wrap your subshell in sh -c anyway.
            – Mateusz Piotrowski
            21 mins ago





            In a way, yes. In order to use a subshell we have to be in a shell of some sort. In case you'd like to feed this command to execve you'd have to wrap your subshell in sh -c anyway.
            – Mateusz Piotrowski
            21 mins ago











            up vote
            0
            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
























              up vote
              0
              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






















                up vote
                0
                down vote










                up vote
                0
                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












                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











                share


                share










                answered 24 secs ago









                JdeBP

                31.1k465142




                31.1k465142



























                     

                    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













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery