`mktemp -d` followed by `pushd` works fine on command line, but not when in a script

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











up vote
1
down vote

favorite












If I paste these lines into a command prompt on Debian...



DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
echo "tmpdir = $DIR"
cd "$DIR"


They make a new temp directory, print the directory name, and then pushd into that directory...



root@beaglebone:/tmp/bbbrtc.2mw02x# DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
root@beaglebone:/tmp/bbbrtc.2mw02x# echo "tmpdir = $DIR"
tmpdir = /tmp/bbbrtc.Grti6K
root@beaglebone:/tmp/bbbrtc.2mw02x# pushd "$DIR"
/tmp/bbbrtc.Grti6K /tmp/bbbrtc.2mw02x ~/bbbphyfix
root@beaglebone:/tmp/bbbrtc.Grti6K#


... as expected.



If I run the exact same commands from inside a shell script...



root@beaglebone:/tmp/bbbrtc.2mw02x# cat test.sh
#!/bin/sh

DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
echo "tmpdir = $DIR"
pushd "$DIR"

root@beaglebone:/tmp/bbbrtc.2mw02x# ./test.sh
tmpdir = /tmp/bbbrtc.O6yYgf
./test.sh: 5: ./test.sh: pushd: not found
root@beaglebone:/tmp/bbbrtc.2mw02x#


...it generates the "pushd: not found" message.



Why do these commands not work from inside a shell script, and what it the proper way to have a script create a temp dir and then pushd into that new dir?







share|improve this question















  • 5




    Your /bin/sh is not the same shell as your interactive shell.
    – Michael Homer
    May 28 at 3:24






  • 3




    Change your shebang to #!/bin/bash for example. pushd is not available in POSIX shell.
    – cuonglm
    May 28 at 3:25










  • This is it! Thank you!
    – bigjosh
    May 28 at 3:33










  • @cuonglm Post as answer and I will gratefully accept it! Thanks!
    – bigjosh
    May 28 at 3:34














up vote
1
down vote

favorite












If I paste these lines into a command prompt on Debian...



DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
echo "tmpdir = $DIR"
cd "$DIR"


They make a new temp directory, print the directory name, and then pushd into that directory...



root@beaglebone:/tmp/bbbrtc.2mw02x# DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
root@beaglebone:/tmp/bbbrtc.2mw02x# echo "tmpdir = $DIR"
tmpdir = /tmp/bbbrtc.Grti6K
root@beaglebone:/tmp/bbbrtc.2mw02x# pushd "$DIR"
/tmp/bbbrtc.Grti6K /tmp/bbbrtc.2mw02x ~/bbbphyfix
root@beaglebone:/tmp/bbbrtc.Grti6K#


... as expected.



If I run the exact same commands from inside a shell script...



root@beaglebone:/tmp/bbbrtc.2mw02x# cat test.sh
#!/bin/sh

DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
echo "tmpdir = $DIR"
pushd "$DIR"

root@beaglebone:/tmp/bbbrtc.2mw02x# ./test.sh
tmpdir = /tmp/bbbrtc.O6yYgf
./test.sh: 5: ./test.sh: pushd: not found
root@beaglebone:/tmp/bbbrtc.2mw02x#


...it generates the "pushd: not found" message.



Why do these commands not work from inside a shell script, and what it the proper way to have a script create a temp dir and then pushd into that new dir?







share|improve this question















  • 5




    Your /bin/sh is not the same shell as your interactive shell.
    – Michael Homer
    May 28 at 3:24






  • 3




    Change your shebang to #!/bin/bash for example. pushd is not available in POSIX shell.
    – cuonglm
    May 28 at 3:25










  • This is it! Thank you!
    – bigjosh
    May 28 at 3:33










  • @cuonglm Post as answer and I will gratefully accept it! Thanks!
    – bigjosh
    May 28 at 3:34












up vote
1
down vote

favorite









up vote
1
down vote

favorite











If I paste these lines into a command prompt on Debian...



DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
echo "tmpdir = $DIR"
cd "$DIR"


They make a new temp directory, print the directory name, and then pushd into that directory...



root@beaglebone:/tmp/bbbrtc.2mw02x# DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
root@beaglebone:/tmp/bbbrtc.2mw02x# echo "tmpdir = $DIR"
tmpdir = /tmp/bbbrtc.Grti6K
root@beaglebone:/tmp/bbbrtc.2mw02x# pushd "$DIR"
/tmp/bbbrtc.Grti6K /tmp/bbbrtc.2mw02x ~/bbbphyfix
root@beaglebone:/tmp/bbbrtc.Grti6K#


... as expected.



If I run the exact same commands from inside a shell script...



root@beaglebone:/tmp/bbbrtc.2mw02x# cat test.sh
#!/bin/sh

DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
echo "tmpdir = $DIR"
pushd "$DIR"

root@beaglebone:/tmp/bbbrtc.2mw02x# ./test.sh
tmpdir = /tmp/bbbrtc.O6yYgf
./test.sh: 5: ./test.sh: pushd: not found
root@beaglebone:/tmp/bbbrtc.2mw02x#


...it generates the "pushd: not found" message.



Why do these commands not work from inside a shell script, and what it the proper way to have a script create a temp dir and then pushd into that new dir?







share|improve this question











If I paste these lines into a command prompt on Debian...



DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
echo "tmpdir = $DIR"
cd "$DIR"


They make a new temp directory, print the directory name, and then pushd into that directory...



root@beaglebone:/tmp/bbbrtc.2mw02x# DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
root@beaglebone:/tmp/bbbrtc.2mw02x# echo "tmpdir = $DIR"
tmpdir = /tmp/bbbrtc.Grti6K
root@beaglebone:/tmp/bbbrtc.2mw02x# pushd "$DIR"
/tmp/bbbrtc.Grti6K /tmp/bbbrtc.2mw02x ~/bbbphyfix
root@beaglebone:/tmp/bbbrtc.Grti6K#


... as expected.



If I run the exact same commands from inside a shell script...



root@beaglebone:/tmp/bbbrtc.2mw02x# cat test.sh
#!/bin/sh

DIR=$(mktemp -d -t bbbrtc.XXXXXX) || exit 1
echo "tmpdir = $DIR"
pushd "$DIR"

root@beaglebone:/tmp/bbbrtc.2mw02x# ./test.sh
tmpdir = /tmp/bbbrtc.O6yYgf
./test.sh: 5: ./test.sh: pushd: not found
root@beaglebone:/tmp/bbbrtc.2mw02x#


...it generates the "pushd: not found" message.



Why do these commands not work from inside a shell script, and what it the proper way to have a script create a temp dir and then pushd into that new dir?









share|improve this question










share|improve this question




share|improve this question









asked May 28 at 3:19









bigjosh

24939




24939







  • 5




    Your /bin/sh is not the same shell as your interactive shell.
    – Michael Homer
    May 28 at 3:24






  • 3




    Change your shebang to #!/bin/bash for example. pushd is not available in POSIX shell.
    – cuonglm
    May 28 at 3:25










  • This is it! Thank you!
    – bigjosh
    May 28 at 3:33










  • @cuonglm Post as answer and I will gratefully accept it! Thanks!
    – bigjosh
    May 28 at 3:34












  • 5




    Your /bin/sh is not the same shell as your interactive shell.
    – Michael Homer
    May 28 at 3:24






  • 3




    Change your shebang to #!/bin/bash for example. pushd is not available in POSIX shell.
    – cuonglm
    May 28 at 3:25










  • This is it! Thank you!
    – bigjosh
    May 28 at 3:33










  • @cuonglm Post as answer and I will gratefully accept it! Thanks!
    – bigjosh
    May 28 at 3:34







5




5




Your /bin/sh is not the same shell as your interactive shell.
– Michael Homer
May 28 at 3:24




Your /bin/sh is not the same shell as your interactive shell.
– Michael Homer
May 28 at 3:24




3




3




Change your shebang to #!/bin/bash for example. pushd is not available in POSIX shell.
– cuonglm
May 28 at 3:25




Change your shebang to #!/bin/bash for example. pushd is not available in POSIX shell.
– cuonglm
May 28 at 3:25












This is it! Thank you!
– bigjosh
May 28 at 3:33




This is it! Thank you!
– bigjosh
May 28 at 3:33












@cuonglm Post as answer and I will gratefully accept it! Thanks!
– bigjosh
May 28 at 3:34




@cuonglm Post as answer and I will gratefully accept it! Thanks!
– bigjosh
May 28 at 3:34










1 Answer
1






active

oldest

votes

















up vote
2
down vote













pushd is a bash command, which is generally not implemented by /bin/sh. To use pushd in a sh script, you would have to provide a script or function with the same functionality.



The idiomatic way of temporarily changing one's working directory for the course of a few commands in a sh script is to do



( cd directory && somecommand )


This would change into directory and execute somecommand if that succeeded. The whole thing is done in a subshell, so the cd will not have any effect on the rest of the script.



Alternatively,



( cd directory || exit 1
command1
command2
command3 )





share|improve this answer





















    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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f446386%2fmktemp-d-followed-by-pushd-works-fine-on-command-line-but-not-when-in-a-sc%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    pushd is a bash command, which is generally not implemented by /bin/sh. To use pushd in a sh script, you would have to provide a script or function with the same functionality.



    The idiomatic way of temporarily changing one's working directory for the course of a few commands in a sh script is to do



    ( cd directory && somecommand )


    This would change into directory and execute somecommand if that succeeded. The whole thing is done in a subshell, so the cd will not have any effect on the rest of the script.



    Alternatively,



    ( cd directory || exit 1
    command1
    command2
    command3 )





    share|improve this answer

























      up vote
      2
      down vote













      pushd is a bash command, which is generally not implemented by /bin/sh. To use pushd in a sh script, you would have to provide a script or function with the same functionality.



      The idiomatic way of temporarily changing one's working directory for the course of a few commands in a sh script is to do



      ( cd directory && somecommand )


      This would change into directory and execute somecommand if that succeeded. The whole thing is done in a subshell, so the cd will not have any effect on the rest of the script.



      Alternatively,



      ( cd directory || exit 1
      command1
      command2
      command3 )





      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        pushd is a bash command, which is generally not implemented by /bin/sh. To use pushd in a sh script, you would have to provide a script or function with the same functionality.



        The idiomatic way of temporarily changing one's working directory for the course of a few commands in a sh script is to do



        ( cd directory && somecommand )


        This would change into directory and execute somecommand if that succeeded. The whole thing is done in a subshell, so the cd will not have any effect on the rest of the script.



        Alternatively,



        ( cd directory || exit 1
        command1
        command2
        command3 )





        share|improve this answer













        pushd is a bash command, which is generally not implemented by /bin/sh. To use pushd in a sh script, you would have to provide a script or function with the same functionality.



        The idiomatic way of temporarily changing one's working directory for the course of a few commands in a sh script is to do



        ( cd directory && somecommand )


        This would change into directory and execute somecommand if that succeeded. The whole thing is done in a subshell, so the cd will not have any effect on the rest of the script.



        Alternatively,



        ( cd directory || exit 1
        command1
        command2
        command3 )






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 28 at 6:43









        Kusalananda

        102k13199314




        102k13199314






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f446386%2fmktemp-d-followed-by-pushd-works-fine-on-command-line-but-not-when-in-a-sc%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?