0

I couldn't find a proper way to express myself fully, because of that I am deeply sorry if I make any mess.

My question is: How can I use letters a..z in a for loop in bash?

#Aim is to list commands are the directory /usr/bin
#!/bin/bash
cd /usr/bin
for i in {a..z}
array=($(ls --ignore=[!a]*)) #Just lists commands starting with "a"
echo ${array[@]}

#I tried to do a substitution in the below code, where may I be wrong?
#!/bin/bash
cd /usr/bin
for i in {a..z}
array=($(ls --ignore=[!$i]*))
echo ${array[@]}
jww
  • 97,681
  • 90
  • 411
  • 885
AreUMine
  • 21
  • 4
  • Are you actually running this script with bash, not some other shell? Note that the shebang line (`#!/bin/bash`) *must* be the first line in the file, and that if you run the script with `sh scriptname` it ignores the shebang and uses `sh` instead of `bash`. Also, your examples are missing the `done`, which will cause syntax errors in any shell. And `ls --ignore=[!$i]*` is a really strange and trouble-prone way to get a list of filenames that start with `$i`; just set `shopt -s nullglob`, then use `array=("$i"*)`. – Gordon Davisson Feb 05 '18 at 20:31
  • @GordonDavisson Exactly. I don't think this question should be marked as duplicate and this should be the answer :) – PesaThe Feb 05 '18 at 21:04
  • To list commands in */usr/bin* just do `ls /usr/bin`, or `echo /usr/bin/*`. – agc Feb 06 '18 at 06:55

1 Answers1

1

To loop through a to z use the following:

for x in {a..z}
do
    echo "$x"   # prints alphabet
done
agc
  • 7,973
  • 2
  • 29
  • 50
Akash Dathan
  • 4,348
  • 2
  • 24
  • 45