Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
while read pos; do
string1= echo $pos | cut -c 20-38
string3="$string1 $string2"
echo "$string3"
done < file

This is the code of my script. I want to display the output in one single line, but the output comes on different lines.

pos = abcdefghi1234567890QWERTYUXY.tar.gz
string1 = 1234567890QWERTYUXY
string2 = abcdefghi1234567890QWERTYUXY.tar.gz

Output wanted:

1234567890QWERTYUXY abcdefghi1234567890QWERTYUXY.tar.gz

But the two strings are displayed in two different lines. I want to display the output in 1 line.

share|improve this question
What is the input? Can you upload the file, or a sample file along the lines of what you're using to something like ? – djeikyb Feb 23 '11 at 10:14
Also double-posted to… – Olli Feb 23 '11 at 10:53
As a note, cross posting is strongly discouraged - it can lead to problems like this one with 1 answer posted on one site and the other three on another – Yi Jiang Feb 23 '11 at 13:45
There's no possible way that code will output more lines than there are lines in the file. Could you please post the actual code that has the problem? and preferably some example input and wanted output. – geirha Feb 23 '11 at 23:34

You can pipe the output through sed, notably:

echo "$string3 | sed ':a;N;$!ba;s/\n/ /g'

All credit to Zsolt Botykai on StackOverflow for his answer

share|improve this answer

A couple changes and it should work:
Generally, when you use a variable like you did in the echo, the original variable remains as it was, so your use of string 2 is totally unnecessary. Also, I am in the habit of using curly braces to {enclose} variables so they can be referenced right inside text, the curly braces work to delimit the variable name, just put the dollar sign in front (example to follow).

1- When you first set string1, do not put spaces on either side of the equal sign.
2- when assigning the results of a command to a variable, you have to force the command to be executed, single back ticks (the character to the left of the #1 key on US keyboards) surrounding the command do this.
3- watch for the number of characters you cut, your code and example do not agree. So that line should look like:

string1=`echo $pos | cut -c 20-38`

The other thing is when you set one value from another do not include the extra equal sign. So the code to set string2 should look like:


I believe the following will do as you request, I adjusted the character counts to give the output of your example:

The input "file" for my test contained:

And my script is:

while read pos; do
string1=`echo ${pos} | cut -c 10-28`
echo "${string1} ${pos}"
done < file

The output looks like:
1234567890QWERTYUXY abcdefghi1234567890QWERTYUXY.tar.gz

Which is exactly what I believe you are asking for.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.