Notinlist
Notinlist

Reputation: 16640

bash: Escape multiple parameters that are read from file

I have a file which contains URLs, one in each line. These URLs may contain spaces or other hazardous elements. I want to call a program with these urls, all of them at once.

urls.txt:

ptoto://domain/maybe with spaces & stuff 1
ptoto://domain/maybe with spaces & stuff 2
ptoto://domain/maybe with spaces & stuff 3

I want transform files like this to a call like:

myCommand "ptoto://domain/maybe with spaces & stuff 1" "ptoto://domain/maybe with spaces & stuff 2" "ptoto://domain/maybe with spaces & stuff 3"

Upvotes: 0

Views: 57

Answers (1)

Aserre
Aserre

Reputation: 5062

Try the following :

while read -r; do
    args+=("$REPLY")
done < textfile.txt
myCommand "${args[@]}"

Here, $REPLY is the default variable where read stores its result when no variable name is prompted. we use an array to store each line, and the double quotes forces to keep spaces & other characters.

"${args[@]}" display each element of the array

Upvotes: 2

Related Questions