0

For the life of me, I cannot figure out how to chroot through a script, and assign a value to a variable. Okay, so I learned how to chroot and execute commands from a script without halting the flow of the script:

#!/bin/bash
# Mini script for chrooting into chroot_dir and executing commands.

chroot_dir=$1
cat << EOF | chroot $chroot_dir /bin/bash
sysname=\`hostname\`
echo "You are running script.sh on ${sysname}"
EOF

Running this script, ./script.sh "/chroot_dir/", I get the following output:

You are running script.sh on

It seems like either sysname never gets assigned, or sysname loses its value immediately. How do I assign a value to sysname using this method to chroot?

1

The reason is that ${sysname} gets evaluated when the script is read, rather than when later executed. If you put a backslash (\) before the dollar ($) it should work better. Note that it has nothing to do with chroot, but it rather is a feature of bash.

  • Whoa! I totally forgot about that... Thanks! – SpaghettiCoder Feb 28 '16 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.