Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8775143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:44:02+00:00 2026-06-13T18:44:02+00:00

Preamble: So I have a project whereby I need to run a conversion tool

  • 0

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:

<filenames.txt>

/home/me/Documents/file1.txt
/home/me/Documents/file2.txt
/home/me/Documents/file3.txt

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:

<script.sh>

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

Produces output like this:

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

“FORMAT_B Filename”, the echo’d text, has appeared before the appended text “form_b.xml”.

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”?).

Question: Why is my echo putting the echo’d text in the middle of my variable?

(possible related question: “Am I going about what I’m doing in completely the wrong way?”)

Edit: 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:44:03+00:00Added an answer on June 13, 2026 at 6:44 pm

    The problem is in the declaration of variables:

    formatb_dir= dirname $some_file | tr '\n' "/"
    formatb_file= basename $some_file ".xml" | tr '\n' "_"
    formatb=$formatb_dir$formatb_file"form_b.xml\n"
    

    Try a new line: echo $formatb_dir: it does not contain anything because after the ‘=’ there is a blank space. Then bash reads dirname $somefile |tr '\n' "/" and it executes this command. The same when you create formatb_file, but this time the command is basename. (this is what appears before “FORMAT_B Filename:”).
    Then formatb is created correctly, but $formatb_dir and $formatb_file are empty, so $formatb will contain only "form_b.xml\n".
    Then you echo “FORMAT_B Filename:”.
    Then you echo $formatb, which will output “form_b.xml”.

    So, you should use something like this:

    formatb_dir=$(dirname $some_file | tr '\n' "/")
    formatb_file=$(basename $some_file ".xml" | tr '\n' "_")
    formatb=$formatb_dir$formatb_file"form_b.xml\n"
    

    Read here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have modified some files of an open source project under GNU GENERAL PUBLIC
Preamble To build dynamic web-sites, we have to master at least four languages: HTML
Small preamble. I was good java developer on 1.4 jdk. After it I have
Preamble I've searched to see if I can find this question anywhere on here
Preamble: I know, disabling warnings is not a good idea. Anyway, I have a
[Preamble: Whereas I realize there may be simpler ways to do this (i.e., just
I have a situation where I can't host a service using net.tcp under windows
Preamble I have written GLR-parser with error recovery. When it encounters an error it
The preamble: I have designed a strongly interfaced and fully mockable data layer class
Without any preamble I want to show you problem I have in my program,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.