0

I am trying to do some tests to see if I can execute a shell script in a variables contents, similar to the Invoke-Expression function in powershell. I am getting the contents of the variable from curl:

a=$(curl https://some.site/test.sh)

however, whenever I print out the variable, it only gives the last line of the shell script.

Is there something I'm missing here? I'm using echo $a to print the variable.

john doe
  • 746
  • 1
  • 12
  • 27
  • 1
    How are you printing the variable? – jesse_b May 13 '21 at 15:14
  • 2
    does some.site/test.sh contain only carriage-returns, and no line-feeds? – cas May 13 '21 at 15:15
  • 1
    also, note the difference between (quoted) `echo "$a" | wc -l` and (unquoted) `echo $a | wc -l`. The former will print the actual line count. The latter will print a result of `1`. – cas May 13 '21 at 15:21
  • If i do `echo "$a"` (with quotes) it prints out the entire shell script which is what I was expecting to see, I was using `echo $a`. Also how do I tell if it has carriage returns? Its a linux shell script so I'm guessing it should have only \n – john doe May 13 '21 at 15:29
  • It doesn't have only CRs, that was just a guess, something to try. Now you have first-hand experience of why you should (almost) always double-quote your variables. the shell will enthusiastically word-split everything it can on any whitespace: spaces, tabs, newlines, etc. – cas May 13 '21 at 15:46

0 Answers0