Bash quoting the current path (pwd)

I ran into the most annoying problem that occurs in a variable PWDwhen the current path contains a space. My code looks something like this:

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

This works fine if the current path does not contain a space. If so, $(pwd)expand to   

'mypath / with space'
not just   
mypath / with space

This will cause the sed expression to get corrupted (due to extra quotes):

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

I have noticed that it does not help to expand pwd follows: ${PWD//\'/}.

Any ideas on how this can be resolved?

+3
source share
2 answers

backquotes pwd:

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

.

+2

,

'$(pwd)'

'"$(pwd)"'

:

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

Source: https://habr.com/ru/post/1717052/


All Articles