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.
It works when the words between _ or . delimiters contain spaces too.
When IFS is unset, bash behaves as if it had the default value so unsetting it is a neater solution than saving it and restoring it unless it may be set to a non-default value.
User@term$ func() { i=0; while [[ $i != ${#1} ]]; do ARR[$i]="${1:$i:$((${#1}-$((${#1}-1))))}"; echo ${ARR[$i]}; let i=$i+1; done }
User@term$ func "This is a string"
T
h
i
s
i
s
a
s
t
r
i
n
g
Remember that 'func this is a string' and 'func "This is a string"' are two different things.
@Gussy - I am not sure what you mean? You created a function and then ran it with a string ... how is that helpful to this thread?
Also is there a reason for the convoluted length expression?
Code:
ARR[$i]="${1:$i:$((${#1}-$((${#1}-1))))}"
# As the length of the input is uniform and not adjusted anywhere else in the code so this is always
ARR[$i]="${1:$i:1}"
@Gussy - I am not sure what you mean? You created a function and then ran it with a string ... how is that helpful to this thread?
Also is there a reason for the convoluted length expression?
Code:
ARR[$i]="${1:$i:$((${#1}-$((${#1}-1))))}"
# As the length of the input is uniform and not adjusted anywhere else in the code so this is always
ARR[$i]="${1:$i:1}"
Opps -- yes; disregard my solution.
I'm very used to seeing people having problems with breaking up strings into characters.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.