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,042116




1,042116










asked Dec 20 '18 at 13:15









WingZero

385




385








  • 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










0






active

oldest

votes











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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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

List directoties down one level, excluding some named directories and files

list processes belonging to a network namespace

list systemd RuntimeDirectory mounts