Execute bash commands that are within a list from python












0















i got this list



commands = ['cd var','cd www','cd html','sudo rm -r folder']


I'm trying to execute one by one all the elements inside as a bash script, with no success. Do i need a for loop here?



how to achieve that?, thanks all!!!!










share|improve this question

























  • Possible duplicate of running multiple bash commands with subprocess

    – l'L'l
    Nov 15 '18 at 0:46






  • 1





    You could condense the cd commands into one command, then use Bash's && (and) operator: os.system("cd var/www/html/ && sudo rm -r folder")

    – Aviv Shai
    Nov 15 '18 at 1:04













  • What does Python have to do with this? You said "execute as a bash script".

    – John Gordon
    Nov 15 '18 at 1:34






  • 1





    Possible duplicate of Delete a file or folder

    – tripleee
    Nov 15 '18 at 5:57






  • 1





    Maybe see also stackoverflow.com/a/51950538/874188 for a number of things to look out for with subprocesses.

    – tripleee
    Nov 15 '18 at 6:23


















0















i got this list



commands = ['cd var','cd www','cd html','sudo rm -r folder']


I'm trying to execute one by one all the elements inside as a bash script, with no success. Do i need a for loop here?



how to achieve that?, thanks all!!!!










share|improve this question

























  • Possible duplicate of running multiple bash commands with subprocess

    – l'L'l
    Nov 15 '18 at 0:46






  • 1





    You could condense the cd commands into one command, then use Bash's && (and) operator: os.system("cd var/www/html/ && sudo rm -r folder")

    – Aviv Shai
    Nov 15 '18 at 1:04













  • What does Python have to do with this? You said "execute as a bash script".

    – John Gordon
    Nov 15 '18 at 1:34






  • 1





    Possible duplicate of Delete a file or folder

    – tripleee
    Nov 15 '18 at 5:57






  • 1





    Maybe see also stackoverflow.com/a/51950538/874188 for a number of things to look out for with subprocesses.

    – tripleee
    Nov 15 '18 at 6:23
















0












0








0








i got this list



commands = ['cd var','cd www','cd html','sudo rm -r folder']


I'm trying to execute one by one all the elements inside as a bash script, with no success. Do i need a for loop here?



how to achieve that?, thanks all!!!!










share|improve this question
















i got this list



commands = ['cd var','cd www','cd html','sudo rm -r folder']


I'm trying to execute one by one all the elements inside as a bash script, with no success. Do i need a for loop here?



how to achieve that?, thanks all!!!!







python arrays tuples






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 2:45









Joran Beasley

73.4k680120




73.4k680120










asked Nov 15 '18 at 0:42









Mauro EncinasMauro Encinas

2414




2414













  • Possible duplicate of running multiple bash commands with subprocess

    – l'L'l
    Nov 15 '18 at 0:46






  • 1





    You could condense the cd commands into one command, then use Bash's && (and) operator: os.system("cd var/www/html/ && sudo rm -r folder")

    – Aviv Shai
    Nov 15 '18 at 1:04













  • What does Python have to do with this? You said "execute as a bash script".

    – John Gordon
    Nov 15 '18 at 1:34






  • 1





    Possible duplicate of Delete a file or folder

    – tripleee
    Nov 15 '18 at 5:57






  • 1





    Maybe see also stackoverflow.com/a/51950538/874188 for a number of things to look out for with subprocesses.

    – tripleee
    Nov 15 '18 at 6:23





















  • Possible duplicate of running multiple bash commands with subprocess

    – l'L'l
    Nov 15 '18 at 0:46






  • 1





    You could condense the cd commands into one command, then use Bash's && (and) operator: os.system("cd var/www/html/ && sudo rm -r folder")

    – Aviv Shai
    Nov 15 '18 at 1:04













  • What does Python have to do with this? You said "execute as a bash script".

    – John Gordon
    Nov 15 '18 at 1:34






  • 1





    Possible duplicate of Delete a file or folder

    – tripleee
    Nov 15 '18 at 5:57






  • 1





    Maybe see also stackoverflow.com/a/51950538/874188 for a number of things to look out for with subprocesses.

    – tripleee
    Nov 15 '18 at 6:23



















Possible duplicate of running multiple bash commands with subprocess

– l'L'l
Nov 15 '18 at 0:46





Possible duplicate of running multiple bash commands with subprocess

– l'L'l
Nov 15 '18 at 0:46




1




1





You could condense the cd commands into one command, then use Bash's && (and) operator: os.system("cd var/www/html/ && sudo rm -r folder")

– Aviv Shai
Nov 15 '18 at 1:04







You could condense the cd commands into one command, then use Bash's && (and) operator: os.system("cd var/www/html/ && sudo rm -r folder")

– Aviv Shai
Nov 15 '18 at 1:04















What does Python have to do with this? You said "execute as a bash script".

– John Gordon
Nov 15 '18 at 1:34





What does Python have to do with this? You said "execute as a bash script".

– John Gordon
Nov 15 '18 at 1:34




1




1





Possible duplicate of Delete a file or folder

– tripleee
Nov 15 '18 at 5:57





Possible duplicate of Delete a file or folder

– tripleee
Nov 15 '18 at 5:57




1




1





Maybe see also stackoverflow.com/a/51950538/874188 for a number of things to look out for with subprocesses.

– tripleee
Nov 15 '18 at 6:23







Maybe see also stackoverflow.com/a/51950538/874188 for a number of things to look out for with subprocesses.

– tripleee
Nov 15 '18 at 6:23














3 Answers
3






active

oldest

votes


















3














for command in commands:
os.system(command)


is one way you could do it ... although just cd'ing into a bunch of directories isnt going to have much impact



NOTE this will run each command in its own subshell ... so they would not remember their state (ie any directory changes or environmental variables)



if you need to run them all in one subshell than you need to chain them together with "&&"



os.system(" && ".join(commands)) # would run all of the commands in a single subshell


as noted in the comments, in general it is preferred to use subprocess module with check_call or one of the other variants. however in this specific instance i personally think that you are in a 6 to 1 half a dozen to the other, and os.system was less typing (and its gonna exist whether you are using python3.7 or python2.5 ... but in general use subprocess exactly which call probably depends on the version of python you are using ... there is a great description in the post linked in the comments by @triplee why you should use subprocess instead)



really you should reformat your commands to simply



commands = ["sudo rm -rf var/www/html/folder"] note that you will probably need to add your python file to your sudoers file



also Im not sure exactly what you are trying to accomplish here ... but i suspect this might not be the ideal way to go about it (although it should work...)






share|improve this answer


























  • This won't work because cd var will run in a subprocess, then the next command runs in the (unchanged) directory of the parent Python process.

    – tripleee
    Nov 15 '18 at 5:55













  • the condition that they all run in the same subshell was not stipulated ... but i can see how the OP would potentially desire that behaviour

    – Joran Beasley
    Nov 15 '18 at 6:22











  • No, it wasn't spelled out, and probably the OP doesn't even realize this; but this is a common enough bug with subprocesses.

    – tripleee
    Nov 15 '18 at 6:23











  • Alao you should prefer subprocess.run() or subprocess.check_call() over os.system(); see the post I linked from another comment above.

    – tripleee
    Nov 15 '18 at 6:25













  • none of the compelling reasons to do that apply here imho .... its really 6 to 1 half a dozen to the other ... in fact he is using commands that would require shell=True so its pretty extra moot ... that said in general yes... this was just less typing

    – Joran Beasley
    Nov 15 '18 at 6:26





















3














This is just a suggestion, but if your just wanting to change directories and delete folders, you could use os.chdir() and shutil.rmtree():



from os import chdir
from os import getcwd
from shutil import rmtree

directories = ['var','www','html','folder']

print(getcwd())
# current working directory: $PWD

for directory in directories[:-1]:
chdir(directory)

print(getcwd())
# current working directory: $PWD/var/www/html

rmtree(directories[-1])


Which will cd three directories deep into html, and delelte folder. The current working directory changes when you call chdir(), as seen when you call os.getcwd().






