0

On Redhat linux environment, my Bash script (setup) contains the following lines of code:

#!/bin/bash
#CASE_C3
export RUN_FILES_LOCATION="./Run_Files/"
export CPP_FILE="${RUN_FILES_LOCATION}/APP77.ME"
echo $CPP_FILE

Running source setup in the terminal gives the following output:

/APP77.MEes/

Expected output:

./Run_Files//APP77.ME

It seems like somehow the variable is being overwritten byte-by-byte instead of concatenated. But I am following the exact syntax for concatenating strings in Bash. Why does that happen?

When I run the exact same lines of code in the terminal, I get the expected output. It's only running through a bash script does the result turn out incorrectly.

Nathan Goedeke
  • 115
  • 1
  • 8
  • try CPP_FILE=${RUN_FILES_LOCATION}"/APP77.ME" – Andrei Bastos May 04 '23 at 16:30
  • 1
    I can't reproduce: https://ideone.com/SszjBF – Barmar May 04 '23 at 16:33
  • 2
    Your source file has DOS line endings (`\r\n`). Convert it to Unix line endings (just `\n`). – M. Nejat Aydin May 04 '23 at 16:34
  • 1
    Use `dos2unix` to fix the script. – Barmar May 04 '23 at 16:34
  • If the complete file has DOS line endings then output should be `-bash: ./setup : /bin/bash^M: bad interpreter: No such file or directory` – Cyrus May 04 '23 at 16:37
  • Check your file for unwanted special characters: `cat -A setup` – Cyrus May 04 '23 at 16:40
  • `cat -A setup` shows that every line ends with the special character `^M$`. Is that correct or not? How would I fix it if it wasn't? – Nathan Goedeke May 04 '23 at 16:43
  • 1
    None of the solutions in the linked question removed the `\r\n`. I think closing the question was a bit premature.... – Nathan Goedeke May 04 '23 at 16:49
  • This is not correct. `dos2unix setup` should fix this. – Cyrus May 04 '23 at 17:16
  • @Cyrus I'm on a closed system and cannot use a third-party software – Nathan Goedeke May 04 '23 at 17:18
  • Several solutions besides `dos2unix` are described there under the heading *Solutions*: https://stackoverflow.com/a/39527986/3776858 – Cyrus May 04 '23 at 17:21
  • Using vim's fileformat command worked. I will mention that the solutions in the other linked question didn't work. – Nathan Goedeke May 04 '23 at 17:32
  • `perl`, `sed` and `awk` have all an option on your RHEL system to edit the file in place. – Cyrus May 04 '23 at 21:25
  • Did you VERIFY that you have carriage returns in your script, as everyone reasonably suggested? If you do, just remove them with any means you like. You do know how to edit a file, don't you? If there are no carriage returns at the end of the lines in your file, there could be a different reason for this behaviour; in this case, post at least a hexdump of the file. – user1934428 May 08 '23 at 11:21

0 Answers0