0

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

  • 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 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 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 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 at 9:06
2

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"
  • Even after using echo "$video", i still get the error File name too long – josh Jun 7 at 20:10
  • if I remove the $,it gives me errors like this.. tmp.sh: line 4: syntax error near unexpected token youtube-dl' tmp.sh: line 4: $video=(youtube-dl -g -f bestvideo – josh Jun 7 at 20:13
  • Assigning it directly to video instead of $video works without the error – josh Jun 7 at 20:14
  • But,do you know why it doesn't work when I set video as an empty variable in the beginning and when I assign to it using $video=$(youtube-dl ...) – josh Jun 7 at 20:15
  • 1
    The error that the user gets is a "Filename too long" error. I'm not able to provoke this error from any of the involved components (bash and youtube-dl). – Kusalananda Jun 8 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.