2

I am storing a url inside a variable in bash. It has nothing to do with real file paths but it keeps giving me the error File name too long.

How do I tell bash to ignore looking for filenames and just store the long url string into the variable?

I am not using a file name. Can someone please explain and guide me on why I am getting that error that doesn't seem relevant?

#!/bin/bash
video=""
$video=$(youtube-dl -g -f bestvideo https://youtubeurl)

echo $video

$ Bash script.sh

error: File name too long

Edit : Apologies,I used $( ) before the command. I typed it wrongly here on the stackexchange editor.Corrected it above.Tried using echo "$video" . I still get the same error. I am using ubuntu on Windows Subsystem for Linux

4
  • It would have been nice if you had copied and pasted the actual command that you used, and the verbatim error that was produced. Errors are always preceded by the name of the utility that produced them, and I can't see what in your code would have generated a "Filename too long" error.
    – Kusalananda
    Jun 8, 2019 at 6:54
  • @Kusalananda This is the actual command. If you mean youtube-dl,this error was produced by plain bash. youtube-dl had nothing to do with it. It was a bash error.
    – josh
    Jun 8, 2019 at 8:08
  • It's just that you spelled bash with a capital B which lead me to believe that you were not in fact copying the actual command. Also, I can't provoke bash nor youtube-dl to produce that specific error ("No such file or directory" is very different from "Filename too long"). That's why I asked. I was just trying to reproduce your error and I couldn't. When I can't reproduce an error, I have to ask what you're doing, as I can't be sure you are actually showing everything exactly the way it happens.
    – Kusalananda
    Jun 8, 2019 at 9:01
  • What you may have done is to actually assign the output of youtube-dl to your variable with a proper assignment (not $video=...) and then added the $ to video somehow and tried it again. This would indeed have given that error. But again, this is not in the code that you show.
    – Kusalananda
    Jun 8, 2019 at 9:06

1 Answer 1

3

You have some issues with your script:

$video=youtube-dl -g -f bestvideo https://youtubeurl

This is not valid syntax. Variable assignments cannot begin with $ (as is common in some other languages). Additionally it appears you actually want to use command substitution there:

video=$(youtube-dl -g -f bestvideo https://youtubeurl)

Also when you echo $video you should quote it (as should you when using variables in almost every other scenario):

echo "$video"

Finally it is unnecessary to set video as an empty parameter at the beginning of your script although this wouldn't cause any issues.

Try your script as:

#!/bin/bash

video=$(youtube-dl -g -f bestvideo https://youtubeurl)
echo "$video"
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .