ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm playing around with a bash script to pass files to a program (such as VLC). I thought something like this would work:
vlc $( ls | sed 's/\ /\\ /g' | tr '\n' ' ' )
ls shows the files in the current directory, then sed changes spaces to "\ " (to escape them) and finally tr removes the line breaks. I end up changing:
01 - Music Track.mp3
02 - Another Track.mp3
to
01\ -\ Music\ Track.mp3 02\ -\ Another\ Track.mp3
"vlc 01\ -\ Music\ Track.mp3 02\ -\ Another\ Track.mp3" works if I type it manually but my script reports a problem with `-\'
I am not sure why this is not working. If you prepend your command with an echo the output looks ok. If you copy+paste the echoed line into the terminal again and hit enter then it executes just fine. It just does not do so when you try it as you have done. Maybe I will investigate this later. Anyway, if the '-' give you trouble then this might be because it is misinterpreted as an option. To bypass the funny command substitution issue this:
Code:
vlc -- *
Since you want to play all files in that directory. The '--' is to tell vlc that there are no more options to parse.
Yes, I had noticed that the output gets split up that way. You do not really need an extra script for that. I got the corresponding 'No such file or directory' error messages in vlc.
The interesting thing at this point, however, is why are the parameters handled this way? Is vlc responsible for splitting them up or is it bash? If it were vlc then this would happen with every filename that has a space in it, thus making it impossible for vlc to play files with spaces in them. Which leaves bash as the last suspect. My guess at this point is that after a command substitution bash does not parse the result. Apparently it simply splits up the resulting string at every space and feeds the tokens to vlc one by one - with the escape character '\'.
However, this behavior does not seem very conclusive at this point. Now it would be understandable that after a command substitution the result is untouched and passed as is to vlc. But this is not the case. It does process it to some degree, i.e. splitting it up. If it does some post-processing then why not do the whole thing and recognizing the escape sequences? Are there any good reasons not to? The way it deals with it seems like going only half-way. Is there a "switch" that can trigger an alternate behavior?
That being said I am open to explanations/corrections and/or good reasons why it has to be this way and not otherwise.
The interesting thing at this point, however, is why are the parameters handled this way? Is vlc responsible for splitting them up or is it bash?
vlc was never called in my example.
Quote:
It does process it to some degree, i.e. splitting it up. If it does some post-processing then why not do the whole thing and recognizing the escape sequences? Are there any good reasons not to?
Quote:
The order of expansions is: brace expansion, tilde expansion, parameter, variable, and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and filename expansion.
After the preceding expansions, all unquoted occurrences of the characters ‘\’, ‘'’, and ‘"’ that did not result from one of the above expansions are removed. Quote Removal
That is the current documented behaviour of bash. As for a "good" reason: I don't think bash (or sh, csh) is well designed, it seems like it just accumulates features as time goes on. It has to be this way because changing would break too many scripts.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.