[TAG] Space in Directory Names
jj at franjam.org.uk
Thu Feb 7 14:50:41 MSK 2008
On Thu, 7 Feb 2008, Amit Kumar Saha wrote:
> On 2/7/08, Ben Okopnik <ben at linuxgazette.net> wrote:
>> On Thu, Feb 07, 2008 at 12:26:20PM +0530, Amit Kumar Saha wrote:
>>> On 2/7/08, Ben Okopnik <ben at linuxgazette.net> wrote:
>>>> On Thu, Feb 07, 2008 at 10:44:23AM +0530, Amit Kumar Saha wrote:
>>>>> Is there any other way other to deal with spaces?
>>>> Sure - use Bash completion.
>>> Yes, but this fails if I have a directory name, such as 'Book'.
>> Really? It works fine for me.
> I have 2 directories - 'Book' and 'Book Reviews'
> $ cd Book
> when I do this and press TAB, I get:
> Book/ Book Reviews/
> Now I do this,
> $ cd Book R <TAB> <TAB> <TAB>.....
> pressing any number of TABS doesn't show up anything
> Am I missing something?
The shell, bash more than likely, interprets the line you type. Ordinarily
it treats the space character as the seperator between arguments on the
command line. It also treats some other characters in a special way too,
e.g. '$' - it assumes you are accessing a shell variable.
To stop bash from treating the ' ' or the '$' or anyother special
character as special you must somehow tell the shell that in this case it
is not special. You can do that for a single character by prefix the
character '\' before the otherwise special character.
e.g. echo 1 2 3
1 2 3
echo 1\ \ \ \ \ \ \ \ \ 2\ \ \ 3
1 2 3
try echo $HOME and echo \$HOME
So if you have spaces (or e.g. '$'s) in your filesnames you needs to
prefix, or "escape", them with a '\'.
In Tab completion, if you have directories Book and Book\ Reviews, then
if you enter
nothing happens because there are 2 entries that could begin like that.
To get the longer directory you must enter a space, but you must "escape"
it with a '\'
ls Book\ <tab>
will do the completion for you.
Another way to stop the shell from treating some special characters as
special, is to quote with double quotes "...s t r i n g...". However some
characters are still special with double quotes,
e.g. echo "Home directory is $HOME"
still works - the HOME environment variable is still substituted.
Yet another way is to quote using single quotes '...$ t r i n g ....'.
This stops the shell from messing with the string at all. No variable
substitutions, no nothing.
There is chapter and verse on this in the bash manual page
Then search for the quoting section
it explains it all in glorious technical colour.
More information about the TAG