![別名嵌套字串](https://rvso.com/image/1087104/%E5%88%A5%E5%90%8D%E5%B5%8C%E5%A5%97%E5%AD%97%E4%B8%B2.png)
我有一個alias
for在我的文件youtube_dl
中添加了一些參數。.bashrc
我想在一個單獨的終端中運行它,類似於它的完成方式這裡。問題是這需要一個字串作為輸入。
我目前的別名:
alias youtube-dl="youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s'"
我希望新別名看起來像這樣:
alias youtube-dl='gnome-terminal -e "youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s'"'
然而,問題是,現在 -strings'
被解釋為兩個單獨的字串。此外,我現在無法添加 url 作為參數。我該如何規避這個問題?
答案1
您可以亂用引號和轉義,但我更喜歡尋找減少引用等級的方法。例如,您可以使用gnome-terminal -x
反而:
-e, --command=STRING
Execute the argument to this option inside the terminal.
-x, --execute
Execute the remainder of the command line inside the
terminal.
所以,
gnome-terminal -e "youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s'"
變成:
gnome-terminal -x youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s'
去掉一層引號。別名是:
alias youtube-dl='gnome-terminal -x youtube-dl -ci --restrict-filenames -o "%(title)s.%(ext)s"'
或者你可以使用一個函數:
youtube-dl()
{
gnome-terminal -x youtube-dl -ci --restrict-filenames -o '%(title)s.%(ext)s' "$@"
}