简体   繁体   English

在 bash 中运行 echo 时获取斜杠是目录错误

[英]Getting slash is a directory error while running echo in bash

I am trying to output following ascii character art every time my terminal loads:每次我的终端加载时,我都试图输出以下 ascii 字符艺术:

   __  __                  _
  / / / /___  ____ _____ _(_)
 / / / / __ \/ __ / /
/ /_/ / / / / /_/ / /_/ / /
\____/_/ /_/\__,_/\__, /_/
                 /____/

So I added following at the end of .bashrc :所以我在.bashrc的末尾添加了以下内容:

echo "   __  __                  _ 
  / / / /___  ____ _____ _(_)
 / / / / __ \/ __ `/ __ `/ / 
/ /_/ / / / / /_/ / /_/ / /  
\____/_/ /_/\__,_/\__, /_/   
                 /____/      "

And it prints:它打印:

在此处输入图片说明

It seems that it is misinterpreting slash in ascii art as escape sequence, that why it is printing -bash: /: Is a directory .似乎将 ascii 艺术中的斜杠误解为转义序列,这就是为什么它打印-bash: /: Is a directory How can I get rid of it?我怎样才能摆脱它? If I add something simple to .bashrc , like echo "Hello World!!"如果我向.bashrc添加一些简单的东西,比如echo "Hello World!!" , it does not print this error message. ,它不会打印此错误消息。

Use the single quote instead:改用单引号:

echo \
'   __  __                  _ 
  / / / /___  ____ _____ _(_)
 / / / / __ \/ __ `/ __ `/ / 
/ /_/ / / / / /_/ / /_/ / /  
\____/_/ /_/\__,_/\__, /_/   
                 /____/      
'

Try like this:像这样尝试:

text=(
    '   __  __                  _'
    '  / / / /___  ____ _____ _(_)'
    ' / / / / __ \/ __ `/ __ `/ /'
    '/ /_/ / / / / /_/ / /_/ / /'
    '\____/_/ /_/\__,_/\__, /_/'
    '                 /____/'
)

printf '%s\n' "${text[@]}"

For a multi-line text literal, a HereDoc is very appropriate:对于多行文本文字,HereDoc 非常合适:

# Use a quoted heredoc 'marker' to disable expansion of backticks
cat <<'LOGO'
   __  __                  _
  / / / /___  ____ _____ _(_)
 / / / / __ \/ __ `/ __ `/ /
/ /_/ / / / / /_/ / /_/ / /
\____/_/ /_/\__,_/\__, /_/
                 /____/
LOGO

Un-quoted heredoc marker:未引用的 heredoc 标记:

cat <<EOF 
hello `echo world`
EOF

Output:输出:

hello world

Quoted heredoc 'marker':引用 heredoc '标记':

cat <<'EOF'
hello `echo world`
EOF

Output:输出:

hello `echo world`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM