Bash: how do I pass a double quoted string to a command?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Bash: how do I pass a double quoted string to a command?
I'm trying to write a shell script to rip CDs using cdda2wav. I have the track names in a file and I'm reading these in and then creating a string like so:
Code:
#!/bin/bash
# Save stdin
exec 6<&0
# stdin is now tracks
exec < tracks
count=0
command1="cdda2wav -s -t"
while read line
do
let "count += 1"
command2="$command1 $count -x dev=/dev/cdrw \""$line\"
$command2
done
exec 0<&6 6<&-
If I echo command2, I get what I expect, e.g.
cdda2wav -s -t 1 -x dev=/dev/cdrw "Ghost Is Back".
This is what I'd usually use on the command line. The problem, however is that when the command is executed in my script, the file name output for the above cdda2wav example ends up being "Ghost.wav, i.e. there's a " at the beginning and the title isn't complete.
So you can see the double quotes are in the right place but weren't interpreted by bash. You need to use eval $command2, eval will cause bash to interpret things properly.
So you can see the double quotes are in the right place but weren't interpreted by bash. You need to use eval $command2, eval will cause bash to interpret things properly.
I'm not sure what the first part of your answer means, but using "eval $command2" works. Thanks!
Quick question: I don't really need to save stdin and restore it at the end, do I?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.