user1519111
user1519111

Reputation: 3

Bash issue with process substitution in a variable

I'm trying to get a dd command in a bash script going with the "if" set to a variable. The variable may be /dev/zero or may be as complicated as <(yes $'\01' | tr -d "\n")

I can seem to get the latter to work.

write_file=/dev/zero
dd if=$write_file bs=2048 seek=1 of=/dev/sda count=524288

That works.

write_file="<(yes $'\01' | tr -d "\n")"
dd if=$write_file bs=2048 seek=1 of=/dev/sda count=524288

This doesn't work. I get a

dd: invalid option -- 'd'
Try `dd --help' for more information.

But If I do a straight

dd if=<(yes $'\01' | tr -d "\n") bs=2048 seek=1 of=/dev/sda count=524288

in the script it works fine.

I'm sure there is some escaping that needs to be done but I'm no where near a bash expert to figure out how to do this!

Update: Tried from suggestions below

write_file="<(yes $'\01' | tr -d \"\n\")"
dd if="$write_file" bs=2048 seek=1 of=/dev/sda count=524288

and got

dd: opening `<(yes $\'\\01\' | tr -d "\\n")': No such file or directory

Upvotes: 0

Views: 1092

Answers (2)

user1519111
user1519111

Reputation: 3

Ok.. I guess I needed an "eval" in front of the dd.

so:

write_file="<(yes $'\x5a' | tr -d \"\n\")"
eval dd if=$write_file bs=2048 seek=1 of=/dev/sda count=5242

Works! I guess it re-parses after it does the variable substitution or something like that.

I got the hint from here

The funny thing is I tried using eval yesterday but I couldn't get it to work. I must have been missing some quoting or escaping somewhere..

Upvotes: 0

user1461760
user1461760

Reputation:

Bash does not parse the content of your variable and pass it directly to dd, which has no idea of what to do with it. You can force Bash to parse the variable using "eval":

 write_file="<(yes $'\01' | tr -d \"\n\")"
 command="dd if=$write_file bs=2048 seek=1 of=/dev/sda count=524288"
 eval "${command}"

Upvotes: 1

Related Questions