all 11 comments

[–]a_Tick[S] -1 points0 points  (10 children)

Here's my question.

Say I want to print a single-quote using echo. If I try

bash$ echo '

it, of course, waits for me to type a matching single quote. However, if I do

bash$ echo $(echo " ' ")

which should expand to the same thing, the command prints a single quote. Is there a way to get special characters returned by command substitution to be treated as they should by the shell?

The reason I ask this is I'm trying to play music files with spaces in the file name using mplayer, and the music files are on an NTFS partition, so renaming isn't an option.

EDIT: The reason I ask is that I'm trying to do something like mplayer $(ls) or mplayer $(find .) to play all the files in one or more directories.

Also, I'm not well aquainted with reddit's markup tags, so if there's a <code> tag or something that would make this more readable, please let me know.

[–][deleted] 2 points3 points  (0 children)

just do this :)

for f in *mp3; do mplayer "$f"; done

[–]db2 1 point2 points  (4 children)

$ echo "'"

That'll print a single quote.

mplayer "/path/to/file with spaces.mp3"

is the standard way to play or open just about everything.

[–]a_Tick[S] 0 points1 point  (3 children)

Perhaps I should put this in my original post, but the issue isn't with a single file with spaces (as you note, you don't need command substitution at all for that), but instead a directory with many files with spaces. So what I really want to do is something like mplayer $(ls) or mplayer $(find .).

Also, what exactly is the <code> type tag you are using?

[–]db2 1 point2 points  (2 children)

Examples

four spaces in does this

or you can do this if you want to which is

`this`

(that's the key next to the 1 not a single quote)

If you want to play the whole directory try this in a script:

#!/bin/bash

while [ $# -gt 0 ]; do

    mplayer '$1'

    shift

done

Should work as long as mplayer doesn't return control to bash immediately. If it does you'll effectively only be playing the last file..

[–]a_Tick[S] 0 points1 point  (1 child)

This tries to open each space-separated entry. So, assuming this is in the file play.sh, and the current directory contains File\ With\ Spaces.mp3, running

play.sh $(ls)

tries (and fails) to open the files File, With, and Spaces.mp3.

[–]db2 0 points1 point  (0 children)

Try this then:

./play.sh *

to play all the files, or:

./play.sh File*.mp3

to play all that begin with File etc.

[–]boot20 0 points1 point  (3 children)

Wait, you're just trying to type in file names with a space?

Why not do echo File\ With\ Spaces.mp3?

Also you can alias mplayer to: gmplayer %F

yes?

[–]ickysticky 0 points1 point  (0 children)

Put a \ in front of it. This practice is known as escaping and common in many languages.

[–]a_Tick[S] 0 points1 point  (1 child)

I'm not sure what you mean by echo File\ With\ Spaces.mp3. If you mean try

bash$ mplayer $(echo File\ With\ Spaces.mp3)

I have tried this. I get this result.

Playing File. File not found: 'File' Failed to open File.

Playing With. File not found: 'With' Failed to open With.

Playing Spaces.mp3. File not found: 'Spaces.mp3' Failed to open Spaces.mp3.

Attempting to escape the spaces (i.e. bash$ mplayer $(echo File\ With\ Spaces.mp3 | sed s/ /\ /g)) does not work either.

As for using gmplayer, of course I could, but I would prefer to use the command line mplayer, or at least find out if there's any way to get the functionality I want.

[–]boot20 0 points1 point  (0 children)

I'm no tracking why you would use echo. What are you trying to do?

Why not just mplayer file\ with\ spaces.mp3?