# scripting chroot



## Alain De Vos (May 3, 2021)

Sometimes I chroot into something and I do a repetitive task afterwards.
How do I script after chroot ?
I know the input, I can foresee the output.
Something very basic like "adduser" after chroot.


----------



## memreflect (May 4, 2021)

chroot(8) says:

```
Example 2: Execution of a Command with a Changed Root Directory

       The following command changes a root directory with chroot and then
       runs ls(1) to list the contents of /sbin.

         # chroot /tmp/testroot ls /sbin
```

For multiple commands, I'd recommend executing a script in the target root directory in most cases, as in `chroot /tmp/testroot sh /filename.sh`, particularly if you're dealing with text manipulation or if the use of '$', quote chars, and backslashes becomes problematic.


----------



## PMc (May 4, 2021)

```
# n="1 2 3"
# chroot /j sh -c 'for i in '"$n"'; do echo $i; done'
1
2
3
#
```

Basic rules:
1. "string" and 'string' can be glued together
2. Variables from the outside should be in such "$string".
3. Variables local within the chroot should be in such '$string'.


----------

