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

I'm trying to write a bash function for my bash profile that downloads YouTube Audio without any verbosity in stderr or stdout but I get the following compilation error:

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

I'm trying a variant of the top answer from Passing parameters to a Bash function but I can't get it to work.

4
  • 1
    Functions do not take a parameter list in their definition. And the keyword 'function' is redundant. Just write: y_dl() { ... } (And don't use a hyphen in the name) Commented Mar 2, 2018 at 18:56
  • Within the function, parameters are referenced by number, but you can name them explicitly. local URL=$1 Commented Mar 2, 2018 at 18:57
  • Re: the function keyword, see wiki.bash-hackers.org/scripting/obsolete Commented Mar 2, 2018 at 19:03
  • ...that linked-to answer doesn't show the () string being modified. Why are you modifying it here? Commented Mar 2, 2018 at 19:14

1 Answer 1

2

The correct and modern way :

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

function construction never takes arguments in a signature style Ex: func(foo, bar) it's shell

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.