Shell – Zsh function with su and echo

quotingshell-scriptzsh

I'm trying to add a function to my .zshrc that makes adding new USE flags to my /etc/portage/package.use file easier. Normally, I'd have to do

su -c 'echo "net-misc/aria2 bash-completion bittorrent" >> /etc/portage/package.use'

So I tried making a function like

new_use() {
    su -c 'echo "$1" >> /etc/portage/package.use'
}

but then I realized it wouldn't work. I want to know if there's a way of making this function work, or at least if functions are appropriate for this. By the way, I don't mind having to type the root password every time I call the function.

Best Answer

Just reverse the quotes.

new_use() {
    su -c "echo '$1' >> /etc/portage/package.use"
}

This will cause $1 to be expanded before the execution of su.