Invoking pushd in a shell scriptDifferent ways to execute a shell scriptchanging current working dir with a scriptTiming out in a shell scriptshell commands to check & create dirRule for invoking subshell in Bash?Interactive shell in background inside another shell scriptHow to run privileged bash script as non-root?Source shell script automatically in terminalHow can I search and execute a shell script from another?self killing shell scriptRun shell script at startup (Kali Linux)Importing environment variables from another script into the current shell

Inorganic chemistry handbook with reaction lists

Generating a list with duplicate entries

Is it a Cyclops number? "Nobody" knows!

Should I apply for my boss's promotion?

Unfamiliar notation in Diabelli's "Duet in D" for piano

I am the person who abides by rules but breaks the rules . Who am I

Should I file my taxes? No income, unemployed, but paid 2k in student loan interest

What exactly is the meaning of "fine wine"?

Averaging over columns while ignoring zero entries

Are small insurances worth it?

Is there a logarithm base for which the logarithm becomes an identity function?

Too soon for a plot twist?

What does "rhumatis" mean?

Is it appropriate to ask a former professor to order a library book for me through ILL?

What is Tony Stark injecting into himself in Iron Man 3?

Who has more? Ireland or Iceland?

How would an energy-based "projectile" blow up a spaceship?

Use Mercury as quenching liquid for swords?

