0

I will preface this by saying I know next to nothing about bash, so I hope to get some clarification.

I have a function in which I can pass a here string or heredoc like so, and it will return a data file. The following examples work perfectly.

# Example 1
fetch_urls <<< "https://n5eil01u.ecs.nsidc.org/DP4/SMAP/SPL3SMP.008/2019.03.09/SMAP_L3_SM_P_20190309_R18290_001.h5"

# Example 2
urls = "https://n5eil01u.ecs.nsidc.org/DP4/SMAP/SPL3SMP.008/2019.03.09/SMAP_L3_SM_P_20190309_R18290_001.h5
https://n5eil01u.ecs.nsidc.org/DP4/SMAP/SPL3SMP.008/2019.03.08/SMAP_L3_SM_P_20190308_R18290_001.h5"

fetch_urls <<< "$urls"

So I can pass my function a string of urls either manually or through a variable. However, when I attempt to read a string of urls from a text file and store it into my url variable to pass to my function, everything breaks down.

I've tried my best to troubleshoot this, but I am not sure what is happening here. When I do echo "$urls" everything looks right.

urls = "https://n5eil01u.ecs.nsidc.org/DP4/SMAP/SPL3SMP.008/2019.03.09/SMAP_L3_SM_P_20190309_R18290_001.h5
https://n5eil01u.ecs.nsidc.org/DP4/SMAP/SPL3SMP.008/2019.03.08/SMAP_L3_SM_P_20190308_R18290_001.h5"

# Will not work
fetch_urls <<< echo "$urls"

# Will work
fetch_urls <<< "$urls"

What is going on here? I thought maybe echo might remove the newline character, and so that is why my function would breakdown. So I tried adding a newline character at the end using `printf '%s\n', but this still does not work.

# This is how I am extracting my urls from my text file
urls="$(while read -r line; do echo "$line"; done < urls.txt)"

# Feeding this into
fetch_urls <<< "$urls"

# or
fetch_urls <<< echo "$urls"

# does not work.
# But setting urls to a string does?
# echo "$urls" looks just like the string!
3
  • 2
    The thing that comes after <<< must be a string, not a command. If you want to pass the output of a command as input, use a pipe: echo "$urls" | fetch_urls. Or I suppose you could capture the output of the command with $( ) and pass that with a here-string: fetch_urls <<< "$(echo "$urls")" Commented Apr 21, 2022 at 2:30
  • 1
    There is still no need to read the file line by line. urls=$(cat yourflle) would be enough. Also your initial question was three hours before this one and you still state that you know nothing about bash. 3 hours is a hell of a lota time to learn a new skill. You could start for example here: guide.bash.academy Commented Apr 21, 2022 at 3:53
  • I've spent more than 3 hours on this. I don't understand bash. Yes, I would like to learn it, and understand it more, but unfortunately I have other priorities. I was stuck on this one issue, and the solutions partly work. I've been reading the docs to try and find out what is the issue, but I am using code that I did not write, and a language I've never used. I am struggling to understand the basic mechanisms as to why and how it works, so I posted another question that I hoped might give another example. The function only works, when the file has one url. Anymore than that, and it fails. Commented Apr 21, 2022 at 4:28

1 Answer 1

0

Below shall work fine.

fetch_urls <<< "$(echo $urls)"

Sign up to request clarification or add additional context in comments.

1 Comment

While this "works", it is a useless use of echo

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.