how to get several commands into one variable for ease of use












0















I am writing a script and I noticed that a certain line of code is constantly being reused.



So I thought why not put it into a variable for ease of use, and when something changes, I only need to change it in one location.



When I do this:



scriptpath="echo -e "n" && curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder"


and then use the variable as following:



$SCRIPTPATH/some_script.sh | bash


I get the following error message:




bash: line 2: $'n': command not found











share|improve this question




















  • 3





    Is there a reason you don't just create a function to do the commands, storing commands in variables is generally bad practice.

    – Eric Renouf
    Dec 20 '18 at 13:19











  • the $SCRIPTPATH is part of a larger command, so i dont know how to combine functions and commands in one line together

    – WingZero
    Dec 20 '18 at 13:23











  • or is it possible to call forth a function with a variable?

    – WingZero
    Dec 20 '18 at 13:24






  • 4





    Perhaps you could give a more full picture of what you're actually trying to do, it feels like we might have something of an XY problem.

    – Eric Renouf
    Dec 20 '18 at 13:31






  • 1





    You can pass arguments to functions, does that solve the problem there?

    – Eric Renouf
    Dec 20 '18 at 13:32
















0















I am writing a script and I noticed that a certain line of code is constantly being reused.



So I thought why not put it into a variable for ease of use, and when something changes, I only need to change it in one location.



When I do this:



scriptpath="echo -e "n" && curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder"


and then use the variable as following:



$SCRIPTPATH/some_script.sh | bash


I get the following error message:




bash: line 2: $'n': command not found











share|improve this question




















  • 3





    Is there a reason you don't just create a function to do the commands, storing commands in variables is generally bad practice.

    – Eric Renouf
    Dec 20 '18 at 13:19











  • the $SCRIPTPATH is part of a larger command, so i dont know how to combine functions and commands in one line together

    – WingZero
    Dec 20 '18 at 13:23











  • or is it possible to call forth a function with a variable?

    – WingZero
    Dec 20 '18 at 13:24






  • 4





    Perhaps you could give a more full picture of what you're actually trying to do, it feels like we might have something of an XY problem.

    – Eric Renouf
    Dec 20 '18 at 13:31






  • 1





    You can pass arguments to functions, does that solve the problem there?

    – Eric Renouf
    Dec 20 '18 at 13:32














0












0








0








I am writing a script and I noticed that a certain line of code is constantly being reused.



So I thought why not put it into a variable for ease of use, and when something changes, I only need to change it in one location.



When I do this:



scriptpath="echo -e "n" && curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder"


and then use the variable as following:



$SCRIPTPATH/some_script.sh | bash


I get the following error message:




bash: line 2: $'n': command not found











share|improve this question
















I am writing a script and I noticed that a certain line of code is constantly being reused.



So I thought why not put it into a variable for ease of use, and when something changes, I only need to change it in one location.



When I do this:



scriptpath="echo -e "n" && curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder"


and then use the variable as following:



$SCRIPTPATH/some_script.sh | bash


I get the following error message:




bash: line 2: $'n': command not found








bash scripting variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 20 '18 at 14:24









Michael Prokopec

1,166117




1,166117










asked Dec 20 '18 at 13:15









WingZeroWingZero

435




435








  • 3





    Is there a reason you don't just create a function to do the commands, storing commands in variables is generally bad practice.

    – Eric Renouf
    Dec 20 '18 at 13:19











  • the $SCRIPTPATH is part of a larger command, so i dont know how to combine functions and commands in one line together

    – WingZero
    Dec 20 '18 at 13:23











  • or is it possible to call forth a function with a variable?

    – WingZero
    Dec 20 '18 at 13:24






  • 4





    Perhaps you could give a more full picture of what you're actually trying to do, it feels like we might have something of an XY problem.

    – Eric Renouf
    Dec 20 '18 at 13:31






  • 1





    You can pass arguments to functions, does that solve the problem there?

    – Eric Renouf
    Dec 20 '18 at 13:32














  • 3





    Is there a reason you don't just create a function to do the commands, storing commands in variables is generally bad practice.

    – Eric Renouf
    Dec 20 '18 at 13:19











  • the $SCRIPTPATH is part of a larger command, so i dont know how to combine functions and commands in one line together

    – WingZero
    Dec 20 '18 at 13:23











  • or is it possible to call forth a function with a variable?

    – WingZero
    Dec 20 '18 at 13:24






  • 4





    Perhaps you could give a more full picture of what you're actually trying to do, it feels like we might have something of an XY problem.

    – Eric Renouf
    Dec 20 '18 at 13:31






  • 1





    You can pass arguments to functions, does that solve the problem there?

    – Eric Renouf
    Dec 20 '18 at 13:32








