SHARE
TWEET

Untitled

quantumech May 18th, 2019 (edited) 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # You can iterate through a sequence of values by doing the following
  2. for n in 1 2 3 4 5
  3. do
  4.     echo $n
  5. done
  6.  
  7. # You can also iterate through an array variable, such as 'a', by doing the following
  8. a=(2 "Hello" 44 552)
  9.  
  10. for i in "${a[@]}"
  11. do
  12.     echo $i
  13. done
  14.  
  15. # You can generate a sequenced list and iterate through it as well
  16. for i in {1..10..2}
  17. do
  18.     echo $i # Print "12345678910"
  19. done
  20.  
  21. # You can use a Java-like for loop that uses the following format
  22. #   (( initialEvent; comparison, operation ))
  23. for (( a=0; a<10; a++ ))
  24. do
  25.     echo $a
  26. done
  27.  
  28. # You can iterate through commands with the following
  29. for i in pwd ls "ls /"
  30. do
  31.     $i # If a variable is just being accessed alone like this, then it is treated like a command
  32.        # This is because Bash just drops in the value of 'i' before executing this line
  33. done
  34.  
  35. # You can use wildcards to generate lists to iterate through file names
  36. for fileName in *
  37. do
  38.     echo $fileName
  39. done
  40.  
  41. # Iterate through all text file names
  42. for fileName in *.txt
  43. do
  44.     echo $fileName
  45. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top