1

when i type ls or locate as an output i get "a file.txt" (there is a space between "a" and "file.txt".

i highlight it with a mouse and than paste it with a middle button.

now i have to put \ after "a" so that "a file.txt" can be read in a command such as "ls a\ file.txt" now if the file name is a name of some book that has a lot of spaces it takes time to modify the name. is there a way to do this quick?

3 Answers 3

4

You can prepend a quote before you paste then close it afterwards. So you'd start your command

ls "

Then paste your text

ls "a file.txt

Then close the quote and hit enter

ls "a file.txt"

instead of

ls a\ file.txt

You don't have to escape every space if you quote the string.

Note: You can use single quotes too and that will prevent variable expansion if you get something like a file named $test

ls '$test'

would not be the same as

ls "$test"

With an empty variable, the second example will list the current directory, not just the file $test

1
  • Nice, I didn't know this works :)
    – Jin
    Sep 23, 2011 at 3:08
0

The backslash is used as an escape character for the shell to treat that space that comes after literally. Try replacing the spaces with underscores or dashes.

2
  • i'm using existing file names that i get from internet. they already have a space character Sep 23, 2011 at 3:11
  • Don't give in to the oppression of the shell! Use human-readable names and quote them appropriately. Computers exist to serve humans, not the other way around. :-)
    – Chris Page
    Sep 26, 2011 at 14:42
0

Some terminal emulators (e.g., Mac OS X Terminal) have a command for pasting text with quoting. In Terminal it's Edit > Paste Escaped Text; it inserts backslashes before special characters. Your terminal program may have a similar command.

You must log in to answer this question.

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