简体   繁体   English

bash中的“for option”和bash中的echo

[英]“for option” in bash and echo in bash

I have two questions: 我有两个问题:

1.When I read some bash scripts, I found a way to iterate all the options passed to the current script: 1.当我阅读一些bash脚本时,我找到了一种迭代传递给当前脚本的所有选项的方法:

for option
do
    echo $option
done

However, I know little about what "option" is in bash. 但是,我对bash中的“选项”知之甚少。 Can anyone give me some references about that? 任何人都可以给我一些参考吗? Thanks. 谢谢。

2.Just as above, when I passed "-e" or "-n" options to my script, "echo" can't print them as a string because "echo" treats them as options! 2.如上所述,当我将“-e”或“-n”选项传递给我的脚本时,“echo”不能将它们打印为字符串,因为“echo”将它们视为选项! How to make "echo" print "-e" and "-n" as content strings? 如何使“echo”打印“-e”和“-n”作为内容字符串?

  1. option is just an arbitrary variable name. option只是一个任意变量名。 The for command will assign it to each argument as it iterates. for命令会在迭代时将其分配给每个参数。

  2. Use printf instead of echo : 使用printf而不是echo


for arg
do
    printf "%s" "$arg"
done

To answer the 2nd part of the question, the general solution is to use printf instead of echo . 要回答问题的第二部分,一般的解决方案是使用printf而不是echo See How do I echo "-e"? 请参阅如何回显“-e”?

1 option is a variable. 1 option是变量。 You could have called it i 你可以称之为i

for i
do
  echo "$i"
done

2 echo You could use 2 echo你可以使用

echo "-e -n hi"
-e -n hi

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

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