Domanda

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:

for option
do
    echo $option
done

However, I know little about what "option" is in 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! How to make "echo" print "-e" and "-n" as content strings?

È stato utile?

Soluzione

  1. option is just an arbitrary variable name. The for command will assign it to each argument as it iterates.

  2. Use printf instead of echo:


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

Altri suggerimenti

To answer the 2nd part of the question, the general solution is to use printf instead of echo. See How do I echo "-e"?

1 option is a variable. You could have called it i

for i
do
  echo "$i"
done

2 echo You could use

echo "-e -n hi"
-e -n hi
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top