The bash script somehow loses all trailing newlines, but I have no idea where, or how to keep them
#!/bin/bash
function rep {
    FIRST=${1// /" "}
    SECOND=${FIRST//&t;/"   "}
    echo "${SECOND//&nl;/"
"}"
}
if [ "$7" == "" ]
then
    cd "$(rep $6)"
    th sample.lua -checkpoint cv/"$(rep $1})"/"$(rep $2)" -length $3 -temperature $4 -sample 1 > samples/"$(rep $1)"/Sample-$5.txt
else
    cd "$(rep $7)"
    th sample.lua -checkpoint cv/"$(rep $1)"/"$(rep $2)" -length $3 -temperature $4 -sample 1 -start_text "$(rep $5)" > samples/"$(rep $1)"/Sample-$6.txt
fi
In this script the function rep replaces tags I have made in input strings that replace to spaces, tabs, and newlines. I'm pretty sure any whitespace and newline characters get trimmed off.
I've looked at solutions like this, https://stackoverflow.com/a/15184414/5332233, but I haven't been able to get it to work in the code.
EDIT - Here's the function when I tried to add a dummy character to the variable
function rep {
    FIRST="${1// /" "}"
    SECOND="${FIRST//&t;/"  "}"
    THIRD="${SECOND//&nl;/"
"}"
    a=$(printf $THIRD; printf x); echo ${a%x}
}
Here's another try, this still gets rid of all of the trailing newlines
#!/bin/bash
function rep {
    IN="$1x"
    FIRST="${IN// /" "}"
    SECOND="${FIRST//&t;/"  "}"
    THIRD="${SECOND//&nl;/"
"}"
    echo "${THIRD%x}"
}
echo "$(rep $5)"
if [ "$7" == "" ]
then
    cd "$(rep $6)"
    th sample.lua -checkpoint cv/"$(rep $1})"/"$(rep $2)" -length $3 -temperature $4 -sample 1 > samples/"$(rep $1)"/Sample-$5.txt
else
    cd "$(rep $7)"
    th sample.lua -checkpoint cv/"$(rep $1)"/"$(rep $2)" -length $3 -temperature $4 -sample 1 -start_text "$(rep $5)" > samples/"$(rep $1)"/Sample-$6.txt
fi
 
    