Search code examples
bashshellunixstring-concatenation

Variables get overwritten when concatenated


I wrote this little bash script:

#!/bin/bash -x
runDirectory="/mnt/sequences_tmp/melody_rnn/logdir/run_"
currName=$1
magenta1="bazel run //magenta/models/melody_rnn:melody_rnn_train -- --config=attention_rnn --run_dir="
echo $magenta1
magenta2="$magenta1$runDirectory"
echo $magenta2
magenta3="$magenta2$currName"
echo $magenta3

so that, if I start the script with the parameter "music" the output should be as follows:

bazel run //magenta/models/melody_rnn:melody_rnn_train -- --config=attention_rnn --run_dir=

bazel run //magenta/models/melody_rnn:melody_rnn_train -- --config=attention_rnn --run_dir=/mnt/sequences_tmp/melody_rnn/logdir/run_

bazel run //magenta/models/melody_rnn:melody_rnn_train -- --config=attention_rnn --run_dir=/mnt/sequences_tmp/melody_rnn/logdir/run_music

but the output I really get is:

bazel run //magenta/models/melody_rnn:melody_rnn_train -- --config=attention_rnn --run_dir=

/mnt/sequences_tmp/melody_rnn/logdir/run_ody_rnn_train -- --config=attention_rnn --run_dir=

musicsequences_tmp/melody_rnn/logdir/run_ody_rnn_train -- --config=attention_rnn --run_dir=

As you can see, instead of appending the next variable/text it instead is added to the front of the last used variable, overwriting the old variable in the progress.

Does anyone know what my error could be? I haven't found any similar questions on here yet (sadly)


Solution

  • That sounds like your variables contain \r (carriage return, 0x0D) somewhere. You can pipe the output into xxd (or any other hex dump tool) to confirm.

    Was your script saved in DOS format?