Note that there are some explanatory texts on larger screens.

plurals
  1. POBash echo outputting text in middle of concatenated string
    primarykey
    data
    text
    <p>Preamble: So I have a project whereby I need to run a conversion tool on a bunch of files. There are multiple steps to the conversion, and it can take a while. Since these files aren't necessarily in the same directory, I created a text file with the filenames to convert:</p> <pre><code>&lt;filenames.txt&gt; /home/me/Documents/file1.txt /home/me/Documents/file2.txt /home/me/Documents/file3.txt </code></pre> <p>Problem: I then have a bash script to run over these files, and do the conversion steps. However, I try and echo the phase and filename, and echo, in this case, sticks the echo'd text in the middle of the filename:</p> <pre><code>&lt;script.sh&gt; cat filenames.txt | while read some_file do echo echo "Begin FORMAT_A to FORMAT_B conversion..." echo "FORMAT_A Filename:" echo $some_file formatb_dir= dirname $some_file | tr '\n' "/" formatb_file= basename $some_file ".xml" | tr '\n' "_" formatb=$formatb_dir$formatb_file"form_b.xml\n" echo "FORMAT_B Filename:" echo $formatb done </code></pre> <p>Produces output like this:</p> <pre><code>Begin FORMAT_A to FORMAT_B conversion... FORMAT_A Filename: /home/me/Documents/file1.txt /home/me/Documents/file1.txt_FORMAT_B Filename: form_b.xml Begin FORMAT_A to FORMAT_B conversion... FORMAT_A Filename: /home/me/Documents/file2.txt /home/me/Documents/file2.txt_FORMAT_B Filename: form_b.xml Begin FORMAT_A to FORMAT_B conversion... FORMAT_A Filename: /home/me/Documents/file3.txt /home/me/Documents/file3.txt_FORMAT_B Filename: form_b.xml </code></pre> <p>"FORMAT_B Filename", the echo'd text, has appeared before the appended text "form_b.xml".</p> <p>I looked at the man pages for dirname, basename, echo and tr, but I didn't spot anything that looks like it'd cause what I'm seeing. I tried looking around on Google, but I'm not sure I'm asking the right questions ("bash echo text before variable"?). </p> <p>Question: <strong>Why is my echo putting the echo'd text in the middle of my variable?</strong></p> <p>(possible related question: "Am I going about what I'm doing in completely the wrong way?")</p> <p><strong>Edit:</strong> I accepted fortea's answer because it walked me through what I didn't understand about variables and getting the output from commands. That said, dogbane and Jonathan Leffler showed better ways of doing what I was doing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload