I'm creating a bash script that will allow me to clone disks, and allow the user to select the input disk to clone and the output disk to clone to. I've almost finished, but I just have one hang up. dd doesn't recognize the variable names for the disks. Supposing srcDisk and destDisk are defined as existing disks (these variables are called elsewhere in my script and work with utilities such as sfdisk and fdisk), here is what I've tried:

    dd if=/dev/$srcDisk of=/dev/$destDisk

I have also wondered if I need to specify partitions, and since number of partitions may vary, I tried a do until loop, with the iterator variable (i) specifying the partition number. This still didn't work:

until [ $i -gt srcPartNum ]
    dd if=/dev/$srcDisk$i of=/dev/$destDisk$i conv=notrunc & pid=$!
    while kill -USR1 $pid; do sleep 1; done

I'm not sure how to fix this or any workarounds.

  • Have you tried placing the variable names within quotes? ("$srcDisk")
    – MikeD
    Commented Mar 16, 2017 at 0:32
  • Try echoing the variables to the command line: is it possible that $srcDisk and/or $destDisk have more than just "sdx" as a value, but contain something like "/dev/sdx", making the command you issued dd if=/dev/$srcDisk of=/dev/$destDisk look like dd if=/dev//dev/sda of=/dev//dev/sdb
    – Schives
    Commented Mar 16, 2017 at 0:37
  • How are you setting the variables? Are they set outside the script, or inside? If set outside, are you exporting them, or passing as srcDisk=foo /path/to/myscript, or neither? What error are you getting when the script runs?
    – phemmer
    Commented Mar 16, 2017 at 1:08
  • So it looks like it's working after putting the variable names between quotes. Thanks! The variables are read in from user input within the script.
    – MattR
    Commented Mar 16, 2017 at 20:39
  • EDIT: It is not working, even after putting in the quotes. What it gives me is: /usr/local/bin/clonedisk.sh: line 37: [: srcPartNum: integer expression expected /usr/local/bin/clonedisk.sh: line 41: 12521 User defined signal 1 dd if=/dev/"$srcDisk" of=/dev/"$destDisk" conv=notrunc The variables are read into the script from user input. I input them in as mmcblk0 and sda respectively.
    – MattR
    Commented Mar 16, 2017 at 20:51

1 Answer 1


First off, your dd will be desperately slow. Use this instead

cat "/dev/$srcDisk" >"/dev/$destDisk"

Your source and destination disks must not be in use when you copy them.

Without seeing an error message or other result from your existing script i can't tell you why dd isn't recognising your variables. Aside from not quoting the variables there's nothing syntactically wrong with your snippet. You could maybe echo out the variables' values at the point you copy the disk, or add set -x a little way above the problematic piece of code.

As an aside, have you investigated scripting something like Clonezilla?

  • 1
    Or pv if you want to see a pretty progress bar.
    – phemmer
    Commented Mar 16, 2017 at 1:08
  • Hm, yeah dd does take a long time. The only thing is the main application for this for me is to clone the system disk, while it's in the device (Raspberry Pi). Will dd mess up if either of the disks are in use?
    – MattR
    Commented Mar 16, 2017 at 20:41
  • 1
    @MattR you can't copy a disk like this while it's in use Commented Mar 16, 2017 at 21:49
  • 1
    @MattR To clarify roaima's comment: if you copy a disk while it's in use, the copy process will succeed, but what it'll copy is an inconsistent state of the disk that's very likely to have corrupted data, either in obvious or non-obvious ways. Commented Mar 16, 2017 at 23:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .