Linux,Shell,bash的for循环笔记221112

220 阅读2分钟

在Ubuntu22.04.01Desktop环境下测试

for

 for ((i=0;i<10;i++));do echo $i;done

等效

for ((i=0;i<10;i++))
do echo $i
done

for((i=0;i<10;i++));doechofor ((i=0;i<10;i++));do echoi;done 0 1 2 3 4 5 6 7 8 9

z@ud224:~$ for ((i=0;i<10;i++));do echo $i;done
0
1
2
3
4
5
6
7
8
9
z@ud224:~$ for ((i=0;i<10;i++))
> do echo $i
> done
0
1
2
3
4
5
6
7
8
9
z@ud224:~$ for((i=0;i<10;i++));do echo $i;done
0
1
2
3
4
5
6
7
8
9
z@ud224:~$ for (( i=0 ; i<10 ; i++ )) ; do echo $i ; done
0
1
2
3
4
5
6
7
8
9
z@ud224:~$ $ for (( i = 0 ; i < 10 ; i++ )) ; do echo $i ; done
bash: 未预期的符号“(”附近有语法错误

for in

for v in a b c 1 2 3 ; do echo $v ; done

for v in a b c 1 2 3 ; do echo $v ; done a b c 1 2 3

root@ud224:~# for v in a b c 1 2 3 ; do echo $v ; done
a
b
c
1
2
3
列出文件夹的文件
 for v in $( ls / ) ; do echo $v ; done

for v in (ls/);doecho( ls / ) ; do echo v ; done bin boot cdrom dev etc him home lib lib32 lib64 libx32 media mnt opt proc root run sbin snap srv swapfile sys tmp usr var

for v in $( ls / ) ; do echo $v ; done
bin
boot
cdrom
dev
etc
him
home
lib
lib32
lib64
libx32
media
mnt
opt
proc
root
run
sbin
snap
srv
swapfile
sys
tmp
usr
var

`for in`` 会按空格分割内容
root@ud224:~# for v in $(cat /etc/apt/sources.list) ; do echo $v ; done
#
deb
cdrom:[Ubuntu
22.04.1
LTS
_Jammy
Jellyfish_
-
Release
amd64
(20220809.1)]/
jammy
main
restricted
#
See
http://help.ubuntu.com/community/UpgradeNotes
for
how
to
upgrade
to
#
newer
versions
of
the
distribution.
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy
main
restricted
#
deb-src
http://cn.archive.ubuntu.com/ubuntu/
jammy
main
restricted
##
Major
bug
fix
updates
produced
after
the
final
release
of
the
##
distribution.
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy-updates
main
restricted
#
deb-src
http://cn.archive.ubuntu.com/ubuntu/
jammy-updates
main
restricted
##
N.B.
software
from
this
repository
is
ENTIRELY
UNSUPPORTED
by
the
Ubuntu
##
team.
Also,
please
note
that
software
in
universe
WILL
NOT
receive
any
##
review
or
updates
from
the
Ubuntu
security
team.
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy
universe
#
deb-src
http://cn.archive.ubuntu.com/ubuntu/
jammy
universe
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy-updates
universe
#
deb-src
http://cn.archive.ubuntu.com/ubuntu/
jammy-updates
universe
##
N.B.
software
from
this
repository
is
ENTIRELY
UNSUPPORTED
by
the
Ubuntu
##
team,
and
may
not
be
under
a
free
licence.
Please
satisfy
yourself
as
to
##
your
rights
to
use
the
software.
Also,
please
note
that
software
in
##
multiverse
WILL
NOT
receive
any
review
or
updates
from
the
Ubuntu
##
security
team.
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy
multiverse
#
deb-src
http://cn.archive.ubuntu.com/ubuntu/
jammy
multiverse
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy-updates
multiverse
#
deb-src
http://cn.archive.ubuntu.com/ubuntu/
jammy-updates
multiverse
##
N.B.
software
from
this
repository
may
not
have
been
tested
as
##
extensively
as
that
contained
in
the
main
release,
although
it
includes
##
newer
versions
of
some
applications
which
may
provide
useful
features.
##
Also,
please
note
that
software
in
backports
WILL
NOT
receive
any
review
##
or
updates
from
the
Ubuntu
security
team.
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy-backports
main
restricted
universe
multiverse
#
deb-src
http://cn.archive.ubuntu.com/ubuntu/
jammy-backports
main
restricted
universe
multiverse
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy-security
main
restricted
#
deb-src
http://security.ubuntu.com/ubuntu
jammy-security
main
restricted
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy-security
universe
#
deb-src
http://security.ubuntu.com/ubuntu
jammy-security
universe
deb
https://mirrors.ustc.edu.cn/ubuntu/
jammy-security
multiverse
#
deb-src
http://security.ubuntu.com/ubuntu
jammy-security
multiverse
#
This
system
was
installed
using
small
removable
media
#
(e.g.
netinst,
live
or
single
CD).
The
matching
"deb
cdrom"
#
entries
were
disabled
at
the
end
of
the
installation
process.
#
For
information
about
how
to
configure
apt
package
sources,
#
see
the
sources.list(5)
manual.