Ubuntu – Alias nested string

aliasbash

I have an alias for youtube_dl which adds some arguments in my .bashrc file. I want to run this in a separate terminal similar to how it is done here. The problem is that this takes a string as input.

my current alias:

alias youtube-dl="youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s'"

how I want the new alias to look like:

alias youtube-dl='gnome-terminal -e "youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s'"'

The problem is, however, that now the '-strings are interpreted as two separate strings. In addition, I am now unable to add the url as an argument. How do I circumvent this?

Best Answer

You could mess around with quotes and escaping, but I prefer to look for ways to reduce the quoting levels. For example, you could use gnome-terminal -x instead:

-e, --command=STRING
         Execute the argument to this option inside the terminal.

-x, --execute
         Execute  the  remainder  of  the  command  line  inside   the
         terminal.

So,

gnome-terminal -e "youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s'"

Becomes:

gnome-terminal -x youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s'

Shaving off one layer of quotes. And the alias would be:

alias youtube-dl='gnome-terminal -x youtube-dl -ci --restrict-filenames -o "%(title)s.%(ext)s"'

Or you could use a function:

youtube-dl()
{
    gnome-terminal -x youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s' "$@"
}
Related Question