I have below bash script, I am a bit confuse about what is $opt meaning. and I do not find detail information about it after search.
test.sh:
echo "opt:" $opt
for opt; do
echo $opt
done
output:
./test.sh -a -b c
opt:
-a
-b
c
I have below bash script, I am a bit confuse about what is $opt meaning. and I do not find detail information about it after search.
test.sh:
echo "opt:" $opt
for opt; do
echo $opt
done
output:
./test.sh -a -b c
opt:
-a
-b
c
$opt
is not a built-in or special variable. If it's mentioned in the script (outside offor opt
without thein something
part, which @mhawke already explained) it should either be defined earlier in the script or it's expected that it isexport
ed before running the script.From the bash(1) man page:
So, if the
in
word is missing the for loop iterates over the positional arguments to the script, i.e.$1
,$2
,$3
, ....There is nothing special about the name
opt
, any legal variable name can be used. Consider this script and its output:the
for
statement goes generally like this:$x
has the valuea
on the first iteration, thenb
, thenc
. thea b c
part need not be written out literally, the list to iterate through can be given by a variable, like the$@
, which has the list of all arguments to the current scope:further,
in "$@"
is assumed if you provide no list explicitly: