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

  • 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
  • something like this

    'function scriptpath { echo -e "\n" && curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder/$SCRIPTNAME | bash }'

    but then i need to give the SCRIPTNAME variable a value when calling forth the scriptpath function

    – WingZero Dec 20 '18 at 13:31
  • 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
  • Why is it that you need to pass \n to the command line before doing curl? – Michael Prokopec Dec 20 '18 at 13:52
  • "A certain piece of code is constantly being reused" is the definition of when you should consider using a function. – Kusalananda Dec 20 '18 at 14:49
  • If that piece of code is repeatedly used within the same program (script), put it in a function. If it's used repeatedly from outside a single program, put it in a script. In any case, your question is unclear, since it doesn't show what you're actually doing (scriptpath doesn't look like a path, should it be one? Is there some relation between scriptpath and SCRIPTPATH?) Please [edit] your question to clarify it (don't just put the clarifications in comments). – ilkkachu Dec 20 '18 at 17:43
  • Also, if you're running curl ... | bash (repeatedly), you may want to consider if it's a safe and sane thing to blindly run code downloaded from the internet. – ilkkachu Dec 20 '18 at 17:44

1 Answers1

1

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?".

Kusalananda
  • 333,661