如何将“-e”分配给bash中的变量

Dud*_*ude 0 bash echo variable

我试图-e在 Bash 4 中分配给一个变量。但该变量仍然为空。

例如:

$ var="-e" 
$ echo $var

$ var='-e' 
$ echo $var

$ var="-t" 
$ echo $var
-t
Run Code Online (Sandbox Code Playgroud)

为什么它适用于-t,但不适用-e

ilk*_*chu 5

它可以工作,但是echo -e除非启用了posixxpg_echo选项,否则运行不会在 Bash 中输出任何内容,因为-e然后将其解释为选项:

$ help echo
echo: echo [-neE] [arg ...]
    Write arguments to the standard output.

    Display the ARGs, separated by a single space character and followed by a
    newline, on the standard output.

    Options:
      -n        do not append a newline
      -e        enable interpretation of the following backslash escapes
      -E        explicitly suppress interpretation of backslash escapes
Run Code Online (Sandbox Code Playgroud)

printf "%s\n" "$var"
Run Code Online (Sandbox Code Playgroud)

反而。

正如 cas 在评论中所指出的,Bash declare -p vartypeset -p var在 ksh、zsh 和 yash(和 bash)中)可用于告诉变量的确切类型和内容。

请参阅:为什么 printf 比 echo 好?