Replace one character with another in Bash

前端 未结 6 855
逝去的感伤
逝去的感伤 2020-11-28 18:38

I need to be able to do is replace a space () with a dot (.) in a string in bash.

I think this would be pretty simple, but I\'m new so I ca

6条回答
  •  没有蜡笔的小新
    2020-11-28 19:09

    Try this for paths:

    echo \"hello world\"|sed 's/ /+/g'|sed 's/+/\/g'|sed 's/\"//g'
    

    It replaces the space inside the double-quoted string with a + sing, then replaces the + sign with a backslash, then removes/replaces the double-quotes.

    I had to use this to replace the spaces in one of my paths in Cygwin.

    echo \"$(cygpath -u $JAVA_HOME)\"|sed 's/ /+/g'|sed 's/+/\\/g'|sed 's/\"//g'
    

提交回复
热议问题