Linux command
in 命令
文本
涉及管道、覆盖或删除,执行前请先确认路径和参数。
常用示例
Iterate over a list of words
for fruit in apple banana cherry; do echo "$fruit"; done
Iterate over a glob
for f in *.txt; do echo "$f"; done
Iterate over command substitution
for user in $(cat users.txt); do echo "$user"; done
Iterate over a brace-expansion range
for i in {1..10}; do echo "$i"; done
Interactive menu with select
select opt in start stop quit; do echo "$opt"; done
说明
in is a reserved word of the POSIX shell grammar, used as a delimiter between the loop variable and the word list in for and select compound commands and in case statements. It is not a standalone program and cannot be invoked directly; shells such as bash, zsh, dash, and ksh parse it as part of the surrounding control structure. Inside a for loop, the variable named before in is assigned successively to each word produced by the list after in (which may be literal words, glob expansions, command substitutions, or parameter expansions). When the word list is omitted entirely, the loop iterates over the positional parameters "$@".
FAQ
What is the in command used for?
in is a reserved word of the POSIX shell grammar, used as a delimiter between the loop variable and the word list in for and select compound commands and in case statements. It is not a standalone program and cannot be invoked directly; shells such as bash, zsh, dash, and ksh parse it as part of the surrounding control structure. Inside a for loop, the variable named before in is assigned successively to each word produced by the list after in (which may be literal words, glob expansions, command substitutions, or parameter expansions). When the word list is omitted entirely, the loop iterates over the positional parameters "$@".
How do I run a basic in example?
Run `for fruit in apple banana cherry; do echo "$fruit"; done` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
Where can I find more in examples?
This page includes 5 examples for in, plus related commands for nearby Linux tasks.