title: How do I execute the arguments to a bash function as root using the su command?
body: <p>I'm writing a script wherein I have a function that runs commands as root. This is so that it can use either sudo, doas, or su depending on what the user has installed. The function looks something like this:</p>
  
  <pre><code>as_root() {
      # $SU_CMD contains either sudo, doas --, or nothing
      if [[ -n $SU_CMD ]]; then
          $SU_CMD $@
      else
          su root -- $@
      fi
  }
  </code></pre>
  
  <p>This does not work if neither sudo nor doas are available. Rather than executing the command as root, <code>su root -- &lt;command&gt;</code> writes <code>/usr/bin/&lt;command&gt;: /usr/bin/&lt;command&gt;: cannot execute binary file</code> to the terminal. I also tried <code>su -c "$@"</code> among other combinations, but no dice. </p>
  
  <p>How would I go about executing the arguments to a bash function as root using su?</p>