3




3





Is there a reason you don't just create a function to do the commands, storing commands in variables is generally bad practice.

– Eric Renouf
Dec 20 '18 at 13:19





Is there a reason you don't just create a function to do the commands, storing commands in variables is generally bad practice.

– Eric Renouf
Dec 20 '18 at 13:19













the $SCRIPTPATH is part of a larger command, so i dont know how to combine functions and commands in one line together

– WingZero
Dec 20 '18 at 13:23





the $SCRIPTPATH is part of a larger command, so i dont know how to combine functions and commands in one line together

– WingZero
Dec 20 '18 at 13:23













or is it possible to call forth a function with a variable?

– WingZero
Dec 20 '18 at 13:24





or is it possible to call forth a function with a variable?

– WingZero
Dec 20 '18 at 13:24




4




4





Perhaps you could give a more full picture of what you're actually trying to do, it feels like we might have something of an XY problem.

– Eric Renouf
Dec 20 '18 at 13:31





Perhaps you could give a more full picture of what you're actually trying to do, it feels like we might have something of an XY problem.

– Eric Renouf
Dec 20 '18 at 13:31




1




1





You can pass arguments to functions, does that solve the problem there?

– Eric Renouf
Dec 20 '18 at 13:32





You can pass arguments to functions, does that solve the problem there?

– Eric Renouf
Dec 20 '18 at 13:32










1 Answer
1






active

oldest

votes


















0














You're describing a situation in which a function is exactly what you want.



do_download () {
printf 'n'
curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder
}


You would then use this in your code as



do_download


If the function needs to take an argument, for example the URL to use,



do_download () {
printf 'n'
curl -s -u lalala:hihihi "$1"
}


Then call it as



do_download "ftp://ftp.somewhere.com/folder"


Storing commands in variables is very rarely something that you'd want to do as quoting and word splitting is difficult to get right. See e.g. "How can we run a command stored in a variable?".






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',
    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%2f490123%2fhow-to-get-several-commands-into-one-variable-for-ease-of-use%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You're describing a situation in which a function is exactly what you want.



    do_download () {
    printf 'n'
    curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder
    }


    You would then use this in your code as



    do_download


    If the function needs to take an argument, for example the URL to use,



    do_download () {
    printf 'n'
    curl -s -u lalala:hihihi "$1"
    }


    Then call it as



    do_download "ftp://ftp.somewhere.com/folder"


    Storing commands in variables is very rarely something that you'd want to do as quoting and word splitting is difficult to get right. See e.g. "How can we run a command stored in a variable?".






    share|improve this answer




























      0














      You're describing a situation in which a function is exactly what you want.



      do_download () {
      printf 'n'
      curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder
      }


      You would then use this in your code as



      do_download


      If the function needs to take an argument, for example the URL to use,



      do_download () {
      printf 'n'
      curl -s -u lalala:hihihi "$1"
      }


      Then call it as



      do_download "ftp://ftp.somewhere.com/folder"


      Storing commands in variables is very rarely something that you'd want to do as quoting and word splitting is difficult to get right. See e.g. "How can we run a command stored in a variable?".






      share|improve this answer


























        0












        0








        0







        You're describing a situation in which a function is exactly what you want.



        do_download () {
        printf 'n'
        curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder
        }


        You would then use this in your code as



        do_download


        If the function needs to take an argument, for example the URL to use,



        do_download () {
        printf 'n'
        curl -s -u lalala:hihihi "$1"
        }


        Then call it as



        do_download "ftp://ftp.somewhere.com/folder"


        Storing commands in variables is very rarely something that you'd want to do as quoting and word splitting is difficult to get right. See e.g. "How can we run a command stored in a variable?".






        share|improve this answer













        You're describing a situation in which a function is exactly what you want.



        do_download () {
        printf 'n'
        curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder
        }


        You would then use this in your code as



        do_download


        If the function needs to take an argument, for example the URL to use,



        do_download () {
        printf 'n'
        curl -s -u lalala:hihihi "$1"
        }


        Then call it as



        do_download "ftp://ftp.somewhere.com/folder"


        Storing commands in variables is very rarely something that you'd want to do as quoting and word splitting is difficult to get right. See e.g. "How can we run a command stored in a variable?".







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 11 at 7:47









        KusalanandaKusalananda

        124k16234385




        124k16234385






























            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%2f490123%2fhow-to-get-several-commands-into-one-variable-for-ease-of-use%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

            How to make a Squid Proxy server?

            第一次世界大戦

            Touch on Surface Book