Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to write a script that will dump all partitions of my android device through the android debug bridge and since this is a time intensive process I want to notify the user about the progress by echoing out messages after certain key events happen. To my surprise I have noticed that sometimes echo does not print all the characters that it should.

The smallest sample that reproduces the error is:

#!/bin/bash

targets=$(adb shell "ls /dev/block/nand*")

for item in $targets; do
    echo "Processing $item"
    echo "dd if=$item bs=4M"
done

This produces the following output:

Processing /dev/block/nanda
dd if=/dev/block/nanda bs=4M
Processing /dev/block/nandd
dd if=/dev/block/nandd bs=4M
Processing /dev/block/nandg
dd if=/dev/block/nandg bs=4M
Processing /dev/block/nandj
 bs=4M/dev/block/nandj
Processing /dev/block/nandb
dd if=/dev/block/nandb bs=4M
Processing /dev/block/nande
dd if=/dev/block/nande bs=4M
Processing /dev/block/nandh
 bs=4M/dev/block/nandh
Processing /dev/block/nandc
dd if=/dev/block/nandc bs=4M
Processing /dev/block/nandf
dd if=/dev/block/nandf bs=4M
Processing /dev/block/nandi
 bs=4M/dev/block/nandi

Why does this happen and how can I fix it?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Your $item ends with a carriage return, so the cursor zips back to the beginning of the line and prints <space>bs=4M

You can do item=${item%$'\r'}

share|improve this answer
    
That was it. Thank you! –  Mihai Bişog Jun 26 '13 at 20:58

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.