Why does this boat have a landing pad? (SpaceX's GO Searcher) Any plans for propulsive capsule landings?

Rationale to prefer local variables over instance variables?

ESPP--any reason not to go all in?

What do you call someone who likes to pick fights?

What does *dead* mean in *What do you mean, dead?*?

Paper published similar to PhD thesis



Invoking pushd in a shell script


Different ways to execute a shell scriptchanging current working dir with a scriptTiming out in a shell scriptshell commands to check & create dirRule for invoking subshell in Bash?Interactive shell in background inside another shell scriptHow to run privileged bash script as non-root?Source shell script automatically in terminalHow can I search and execute a shell script from another?self killing shell scriptRun shell script at startup (Kali Linux)Importing environment variables from another script into the current shell













6















I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.



What's a way to make this execute in the shell terminal?



#!/bin/sh

pushd $1
time


What I really want to accomplish is invoke pushd followed by other-command with one command.










share|improve this question
























  • Related question: unix.stackexchange.com/q/2976/250

    – phunehehe
    Dec 29 '10 at 0:49















6















I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.



What's a way to make this execute in the shell terminal?



#!/bin/sh

pushd $1
time


What I really want to accomplish is invoke pushd followed by other-command with one command.










share|improve this question
























  • Related question: unix.stackexchange.com/q/2976/250

    – phunehehe
    Dec 29 '10 at 0:49













6












6








6








I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.



What's a way to make this execute in the shell terminal?



#!/bin/sh

pushd $1
time


What I really want to accomplish is invoke pushd followed by other-command with one command.










share|improve this question
















I have a simple script that I want to invoke 'pushed' followed by another command. But the 'pushd' command inside the script doesn't seem to stick past the script.



What's a way to make this execute in the shell terminal?



#!/bin/sh

pushd $1
time


What I really want to accomplish is invoke pushd followed by other-command with one command.







command-line shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '10 at 0:40

























asked Dec 28 '10 at 22:28







Sarah Soto



















  • Related question: unix.stackexchange.com/q/2976/250

    – phunehehe
    Dec 29 '10 at 0:49

















  • Related question: unix.stackexchange.com/q/2976/250

    – phunehehe
    Dec 29 '10 at 0:49
















Related question: unix.stackexchange.com/q/2976/250

– phunehehe
Dec 29 '10 at 0:49





Related question: unix.stackexchange.com/q/2976/250

– phunehehe
Dec 29 '10 at 0:49










3 Answers
3






active

oldest

votes


















8














A shell script normally executes in a separate instance of the shell program, /bin/sh in this case. Your pushd command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.



To execute that script within the current shell, say this instead:



$ . my-command somedir


or, more verbosely:



$ source my-command somedir


To make it appear that your program works like any other, you can use an alias:



$ alias mycmd='source my-command'
$ mycmd /bin
$ pwd
/bin





share|improve this answer























  • What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?

    – Sarah Soto
    Dec 29 '10 at 0:23











  • @sarah-soto All you need then is pushd <params>; other-command

    – Shawn J. Goff
    Dec 29 '10 at 0:26











  • I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory

    – Sarah Soto
    Dec 29 '10 at 0:38











  • You could use a shell function: function p2 () pushd "$@"; other-command;

    – Steven D
    Dec 29 '10 at 0:46







  • 2





    A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your ~/.bash_profile (or whatever your shell uses, if not Bash) will also work. Your choice.

    – Warren Young
    Dec 29 '10 at 3:10


















2














Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.



To run the script in the same process, you can 'source' the script like this



. /path/to/script.sh args





share|improve this answer
































    0














    Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.



    Add this in .bashrc:



    foo() 
    pushd $1
    time

    export foo


    In shell:



    $ foo mydir1


    output:



    ~/mydir1 ~

    real 0m0.000s
    user 0m0.000s
    sys 0m0.000s





    share|improve this answer








    New contributor




    otter.pro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.



















      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',
      autoActivateHeartbeat: false,
      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%2f5204%2finvoking-pushd-in-a-shell-script%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown
























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      8














      A shell script normally executes in a separate instance of the shell program, /bin/sh in this case. Your pushd command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.



      To execute that script within the current shell, say this instead:



      $ . my-command somedir


      or, more verbosely:



      $ source my-command somedir


      To make it appear that your program works like any other, you can use an alias:



      $ alias mycmd='source my-command'
      $ mycmd /bin
      $ pwd
      /bin





      share|improve this answer























      • What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?

        – Sarah Soto
        Dec 29 '10 at 0:23











      • @sarah-soto All you need then is pushd <params>; other-command

        – Shawn J. Goff
        Dec 29 '10 at 0:26











      • I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory

        – Sarah Soto
        Dec 29 '10 at 0:38











      • You could use a shell function: function p2 () pushd "$@"; other-command;

        – Steven D
        Dec 29 '10 at 0:46







      • 2





        A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your ~/.bash_profile (or whatever your shell uses, if not Bash) will also work. Your choice.

        – Warren Young
        Dec 29 '10 at 3:10















      8














      A shell script normally executes in a separate instance of the shell program, /bin/sh in this case. Your pushd command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.



      To execute that script within the current shell, say this instead:



      $ . my-command somedir


      or, more verbosely:



      $ source my-command somedir


      To make it appear that your program works like any other, you can use an alias:



      $ alias mycmd='source my-command'
      $ mycmd /bin
      $ pwd
      /bin





      share|improve this answer























      • What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?

        – Sarah Soto
        Dec 29 '10 at 0:23











      • @sarah-soto All you need then is pushd <params>; other-command

        – Shawn J. Goff
        Dec 29 '10 at 0:26











      • I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory

        – Sarah Soto
        Dec 29 '10 at 0:38











      • You could use a shell function: function p2 () pushd "$@"; other-command;

        – Steven D
        Dec 29 '10 at 0:46







      • 2





        A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your ~/.bash_profile (or whatever your shell uses, if not Bash) will also work. Your choice.

        – Warren Young
        Dec 29 '10 at 3:10













      8












      8








      8







      A shell script normally executes in a separate instance of the shell program, /bin/sh in this case. Your pushd command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.



      To execute that script within the current shell, say this instead:



      $ . my-command somedir


      or, more verbosely:



      $ source my-command somedir


      To make it appear that your program works like any other, you can use an alias:



      $ alias mycmd='source my-command'
      $ mycmd /bin
      $ pwd
      /bin





      share|improve this answer













      A shell script normally executes in a separate instance of the shell program, /bin/sh in this case. Your pushd command affects that sub-shell's working directory only. If it were otherwise, any program you ran from the shell could mess with your shell's working directory.



      To execute that script within the current shell, say this instead:



      $ . my-command somedir


      or, more verbosely:



      $ source my-command somedir


      To make it appear that your program works like any other, you can use an alias:



      $ alias mycmd='source my-command'
      $ mycmd /bin
      $ pwd
      /bin






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 28 '10 at 22:54









      Warren YoungWarren Young

      55.7k11143148




      55.7k11143148












      • What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?

        – Sarah Soto
        Dec 29 '10 at 0:23











      • @sarah-soto All you need then is pushd <params>; other-command

        – Shawn J. Goff
        Dec 29 '10 at 0:26











      • I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory

        – Sarah Soto
        Dec 29 '10 at 0:38











      • You could use a shell function: function p2 () pushd "$@"; other-command;

        – Steven D
        Dec 29 '10 at 0:46







      • 2





        A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your ~/.bash_profile (or whatever your shell uses, if not Bash) will also work. Your choice.

        – Warren Young
        Dec 29 '10 at 3:10

















      • What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?

        – Sarah Soto
        Dec 29 '10 at 0:23











      • @sarah-soto All you need then is pushd <params>; other-command

        – Shawn J. Goff
        Dec 29 '10 at 0:26











      • I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory

        – Sarah Soto
        Dec 29 '10 at 0:38











      • You could use a shell function: function p2 () pushd "$@"; other-command;

        – Steven D
        Dec 29 '10 at 0:46







      • 2





        A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your ~/.bash_profile (or whatever your shell uses, if not Bash) will also work. Your choice.

        – Warren Young
        Dec 29 '10 at 3:10
















      What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?

      – Sarah Soto
      Dec 29 '10 at 0:23





      What I really want is execute 'pushd <params>' followed by another command that takes no params. I may not need a shell script for it - what's a good way to do this?

      – Sarah Soto
      Dec 29 '10 at 0:23













      @sarah-soto All you need then is pushd <params>; other-command

      – Shawn J. Goff
      Dec 29 '10 at 0:26





      @sarah-soto All you need then is pushd <params>; other-command

      – Shawn J. Goff
      Dec 29 '10 at 0:26













      I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory

      – Sarah Soto
      Dec 29 '10 at 0:38





      I should have clarified: I want a shortcut so I am invoking both commands one after the after without implicitly calling both. I can't aliase the two commands to be alias p2='pushd; other-command' and then invoke 'p2 ~/temp' as the shell will return the error: '-bash: pushd: no other directory

      – Sarah Soto
      Dec 29 '10 at 0:38













      You could use a shell function: function p2 () pushd "$@"; other-command;

      – Steven D
      Dec 29 '10 at 0:46






      You could use a shell function: function p2 () pushd "$@"; other-command;

      – Steven D
      Dec 29 '10 at 0:46





      2




      2





      A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your ~/.bash_profile (or whatever your shell uses, if not Bash) will also work. Your choice.

      – Warren Young
      Dec 29 '10 at 3:10





      A shell script is a fine way to do this. It's inefficient, but this is not a case where optimization is worth bothering with. Defining a shell function in your ~/.bash_profile (or whatever your shell uses, if not Bash) will also work. Your choice.

      – Warren Young
      Dec 29 '10 at 3:10













      2














      Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.



      To run the script in the same process, you can 'source' the script like this



      . /path/to/script.sh args





      share|improve this answer





























        2














        Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.



        To run the script in the same process, you can 'source' the script like this



        . /path/to/script.sh args





        share|improve this answer



























          2












          2








          2







          Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.



          To run the script in the same process, you can 'source' the script like this



          . /path/to/script.sh args





          share|improve this answer















          Scripts cannot alter their parent processe's environment. Because of this, any environment changes made in the script are lost.



          To run the script in the same process, you can 'source' the script like this



          . /path/to/script.sh args






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 29 '10 at 0:24

























          answered Dec 28 '10 at 22:56









          Shawn J. GoffShawn J. Goff

          30k19112134




          30k19112134





















              0














              Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.



              Add this in .bashrc:



              foo() 
              pushd $1
              time

              export foo


              In shell:



              $ foo mydir1


              output:



              ~/mydir1 ~

              real 0m0.000s
              user 0m0.000s
              sys 0m0.000s





              share|improve this answer








              New contributor




              otter.pro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.
























                0














                Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.



                Add this in .bashrc:



                foo() 
                pushd $1
                time

                export foo


                In shell:



                $ foo mydir1


                output:



                ~/mydir1 ~

                real 0m0.000s
                user 0m0.000s
                sys 0m0.000s





                share|improve this answer








                New contributor




                otter.pro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






















                  0












                  0








                  0







                  Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.



                  Add this in .bashrc:



                  foo() 
                  pushd $1
                  time

                  export foo


                  In shell:



                  $ foo mydir1


                  output:



                  ~/mydir1 ~

                  real 0m0.000s
                  user 0m0.000s
                  sys 0m0.000s





                  share|improve this answer








                  New contributor




                  otter.pro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.










                  Instead of a shell script, function is more appropriate for this kind of operation, especially when using pushd.



                  Add this in .bashrc:



                  foo() 
                  pushd $1
                  time

                  export foo


                  In shell:



                  $ foo mydir1


                  output:



                  ~/mydir1 ~

                  real 0m0.000s
                  user 0m0.000s
                  sys 0m0.000s






                  share|improve this answer








                  New contributor




                  otter.pro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  otter.pro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 5 hours ago









                  otter.prootter.pro

                  11




                  11




                  New contributor




                  otter.pro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  otter.pro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  otter.pro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f5204%2finvoking-pushd-in-a-shell-script%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Frič See also Navigation menuinternal link

                      Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                      fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox