How does pushd work?

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











up vote
0
down vote

favorite












In Ubuntu 16.04 with Bash I had a problem when I didn't have a convenient way to upgrade all my WordPress components (core, translations, theme, plugin) and I used the following code to solve it:



cat <<-EOF > /etc/cron.daily/cron_daily
#!/bin/bash
drt="/var/www/html"
for dir in "$drt/*/"; do
if pushd "$dir"; then
wp plugin update --all --allow-root
wp core update --allow-root
wp language core update --allow-root
wp theme update --all --allow-root
rse
popd
fi
done
EOF


What I'd like to ask is actually comprised of these questions:



  1. How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that dir represents each directory inside /var/www/html?


  2. How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".







share|improve this question






















  • Not clear to me what you are asking. drt is set somewhere (apparently to /var/www/html/), and $drt/* simply expands to everything in that directory. With pushd you try to enter the directory, and if that works, you do some stuff, and with popd you return to your previous directory.
    – pfnuesel
    Mar 14 at 16:56














up vote
0
down vote

favorite












In Ubuntu 16.04 with Bash I had a problem when I didn't have a convenient way to upgrade all my WordPress components (core, translations, theme, plugin) and I used the following code to solve it:



cat <<-EOF > /etc/cron.daily/cron_daily
#!/bin/bash
drt="/var/www/html"
for dir in "$drt/*/"; do
if pushd "$dir"; then
wp plugin update --all --allow-root
wp core update --allow-root
wp language core update --allow-root
wp theme update --all --allow-root
rse
popd
fi
done
EOF


What I'd like to ask is actually comprised of these questions:



  1. How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that dir represents each directory inside /var/www/html?


  2. How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".







share|improve this question






















  • Not clear to me what you are asking. drt is set somewhere (apparently to /var/www/html/), and $drt/* simply expands to everything in that directory. With pushd you try to enter the directory, and if that works, you do some stuff, and with popd you return to your previous directory.
    – pfnuesel
    Mar 14 at 16:56












up vote
0
down vote

favorite









up vote
0
down vote

favorite











In Ubuntu 16.04 with Bash I had a problem when I didn't have a convenient way to upgrade all my WordPress components (core, translations, theme, plugin) and I used the following code to solve it:



cat <<-EOF > /etc/cron.daily/cron_daily
#!/bin/bash
drt="/var/www/html"
for dir in "$drt/*/"; do
if pushd "$dir"; then
wp plugin update --all --allow-root
wp core update --allow-root
wp language core update --allow-root
wp theme update --all --allow-root
rse
popd
fi
done
EOF


What I'd like to ask is actually comprised of these questions:



  1. How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that dir represents each directory inside /var/www/html?


  2. How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".







share|improve this question














In Ubuntu 16.04 with Bash I had a problem when I didn't have a convenient way to upgrade all my WordPress components (core, translations, theme, plugin) and I used the following code to solve it:



cat <<-EOF > /etc/cron.daily/cron_daily
#!/bin/bash
drt="/var/www/html"
for dir in "$drt/*/"; do
if pushd "$dir"; then
wp plugin update --all --allow-root
wp core update --allow-root
wp language core update --allow-root
wp theme update --all --allow-root
rse
popd
fi
done
EOF


What I'd like to ask is actually comprised of these questions:



  1. How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that dir represents each directory inside /var/www/html?


  2. How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".









share|improve this question













share|improve this question




share|improve this question








edited Mar 14 at 17:17

























asked Mar 14 at 16:32









user9303970

123224




123224











  • Not clear to me what you are asking. drt is set somewhere (apparently to /var/www/html/), and $drt/* simply expands to everything in that directory. With pushd you try to enter the directory, and if that works, you do some stuff, and with popd you return to your previous directory.
    – pfnuesel
    Mar 14 at 16:56
















  • Not clear to me what you are asking. drt is set somewhere (apparently to /var/www/html/), and $drt/* simply expands to everything in that directory. With pushd you try to enter the directory, and if that works, you do some stuff, and with popd you return to your previous directory.
    – pfnuesel
    Mar 14 at 16:56















Not clear to me what you are asking. drt is set somewhere (apparently to /var/www/html/), and $drt/* simply expands to everything in that directory. With pushd you try to enter the directory, and if that works, you do some stuff, and with popd you return to your previous directory.
– pfnuesel
Mar 14 at 16:56




Not clear to me what you are asking. drt is set somewhere (apparently to /var/www/html/), and $drt/* simply expands to everything in that directory. With pushd you try to enter the directory, and if that works, you do some stuff, and with popd you return to your previous directory.
– pfnuesel
Mar 14 at 16:56










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted











How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that $dir [iteratively] represents each directory inside /var/www/html/?




That's just how Bash shell globs work/behave, but you are mistaken about one thing: dir/* is a glob which includes all files within dir, not just all directories: in a POSIX environment, directories are a file type, but in this case only directories are relevant to the for loop and the subsequent pushd-popd pair.




How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".




pushd and popd are a pair of tools that work with a data structure known as a "stack". Picture a spring-loaded dispenser, like a Pez dispenser.



If you push an item onto a stack, you are storing it for later use, like pushing one 'pill' into the top of the Pez dispenser.



If you pop an item from a stack, you are taking it out for use or reference. This removes it from the stack.



Take a look at this behavior for a simple example of how pushd and popd work:



$ pwd
/home/myuser
$ pushd /etc
/etc ~
$ pwd
/etc
$ pushd /usr/local
/usr/local /etc ~
$ pwd
/usr/local
$ popd
/etc ~
$ pwd
/etc
$ popd
~
$ pwd
/home/myuser


Your for loop basically works by saying if I can push this directory onto the directory stack, then that means firstly that the directory now exists, and secondly that that directory is now the current working directory. It will proceed to do the work, and then popd to go back to wherever it had been before, and then run the next iteration of the loop.






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%2f430220%2fhow-does-pushd-work%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
    3
    down vote



    accepted











    How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that $dir [iteratively] represents each directory inside /var/www/html/?




    That's just how Bash shell globs work/behave, but you are mistaken about one thing: dir/* is a glob which includes all files within dir, not just all directories: in a POSIX environment, directories are a file type, but in this case only directories are relevant to the for loop and the subsequent pushd-popd pair.




    How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".




    pushd and popd are a pair of tools that work with a data structure known as a "stack". Picture a spring-loaded dispenser, like a Pez dispenser.



    If you push an item onto a stack, you are storing it for later use, like pushing one 'pill' into the top of the Pez dispenser.



    If you pop an item from a stack, you are taking it out for use or reference. This removes it from the stack.



    Take a look at this behavior for a simple example of how pushd and popd work:



    $ pwd
    /home/myuser
    $ pushd /etc
    /etc ~
    $ pwd
    /etc
    $ pushd /usr/local
    /usr/local /etc ~
    $ pwd
    /usr/local
    $ popd
    /etc ~
    $ pwd
    /etc
    $ popd
    ~
    $ pwd
    /home/myuser


    Your for loop basically works by saying if I can push this directory onto the directory stack, then that means firstly that the directory now exists, and secondly that that directory is now the current working directory. It will proceed to do the work, and then popd to go back to wherever it had been before, and then run the next iteration of the loop.






    share|improve this answer


























      up vote
      3
      down vote



      accepted











      How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that $dir [iteratively] represents each directory inside /var/www/html/?




      That's just how Bash shell globs work/behave, but you are mistaken about one thing: dir/* is a glob which includes all files within dir, not just all directories: in a POSIX environment, directories are a file type, but in this case only directories are relevant to the for loop and the subsequent pushd-popd pair.




      How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".




      pushd and popd are a pair of tools that work with a data structure known as a "stack". Picture a spring-loaded dispenser, like a Pez dispenser.



      If you push an item onto a stack, you are storing it for later use, like pushing one 'pill' into the top of the Pez dispenser.



      If you pop an item from a stack, you are taking it out for use or reference. This removes it from the stack.



      Take a look at this behavior for a simple example of how pushd and popd work:



      $ pwd
      /home/myuser
      $ pushd /etc
      /etc ~
      $ pwd
      /etc
      $ pushd /usr/local
      /usr/local /etc ~
      $ pwd
      /usr/local
      $ popd
      /etc ~
      $ pwd
      /etc
      $ popd
      ~
      $ pwd
      /home/myuser


      Your for loop basically works by saying if I can push this directory onto the directory stack, then that means firstly that the directory now exists, and secondly that that directory is now the current working directory. It will proceed to do the work, and then popd to go back to wherever it had been before, and then run the next iteration of the loop.






      share|improve this answer
























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted







        How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that $dir [iteratively] represents each directory inside /var/www/html/?




        That's just how Bash shell globs work/behave, but you are mistaken about one thing: dir/* is a glob which includes all files within dir, not just all directories: in a POSIX environment, directories are a file type, but in this case only directories are relevant to the for loop and the subsequent pushd-popd pair.




        How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".




        pushd and popd are a pair of tools that work with a data structure known as a "stack". Picture a spring-loaded dispenser, like a Pez dispenser.



        If you push an item onto a stack, you are storing it for later use, like pushing one 'pill' into the top of the Pez dispenser.



        If you pop an item from a stack, you are taking it out for use or reference. This removes it from the stack.



        Take a look at this behavior for a simple example of how pushd and popd work:



        $ pwd
        /home/myuser
        $ pushd /etc
        /etc ~
        $ pwd
        /etc
        $ pushd /usr/local
        /usr/local /etc ~
        $ pwd
        /usr/local
        $ popd
        /etc ~
        $ pwd
        /etc
        $ popd
        ~
        $ pwd
        /home/myuser


        Your for loop basically works by saying if I can push this directory onto the directory stack, then that means firstly that the directory now exists, and secondly that that directory is now the current working directory. It will proceed to do the work, and then popd to go back to wherever it had been before, and then run the next iteration of the loop.






        share|improve this answer















        How does the dir variable resembles the asterisk (*) coming a little bit after it in the same line (how is the Bash interpreter knows that $dir [iteratively] represents each directory inside /var/www/html/?




        That's just how Bash shell globs work/behave, but you are mistaken about one thing: dir/* is a glob which includes all files within dir, not just all directories: in a POSIX environment, directories are a file type, but in this case only directories are relevant to the for loop and the subsequent pushd-popd pair.




        How does the pushd-popd sequence(?) works here? I understand it to "if you are inside $dir which resembles each iteration of the for loop, do stuff".




        pushd and popd are a pair of tools that work with a data structure known as a "stack". Picture a spring-loaded dispenser, like a Pez dispenser.



        If you push an item onto a stack, you are storing it for later use, like pushing one 'pill' into the top of the Pez dispenser.



        If you pop an item from a stack, you are taking it out for use or reference. This removes it from the stack.



        Take a look at this behavior for a simple example of how pushd and popd work:



        $ pwd
        /home/myuser
        $ pushd /etc
        /etc ~
        $ pwd
        /etc
        $ pushd /usr/local
        /usr/local /etc ~
        $ pwd
        /usr/local
        $ popd
        /etc ~
        $ pwd
        /etc
        $ popd
        ~
        $ pwd
        /home/myuser


        Your for loop basically works by saying if I can push this directory onto the directory stack, then that means firstly that the directory now exists, and secondly that that directory is now the current working directory. It will proceed to do the work, and then popd to go back to wherever it had been before, and then run the next iteration of the loop.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 14 at 19:46

























        answered Mar 14 at 17:03









        DopeGhoti

        40.2k54779




        40.2k54779






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430220%2fhow-does-pushd-work%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?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay