TylerSingleton
TylerSingleton

Reputation: 177

Bash: Does echo or printf not return a string?

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!

Upvotes: 0

Views: 116

Answers (1)

andoryu-
andoryu-

Reputation: 134

Below shall work fine.

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

Upvotes: 0

Related Questions