문제

I have encountered a most annoying problem that occurs on the PWD variable when the current path includes a space. My code looks somewhat like this:

mycommand |sed -E '  
 s|mystuff|replacement| ;  
 s|'$(pwd)'|replacement| ;  
 '

This works great, unless the current path contains a space character. If it does, $(pwd) is expanded to

'mypath/with space'
instead of just
mypath/with space

This cause the sed expression to be messed up (because of the extra quotes):

sed: 1: "s|mypath/with": unterminated substitute pattern

I have noticed that it doesn't help to expand pwd like this: ${PWD//\'/}.

Any ideas on how this can be solved?

도움이 되었습니까?

해결책

Replace single quotes by double quotes and replace quotes with backquotes around pwd:

mycommand | sed -E "
 s|mystuff|replacement| ;
 s|`pwd`|replacement| ;
"

Double quotes allow expansion of variables and backquoted commands.

다른 팁

what happens if you replace

'$(pwd)'

with

'"$(pwd)"'

would look like this then:

mycommand | sed -E '  
 s|mystuff|replacement| ;  
 s|'"$(pwd)"'|replacement| ;  
 '
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top