How do I echo “-e”?

后端 未结 6 1832
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 02:24

I want to echo a string that might contain the same parameters as echo. How can I do it without modifying the string?

For

6条回答
  •  再見小時候
    2020-12-07 02:47

    The proper bash way is to use printf:

    printf "%s\n" "$var"
    

    By the way, your echo didn't work because when you run:

    var="-e something"
    echo $var
    

    (without quoting $var), echo will see two arguments: -e and something. Because when echo meets -e as its first argument, it considers it's an option (this is also true for -n and -E), and so processes it as such. If you had quoted var, as shown in other answers, it would have worked.

提交回复
热议问题