ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to create an array containing all ASCII characters, how do I create one:
Code:
#!/bin/bash
CHARLIST=( 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 \
0 1 2 3 4 5 6 7 8 9 \
~ ! @ # $ % ^ & * ( ) _ + ` - = \
{ } | [ ] \ : " ; ' < > ? , . / \
)
for char in ${CHARLIST[@]}
do
echo ${char}
done
exit 0
I am having problems with the # character and the characters of the last line of the array.
#!/bin/bash
set -f
CHARLIST=( 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 0 1 2 3 4 5 6 7 8 9 \~ \! \@ \# \$ \% \^ \& \* \( \) \_ \+ \` \- \= \{ \} \| \[ \] \\ \: \" \; \' \< \> \? \, \. \/ )
for char in "${CHARLIST[@]}"
do
echo "${char}"
done
exit 0
First of all, I got rid of the line-breaks, which made it confusing to work with. Then "set -f" turns off globbing in bash so * doesn't expand to the directory contents. (Edit: quoting "${CHARLIST[@]}" in the loop means not having to fool with the globbing setting.) Finally backslashing all of the non-alphanumeric characters avoids any shell interpretation (probably not all of them need escaping, but it doesn't hurt).
Last edited by David the H.; 02-18-2010 at 08:48 AM.
Reason: update
@pixellany: If you put $char between double quotes the * is not expanded: while read char; do echo "$char"; done < asc
@the_gripmaster: Did you have a look at pixellany's comment? Your array has all sorts of characters that need to be escaped (the CHARLIST variable won't be filled because of this as stated by the errors shown).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.