share|improve this answer

































    1














    declare -a command=("cd var","cd www","cd html","sudo rm -r folder")

    ## now loop through the above array
    for i in "${command[@]}"
    do
    echo "$i"
    # or do whatever with individual element of the array
    done

    # You can access them using echo "${arr[0]}", "${arr[1]}" also





    share|improve this answer
























    • this is awesome :) I think they were asking how to do it in python but this is great! thanks!

      – Joran Beasley
      Nov 15 '18 at 1:22











    • The OP's writing Python code.

      – Emily E.
      Nov 15 '18 at 2:38











    • A python list can be very easily imported in to bash stackoverflow.com/questions/26162394/…

      – Kalangu
      Nov 15 '18 at 21:42













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53310878%2fexecute-bash-commands-that-are-within-a-list-from-python%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









    3














    for command in commands:
    os.system(command)


    is one way you could do it ... although just cd'ing into a bunch of directories isnt going to have much impact



    NOTE this will run each command in its own subshell ... so they would not remember their state (ie any directory changes or environmental variables)



    if you need to run them all in one subshell than you need to chain them together with "&&"



    os.system(" && ".join(commands)) # would run all of the commands in a single subshell


    as noted in the comments, in general it is preferred to use subprocess module with check_call or one of the other variants. however in this specific instance i personally think that you are in a 6 to 1 half a dozen to the other, and os.system was less typing (and its gonna exist whether you are using python3.7 or python2.5 ... but in general use subprocess exactly which call probably depends on the version of python you are using ... there is a great description in the post linked in the comments by @triplee why you should use subprocess instead)



    really you should reformat your commands to simply



    commands = ["sudo rm -rf var/www/html/folder"] note that you will probably need to add your python file to your sudoers file



    also Im not sure exactly what you are trying to accomplish here ... but i suspect this might not be the ideal way to go about it (although it should work...)






    share|improve this answer


























    • This won't work because cd var will run in a subprocess, then the next command runs in the (unchanged) directory of the parent Python process.

      – tripleee
      Nov 15 '18 at 5:55













    • the condition that they all run in the same subshell was not stipulated ... but i can see how the OP would potentially desire that behaviour

      – Joran Beasley
      Nov 15 '18 at 6:22











    • No, it wasn't spelled out, and probably the OP doesn't even realize this; but this is a common enough bug with subprocesses.

      – tripleee
      Nov 15 '18 at 6:23











    • Alao you should prefer subprocess.run() or subprocess.check_call() over os.system(); see the post I linked from another comment above.

      – tripleee
      Nov 15 '18 at 6:25













    • none of the compelling reasons to do that apply here imho .... its really 6 to 1 half a dozen to the other ... in fact he is using commands that would require shell=True so its pretty extra moot ... that said in general yes... this was just less typing

      – Joran Beasley
      Nov 15 '18 at 6:26


















    3














    for command in commands:
    os.system(command)


    is one way you could do it ... although just cd'ing into a bunch of directories isnt going to have much impact



    NOTE this will run each command in its own subshell ... so they would not remember their state (ie any directory changes or environmental variables)



    if you need to run them all in one subshell than you need to chain them together with "&&"



    os.system(" && ".join(commands)) # would run all of the commands in a single subshell


    as noted in the comments, in general it is preferred to use subprocess module with check_call or one of the other variants. however in this specific instance i personally think that you are in a 6 to 1 half a dozen to the other, and os.system was less typing (and its gonna exist whether you are using python3.7 or python2.5 ... but in general use subprocess exactly which call probably depends on the version of python you are using ... there is a great description in the post linked in the comments by @triplee why you should use subprocess instead)



    really you should reformat your commands to simply



    commands = ["sudo rm -rf var/www/html/folder"] note that you will probably need to add your python file to your sudoers file



    also Im not sure exactly what you are trying to accomplish here ... but i suspect this might not be the ideal way to go about it (although it should work...)






    share|improve this answer


























    • This won't work because cd var will run in a subprocess, then the next command runs in the (unchanged) directory of the parent Python process.

      – tripleee
      Nov 15 '18 at 5:55













    • the condition that they all run in the same subshell was not stipulated ... but i can see how the OP would potentially desire that behaviour

      – Joran Beasley
      Nov 15 '18 at 6:22











    • No, it wasn't spelled out, and probably the OP doesn't even realize this; but this is a common enough bug with subprocesses.

      – tripleee
      Nov 15 '18 at 6:23











    • Alao you should prefer subprocess.run() or subprocess.check_call() over os.system(); see the post I linked from another comment above.

      – tripleee
      Nov 15 '18 at 6:25













    • none of the compelling reasons to do that apply here imho .... its really 6 to 1 half a dozen to the other ... in fact he is using commands that would require shell=True so its pretty extra moot ... that said in general yes... this was just less typing

      – Joran Beasley
      Nov 15 '18 at 6:26
















    3












    3








    3







    for command in commands:
    os.system(command)


    is one way you could do it ... although just cd'ing into a bunch of directories isnt going to have much impact



    NOTE this will run each command in its own subshell ... so they would not remember their state (ie any directory changes or environmental variables)



    if you need to run them all in one subshell than you need to chain them together with "&&"



    os.system(" && ".join(commands)) # would run all of the commands in a single subshell


    as noted in the comments, in general it is preferred to use subprocess module with check_call or one of the other variants. however in this specific instance i personally think that you are in a 6 to 1 half a dozen to the other, and os.system was less typing (and its gonna exist whether you are using python3.7 or python2.5 ... but in general use subprocess exactly which call probably depends on the version of python you are using ... there is a great description in the post linked in the comments by @triplee why you should use subprocess instead)



    really you should reformat your commands to simply



    commands = ["sudo rm -rf var/www/html/folder"] note that you will probably need to add your python file to your sudoers file



    also Im not sure exactly what you are trying to accomplish here ... but i suspect this might not be the ideal way to go about it (although it should work...)






    share|improve this answer















    for command in commands:
    os.system(command)


    is one way you could do it ... although just cd'ing into a bunch of directories isnt going to have much impact



    NOTE this will run each command in its own subshell ... so they would not remember their state (ie any directory changes or environmental variables)



    if you need to run them all in one subshell than you need to chain them together with "&&"



    os.system(" && ".join(commands)) # would run all of the commands in a single subshell


    as noted in the comments, in general it is preferred to use subprocess module with check_call or one of the other variants. however in this specific instance i personally think that you are in a 6 to 1 half a dozen to the other, and os.system was less typing (and its gonna exist whether you are using python3.7 or python2.5 ... but in general use subprocess exactly which call probably depends on the version of python you are using ... there is a great description in the post linked in the comments by @triplee why you should use subprocess instead)



    really you should reformat your commands to simply



    commands = ["sudo rm -rf var/www/html/folder"] note that you will probably need to add your python file to your sudoers file



    also Im not sure exactly what you are trying to accomplish here ... but i suspect this might not be the ideal way to go about it (although it should work...)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 6:35

























    answered Nov 15 '18 at 0:44









    Joran BeasleyJoran Beasley

    73.4k680120




    73.4k680120













    • This won't work because cd var will run in a subprocess, then the next command runs in the (unchanged) directory of the parent Python process.

      – tripleee
      Nov 15 '18 at 5:55













    • the condition that they all run in the same subshell was not stipulated ... but i can see how the OP would potentially desire that behaviour

      – Joran Beasley
      Nov 15 '18 at 6:22











    • No, it wasn't spelled out, and probably the OP doesn't even realize this; but this is a common enough bug with subprocesses.

      – tripleee
      Nov 15 '18 at 6:23











    • Alao you should prefer subprocess.run() or subprocess.check_call() over os.system(); see the post I linked from another comment above.

      – tripleee
      Nov 15 '18 at 6:25













    • none of the compelling reasons to do that apply here imho .... its really 6 to 1 half a dozen to the other ... in fact he is using commands that would require shell=True so its pretty extra moot ... that said in general yes... this was just less typing

      – Joran Beasley
      Nov 15 '18 at 6:26





















    • This won't work because cd var will run in a subprocess, then the next command runs in the (unchanged) directory of the parent Python process.

      – tripleee
      Nov 15 '18 at 5:55













    • the condition that they all run in the same subshell was not stipulated ... but i can see how the OP would potentially desire that behaviour

      – Joran Beasley
      Nov 15 '18 at 6:22











    • No, it wasn't spelled out, and probably the OP doesn't even realize this; but this is a common enough bug with subprocesses.

      – tripleee
      Nov 15 '18 at 6:23











    • Alao you should prefer subprocess.run() or subprocess.check_call() over os.system(); see the post I linked from another comment above.

      – tripleee
      Nov 15 '18 at 6:25













    • none of the compelling reasons to do that apply here imho .... its really 6 to 1 half a dozen to the other ... in fact he is using commands that would require shell=True so its pretty extra moot ... that said in general yes... this was just less typing

      – Joran Beasley
      Nov 15 '18 at 6:26



















    This won't work because cd var will run in a subprocess, then the next command runs in the (unchanged) directory of the parent Python process.

    – tripleee
    Nov 15 '18 at 5:55







    This won't work because cd var will run in a subprocess, then the next command runs in the (unchanged) directory of the parent Python process.

    – tripleee
    Nov 15 '18 at 5:55















    the condition that they all run in the same subshell was not stipulated ... but i can see how the OP would potentially desire that behaviour

    – Joran Beasley
    Nov 15 '18 at 6:22





    the condition that they all run in the same subshell was not stipulated ... but i can see how the OP would potentially desire that behaviour

    – Joran Beasley
    Nov 15 '18 at 6:22













    No, it wasn't spelled out, and probably the OP doesn't even realize this; but this is a common enough bug with subprocesses.

    – tripleee
    Nov 15 '18 at 6:23





    No, it wasn't spelled out, and probably the OP doesn't even realize this; but this is a common enough bug with subprocesses.

    – tripleee
    Nov 15 '18 at 6:23













    Alao you should prefer subprocess.run() or subprocess.check_call() over os.system(); see the post I linked from another comment above.

    – tripleee
    Nov 15 '18 at 6:25







    Alao you should prefer subprocess.run() or subprocess.check_call() over os.system(); see the post I linked from another comment above.

    – tripleee
    Nov 15 '18 at 6:25















    none of the compelling reasons to do that apply here imho .... its really 6 to 1 half a dozen to the other ... in fact he is using commands that would require shell=True so its pretty extra moot ... that said in general yes... this was just less typing

    – Joran Beasley
    Nov 15 '18 at 6:26







    none of the compelling reasons to do that apply here imho .... its really 6 to 1 half a dozen to the other ... in fact he is using commands that would require shell=True so its pretty extra moot ... that said in general yes... this was just less typing

    – Joran Beasley
    Nov 15 '18 at 6:26















    3














    This is just a suggestion, but if your just wanting to change directories and delete folders, you could use os.chdir() and shutil.rmtree():



    from os import chdir
    from os import getcwd
    from shutil import rmtree

    directories = ['var','www','html','folder']

    print(getcwd())
    # current working directory: $PWD

    for directory in directories[:-1]:
    chdir(directory)

    print(getcwd())
    # current working directory: $PWD/var/www/html

    rmtree(directories[-1])


    Which will cd three directories deep into html, and delelte folder. The current working directory changes when you call chdir(), as seen when you call os.getcwd().






    share|improve this answer






























      3














      This is just a suggestion, but if your just wanting to change directories and delete folders, you could use os.chdir() and shutil.rmtree():



      from os import chdir
      from os import getcwd
      from shutil import rmtree

      directories = ['var','www','html','folder']

      print(getcwd())
      # current working directory: $PWD

      for directory in directories[:-1]:
      chdir(directory)

      print(getcwd())
      # current working directory: $PWD/var/www/html

      rmtree(directories[-1])


      Which will cd three directories deep into html, and delelte folder. The current working directory changes when you call chdir(), as seen when you call os.getcwd().






      share|improve this answer




























        3












        3








        3







        This is just a suggestion, but if your just wanting to change directories and delete folders, you could use os.chdir() and shutil.rmtree():



        from os import chdir
        from os import getcwd
        from shutil import rmtree

        directories = ['var','www','html','folder']

        print(getcwd())
        # current working directory: $PWD

        for directory in directories[:-1]:
        chdir(directory)

        print(getcwd())
        # current working directory: $PWD/var/www/html

        rmtree(directories[-1])


        Which will cd three directories deep into html, and delelte folder. The current working directory changes when you call chdir(), as seen when you call os.getcwd().






        share|improve this answer















        This is just a suggestion, but if your just wanting to change directories and delete folders, you could use os.chdir() and shutil.rmtree():



        from os import chdir
        from os import getcwd
        from shutil import rmtree

        directories = ['var','www','html','folder']

        print(getcwd())
        # current working directory: $PWD

        for directory in directories[:-1]:
        chdir(directory)

        print(getcwd())
        # current working directory: $PWD/var/www/html

        rmtree(directories[-1])


        Which will cd three directories deep into html, and delelte folder. The current working directory changes when you call chdir(), as seen when you call os.getcwd().







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 1:50

























        answered Nov 15 '18 at 1:38









        RoadRunnerRoadRunner

        11.2k31340




        11.2k31340























            1














            declare -a command=("cd var","cd www","cd html","sudo rm -r folder")

            ## now loop through the above array
            for i in "${command[@]}"
            do
            echo "$i"
            # or do whatever with individual element of the array
            done

            # You can access them using echo "${arr[0]}", "${arr[1]}" also





            share|improve this answer
























            • this is awesome :) I think they were asking how to do it in python but this is great! thanks!

              – Joran Beasley
              Nov 15 '18 at 1:22











            • The OP's writing Python code.

              – Emily E.
              Nov 15 '18 at 2:38











            • A python list can be very easily imported in to bash stackoverflow.com/questions/26162394/…

              – Kalangu
              Nov 15 '18 at 21:42


















            1














            declare -a command=("cd var","cd www","cd html","sudo rm -r folder")

            ## now loop through the above array
            for i in "${command[@]}"
            do
            echo "$i"
            # or do whatever with individual element of the array
            done

            # You can access them using echo "${arr[0]}", "${arr[1]}" also





            share|improve this answer
























            • this is awesome :) I think they were asking how to do it in python but this is great! thanks!

              – Joran Beasley
              Nov 15 '18 at 1:22











            • The OP's writing Python code.

              – Emily E.
              Nov 15 '18 at 2:38











            • A python list can be very easily imported in to bash stackoverflow.com/questions/26162394/…

              – Kalangu
              Nov 15 '18 at 21:42
















            1












            1








            1







            declare -a command=("cd var","cd www","cd html","sudo rm -r folder")

            ## now loop through the above array
            for i in "${command[@]}"
            do
            echo "$i"
            # or do whatever with individual element of the array
            done

            # You can access them using echo "${arr[0]}", "${arr[1]}" also





            share|improve this answer













            declare -a command=("cd var","cd www","cd html","sudo rm -r folder")

            ## now loop through the above array
            for i in "${command[@]}"
            do
            echo "$i"
            # or do whatever with individual element of the array
            done

            # You can access them using echo "${arr[0]}", "${arr[1]}" also






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 1:02









            KalanguKalangu

            516




            516













            • this is awesome :) I think they were asking how to do it in python but this is great! thanks!

              – Joran Beasley
              Nov 15 '18 at 1:22











            • The OP's writing Python code.

              – Emily E.
              Nov 15 '18 at 2:38











            • A python list can be very easily imported in to bash stackoverflow.com/questions/26162394/…

              – Kalangu
              Nov 15 '18 at 21:42





















            • this is awesome :) I think they were asking how to do it in python but this is great! thanks!

              – Joran Beasley
              Nov 15 '18 at 1:22











            • The OP's writing Python code.

              – Emily E.
              Nov 15 '18 at 2:38











            • A python list can be very easily imported in to bash stackoverflow.com/questions/26162394/…

              – Kalangu
              Nov 15 '18 at 21:42



















            this is awesome :) I think they were asking how to do it in python but this is great! thanks!

            – Joran Beasley
            Nov 15 '18 at 1:22





            this is awesome :) I think they were asking how to do it in python but this is great! thanks!

            – Joran Beasley
            Nov 15 '18 at 1:22













            The OP's writing Python code.

            – Emily E.
            Nov 15 '18 at 2:38





            The OP's writing Python code.

            – Emily E.
            Nov 15 '18 at 2:38













            A python list can be very easily imported in to bash stackoverflow.com/questions/26162394/…

            – Kalangu
            Nov 15 '18 at 21:42







            A python list can be very easily imported in to bash stackoverflow.com/questions/26162394/…

            – Kalangu
            Nov 15 '18 at 21:42




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53310878%2fexecute-bash-commands-that-are-within-a-list-from-python%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

            Bressuire

            Vorschmack

            Quarantine