代码之家  ›  专栏  ›  技术社区  ›  O.rka

使用参数[重复]创建bash函数时出现语法错误

  •  2
  • O.rka  · 技术社区  · 7 年前
    function y-dl ($URL) {
    cd ~/Music/
    youtube-dl -f bestaudio --prefer-ffmpeg --extract-audio --audio-format mp3 $URL > /dev/null 2>&1
    }
    

    我正在尝试为我的bash配置文件编写一个bash函数,该函数可以在stderr或stdout中下载YouTube音频,但我遇到以下编译错误:

    -bash: /Users/mu/.bash_profile: line 11: syntax error near unexpected token `$URL'
    -bash: /Users/mu/.bash_profile: line 11: `function y-dl ($URL) {'
    

    我在尝试一个来自 Passing parameters to a Bash function 但我不能让它工作。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Gilles Quénot ticktalk    7 年前

    正确而现代的方式:

    y-dl() {
        cd ~/Music/
        youtube-dl -f bestaudio --prefer-ffmpeg --extract-audio --audio-format mp3 "$1" &>/dev/null
    }
    

    函数构造从不在函数中使用参数 签名 样式Ex: func(foo, bar) 这是壳牌