title: Executing function inside chroot in bash
body: <p>What would be the ideal way to pass a function into a chroot from the host, in bash?</p>
  
  <p>For example, </p>
  
  <pre><code>install_script () {
      wget some_source_files &amp;&amp; configure &amp;&amp; make &amp;&amp; make install
  }
  </code></pre>
  
  <p>and, </p>
  
  <pre><code>some_command -v foo &gt;/dev/null 2&gt;&amp;1 || install_script
  </code></pre>
  
  <p>but if i want to execute the same from the host into chroot, How do i go about doing it? </p>
  
  <p>One way i can think of is to pass the function to a file inside the chrooted directory,</p>
  
  <pre><code>cat &gt; $chrooted_dir/etc/install_script.sh &lt;&lt;"EOF"
  
  #!/bin/bash
  wget source_files ; ./configure ; make ; make install
  
  EOF
  </code></pre>
  
  <p>and execute from host,</p>
  
  <pre><code>chroot $chrooted_dir /bin/bash "check_command  || /etc/install_script.sh"
  </code></pre>
  
  <p>But i am wondering if there is a more elegant way to approach this? Ideally, i would like to execute the commands from a cript in the host and have some installation performed inside chroot system. </p>
  
  <p>P.S: I would also appreciate of any relevant sources/links to understand bash handles function declarations and subsequent inherits on chrooting. </p>