pleasega pleasega - 2 years ago 69
Bash Question

Pass file contents to linux command arguments

I have the randomly generated password stored in


I would like to set the contents of that file as the password in an automated installation.
I tried:

sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $(cat /tmp/mysql_passwd.txt)"

But the password just becomes
$(cat /tmp/mysql_passwd.txt)

I also tried using backticks around the
$(cat /tmp/mysql_passwd.txt)
but the password adds the backticks.

How do I pass the contents of the file to the command?

Answer Source

This code is already correct as-written (though it's putting content on stdin rather than arguments, that's what debconf-set-selections expects).

You can test this trivially:

echo "hello world" >file
cat <<<"content follows: $(cat file)"

...the output of the above being:

content follows: hello world

Incidentally, when your shell is known to be bash you might consider $(<file) as a more efficient replacement to $(cat file).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download