pleasega pleasega - 7 months ago 10
Bash Question

Pass file contents to linux command arguments

I have the randomly generated password stored in

/tmp/mysqlpwd.txt


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

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).

Comments