wiki.bash-hackers.org/syntax/ccmd/user_select.md

52 lines
1.3 KiB
Markdown
Raw Normal View History

2023-04-15 13:23:49 +02:00
====== User selections ======
===== Synopsis =====
2023-04-15 13:53:05 +02:00
<code>
select <NAME>; do
<LIST>
2023-04-15 13:23:49 +02:00
done
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
2023-04-15 13:53:05 +02:00
<code>
select <NAME> in <WORDS>; do
<LIST>
2023-04-15 13:23:49 +02:00
done
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
2023-04-15 13:53:05 +02:00
<code>
2023-04-15 13:23:49 +02:00
# alternative, historical and undocumented syntax
2023-04-15 13:53:05 +02:00
select <NAME>
2023-04-15 13:23:49 +02:00
{
2023-04-15 13:53:05 +02:00
<LIST>
2023-04-15 13:23:49 +02:00
}
2023-04-15 13:53:05 +02:00
select <NAME> in <WORDS>
2023-04-15 13:23:49 +02:00
{
2023-04-15 13:53:05 +02:00
<LIST>
2023-04-15 13:23:49 +02:00
}
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
===== Description =====
2023-04-15 13:53:05 +02:00
This compound command provides a kind of menu. The user is prompted with a //numbered list// of the given words, and is asked to input the index number of the word. If a word was selected, the variable ''<NAME>'' is set to this word, and the [[syntax:basicgrammar#lists | list]] ''<LIST>'' is executed.
2023-04-15 13:23:49 +02:00
2023-04-15 13:53:05 +02:00
If no ''in <WORDS>'' is given, then the positional parameters are taken as words (as if ''in &quot;$@&quot;'' was written).
2023-04-15 13:23:49 +02:00
Regardless of the functionality, the //number// the user entered is saved in the variable ''REPLY''.
2023-04-15 13:53:05 +02:00
Bash knows an alternative syntax for the ''select'' command, enclosing the loop body in ''{<nowiki>...</nowiki>}'' instead of ''do <nowiki>...</nowiki> done'':
<code>
2023-04-15 13:23:49 +02:00
select x in 1 2 3
{
echo $x
}
2023-04-15 13:53:05 +02:00
</code>
2023-04-15 13:23:49 +02:00
This syntax is **not documented** and should not be used. I found the parser definitions for it in 1.x code, and in modern 4.x code. My guess is that it's there for compatiblity reasons. This syntax is not specified by POSIX(R).
===== Examples =====
===== Portability considerations =====
===== See also =====