Question

I'm looking at some aliases to quickly save the current directory so it can be opened later.

ALIAS cdo='pwd|sed 's/ /\ /g' > ~/.cdo'
AIIAS cdn='cd "$(cat ~/.cdo)"'

The sed command obviously deals with spaces.

However, I still have a curious problem that I cannot fix that must involve variable interpretation.

jk@bt:~# cd Hello\ World/
jk@bt:~/Hello World# pwd|sed 's/ /\\ /g' > ~/.cdo
jk@bt:~/Hello World# cat ~/.cdo
/home/jk/Hello\ World
jk@bt:~/Hello World# cd ..
jk@bt:~# cd "$(cat ~/.cdo)"
bash: cd: /home/jk/Hello\ World: No such file or directory
Was it helpful?

Solution

Looks to me as if you're doubling the masking.

One way to handle spaces is to mask them individually with a backslash:

 cd here\ I\ go 

or to mask all of them with double quotes:

 cd "here I go" 

while this would be allowed as well:

 cd here" I "go

However, mixing them means, that you want literally backslashes:

 cd "here\ I\ go" 

and that's what's happening.

Note that while not very common, tabs and newlines can be included in files as well. Masking them works the same way, but reading them from a file might be different, and multiple blanks are often condensed by the shell to a single one.

OTHER TIPS

You have to escape with \ if the filename isn't between " and "

Escaping with cdo but not using " in cdn

ALIAS cdo='pwd|sed 's/ /\ /g' > ~/.cdo'
AIIAS cdn='cd $(cat ~/.cdo)'

Not escaping and using "" (I think this is better)

ALIAS cdo='pwd > ~/.cdo'
AIIAS cdn='cd "$(cat ~/.cdo)"'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top