My script creates a chroot cage to install GRUB to a USB, run as sudo of course:
SYSTEM_DIRS=(etc bin sbin var lib lib64 usr proc sys dev tmp)
boot_partition=/media/user/boot
for dir in ${SYSTEM_DIRS[@]}; do
mount --bind /$dir ${boot_partition}/${dir}
done
Then execute some commands inside the the chroot:
chroot ${boot_partition}/ touch foo # works fine
...
But when I want to execute the command exit
chroot ${boot_partition}/ exit
I get:
chroot: failed to execute the command <<exit>>: No such file or directory
Why does this happen and there is a way to fix it?
chrootcommand changes the root directory for e.g.touch foo(or whatever command you execute), but it does not change it for the script, so "exiting it" makes no sense.exitis a shell built-in, and there is no external equivalent utility, so it can't execute it.(chroot)root@foo:/# exitchroot. That shell has to exit. Extiing the chrooted shell will give control back to the non-chrooted shell session. Thechrootcommand only affects the command that it runs. There is no indication in your question that you runchroot "${boot_partition}/" bash.