Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # You can iterate through a sequence of values by doing the following
- for n in 1 2 3 4 5
- do
- echo $n
- done
- # You can also iterate through an array variable, such as 'a', by doing the following
- a=(2 "Hello" 44 552)
- for i in "${a[@]}"
- do
- echo $i
- done
- # You can generate a sequenced list and iterate through it as well
- for i in {1..10..2}
- do
- echo $i # Print "12345678910"
- done
- # You can use a Java-like for loop that uses the following format
- # (( initialEvent; comparison, operation ))
- for (( a=0; a<10; a++ ))
- do
- echo $a
- done
- # You can iterate through commands with the following
- for i in pwd ls "ls /"
- do
- $i # If a variable is just being accessed alone like this, then it is treated like a command
- # This is because Bash just drops in the value of 'i' before executing this line
- done
- # You can use wildcards to generate lists to iterate through file names
- for fileName in *
- do
- echo $fileName
- done
- # Iterate through all text file names
- for fileName in *.txt
- do
- echo $fileName
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement