3

I know we can print a sequence of letters of the Alphabet using echo command and experimented it a little by doing this.

Lowercase letter Sequence

$ echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z

Uppercase letter Sequence

$ echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Uppercase and Lowercase letter Sequence

$ echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z

Now this is the part that I don't get. See the special charcaters from Z to a. Here's a better view.

$ echo {a..Z}
a ` _ ^ ]  [ Z

I want to know

  1. How these special characters came here in between Uppercase and lowercase letters ?

  2. What does these special character sequence mean ?

  3. Also, How do I only print this sequence in-between ?

4

{A..z} expands according to the character encoding of your current locale, which most likely is ASCII or something close to it. If you run man ascii (in case you have this on your system) or have a look at an ASCII table you see these additional characters in-between Z and a (the same you get when running echo {Z..a} actually).

To get just upper/lowercase characters use

echo {A..Z} {a..z}

To get just those in-between you may use

typeset -a foo=($(echo {Z..a}))
echo ${foo[@]:1:((${#foo[@]} - 2))}

which creates an array first including the two characters just outside the boundary and then accesses all array elements besides the boundary characters.

PS: Figuring out why typeset -a foo=({Z..a}) doesn't work is left as an exercise to the reader :-)

  • How to print only this sequence through echo - ` _ ^ ] [ that is inbetween uppercase and lowercase letters ? – C0deDaedalus Jan 28 '18 at 9:38
  • @C0deDaedalus echo '` _ ^ ] [' – Kusalananda Jan 28 '18 at 9:42
  • 1
    You'll notice backslash is missing in there (suggesting you've tried that in bash instead of zsh) and is been replaced with an empty argument. And if you replace {A..z} with {A..z}x you get an error about an unclosed ` in bash. That's another side effect of brace expansion being done very early in bash contrary to other shells. – Stéphane Chazelas Jan 28 '18 at 10:44
  • typeset -a foo=({Z..a}) seems to work on Bash 4.3/4.4 (except for the backslash and backtick) – ilkkachu Jan 28 '18 at 11:05
  • Actually, foo is missing the backslash (ASCII 1C). Only this code seems to actually set all the characters in between ASCII Z and ASCII a: declare foo=('Z' '[' '\' ']' '^' '_' '' 'a'); declare -p foo`. – Isaac Jan 28 '18 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.