Command to escape a string in bash

后端 未结 3 1722
遥遥无期
遥遥无期 2020-11-30 21:14

I need a bash command that will convert a string to something that is escaped. Here\'s an example:

echo \"hello\\world\" | escape | someprog
3条回答
  •  忘掉有多难
    2020-11-30 21:34

    In Bash:

    printf "%q" "hello\world" | someprog
    

    for example:

    printf "%q" "hello\world"
    hello\\world
    

    This could be used through variables too:

    printf -v var "%q\n" "hello\world"
    echo "$var"
    hello\\world
    

提交回复
热议问题