Splitting a string without delimiter and save it in an array [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite













This question already has an answer here:



  • Split single string into character array using ONLY bash

    4 answers



I would like to split three letters such as WER into three independent letters.
as follows:



W = array[0]
E = array[1]
R = array[2]


I tried the command



WER | cut -c1 but I could not save the new string W in a variable.



I tried



set var1 = WER | cut -c1 and it didn't work.










share|improve this question















marked as duplicate by don_crissti, thrig, Jeff Schaller, αғsнιη, Romeo Ninov Sep 6 at 4:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Why? For what purpose?
    – hhoke1
    Sep 5 at 21:22














up vote
1
down vote

favorite













This question already has an answer here:



  • Split single string into character array using ONLY bash

    4 answers



I would like to split three letters such as WER into three independent letters.
as follows:



W = array[0]
E = array[1]
R = array[2]


I tried the command



WER | cut -c1 but I could not save the new string W in a variable.



I tried



set var1 = WER | cut -c1 and it didn't work.










share|improve this question















marked as duplicate by don_crissti, thrig, Jeff Schaller, αғsнιη, Romeo Ninov Sep 6 at 4:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Why? For what purpose?
    – hhoke1
    Sep 5 at 21:22












up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:



  • Split single string into character array using ONLY bash

    4 answers



I would like to split three letters such as WER into three independent letters.
as follows:



W = array[0]
E = array[1]
R = array[2]


I tried the command



WER | cut -c1 but I could not save the new string W in a variable.



I tried



set var1 = WER | cut -c1 and it didn't work.










share|improve this question
















This question already has an answer here:



  • Split single string into character array using ONLY bash

    4 answers



I would like to split three letters such as WER into three independent letters.
as follows:



W = array[0]
E = array[1]
R = array[2]


I tried the command



WER | cut -c1 but I could not save the new string W in a variable.



I tried



set var1 = WER | cut -c1 and it didn't work.





This question already has an answer here:



  • Split single string into character array using ONLY bash

    4 answers







bash shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 5 at 22:34









Rui F Ribeiro

36.8k1273117




36.8k1273117










asked Sep 5 at 21:21









user309317

92




92




marked as duplicate by don_crissti, thrig, Jeff Schaller, αғsнιη, Romeo Ninov Sep 6 at 4:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by don_crissti, thrig, Jeff Schaller, αғsнιη, Romeo Ninov Sep 6 at 4:38


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Why? For what purpose?
    – hhoke1
    Sep 5 at 21:22
















  • Why? For what purpose?
    – hhoke1
    Sep 5 at 21:22















Why? For what purpose?
– hhoke1
Sep 5 at 21:22




Why? For what purpose?
– hhoke1
Sep 5 at 21:22










2 Answers
2






active

oldest

votes

















up vote
2
down vote













In bash:



str=WER

a=( "$str:0:1" "$str:1:1" "$str:2:1" )


or, as a loop over an arbitrarily long string:



str=WER

a=()
for (( i = 0; i < $#str; ++i )); do
a+=( "$str:i:1" )
done


$parameter:offset:length is a bash substring expansion that will result in the length number of characters of the string $parameter from offset offset.






share|improve this answer




















  • For your first approach it gives: Bad : modifier in $ (0) but also for 1,2 etc.
    – user309317
    Sep 6 at 12:31










  • @user309317, you used the "bash" tag in your question, but it looks like you're actually using csh or tcsh. Please clarify your question with details of which shell you're using.
    – Stéphane Chazelas
    Sep 6 at 15:01










  • @user309317 Make sure that you are actually using the bash shell (the question was tagged with bash).
    – Kusalananda
    Sep 6 at 16:00

















up vote
2
down vote













It's easier with zsh:



string=WER
array=($(s::)string)
printf '<%s>n' $array


(note that array indicies in zsh start at 1 like in most other shells, not 0 like in bash/ksh).



Or with fish (arrays also start at 1):



set string WER
set array (string split '' $string)
printf '<%s>n' $array


(assumes $string doesn't contain newline characters though).



POSIXly (the POSIX shell has one array: $@ (also starts at 1: $1)), so would also work in bash or zsh and your system's standard sh:



string=WER
set --
while [ -n "$string" ]; do
tmp=$string#?
set -- "$@" "$string%%"$tmp""
string=$tmp
done
printf '<%s>n' "$@"


With csh or tcsh which you seem to be using:



set string = WER
set array = "`printf '%sn' $string:q | fold -w1`"
printf '<%s>n' $array:q


Like for fish, it also assumes the string doesn't contain newline characters. Also note that some fold implementations will fold on bytes instead of characters which would mean it wouldn't work properly if the string contained multi-byte characters.






share|improve this answer






















  • When I try your first approach I always receive: Illegal variable name. Second one: Variable name must begin with a letter. Third one: set -- does not work. Sorry, but I am not really experienced with bash
    – user309317
    Sep 6 at 12:29










  • @user309317, I think you missed the with zsh and with fish which are different shells from bash. The last one should work with bash as indicated.
    – Stéphane Chazelas
    Sep 6 at 12:35










  • Thanks alot for your answer, but I still get the error: set: Variable name must begin with a letter when I try the POSIXIy
    – user309317
    Sep 6 at 14:09







  • 1




    @user309317, you tried the POSIXly in csh or tcsh, not bash nor any other POSIX shell.
    – Stéphane Chazelas
    Sep 6 at 14:11










  • Now it works. Is there also a possibility in csh?
    – user309317
    Sep 6 at 14:56

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













In bash:



str=WER

a=( "$str:0:1" "$str:1:1" "$str:2:1" )


or, as a loop over an arbitrarily long string:



str=WER

a=()
for (( i = 0; i < $#str; ++i )); do
a+=( "$str:i:1" )
done


$parameter:offset:length is a bash substring expansion that will result in the length number of characters of the string $parameter from offset offset.






share|improve this answer




















  • For your first approach it gives: Bad : modifier in $ (0) but also for 1,2 etc.
    – user309317
    Sep 6 at 12:31










  • @user309317, you used the "bash" tag in your question, but it looks like you're actually using csh or tcsh. Please clarify your question with details of which shell you're using.
    – Stéphane Chazelas
    Sep 6 at 15:01










  • @user309317 Make sure that you are actually using the bash shell (the question was tagged with bash).
    – Kusalananda
    Sep 6 at 16:00














up vote
2
down vote













In bash:



str=WER

a=( "$str:0:1" "$str:1:1" "$str:2:1" )


or, as a loop over an arbitrarily long string:



str=WER

a=()
for (( i = 0; i < $#str; ++i )); do
a+=( "$str:i:1" )
done


$parameter:offset:length is a bash substring expansion that will result in the length number of characters of the string $parameter from offset offset.






share|improve this answer




















  • For your first approach it gives: Bad : modifier in $ (0) but also for 1,2 etc.
    – user309317
    Sep 6 at 12:31










  • @user309317, you used the "bash" tag in your question, but it looks like you're actually using csh or tcsh. Please clarify your question with details of which shell you're using.
    – Stéphane Chazelas
    Sep 6 at 15:01










  • @user309317 Make sure that you are actually using the bash shell (the question was tagged with bash).
    – Kusalananda
    Sep 6 at 16:00












up vote
2
down vote










up vote
2
down vote









In bash:



str=WER

a=( "$str:0:1" "$str:1:1" "$str:2:1" )


or, as a loop over an arbitrarily long string:



str=WER

a=()
for (( i = 0; i < $#str; ++i )); do
a+=( "$str:i:1" )
done


$parameter:offset:length is a bash substring expansion that will result in the length number of characters of the string $parameter from offset offset.






share|improve this answer












In bash:



str=WER

a=( "$str:0:1" "$str:1:1" "$str:2:1" )


or, as a loop over an arbitrarily long string:



str=WER

a=()
for (( i = 0; i < $#str; ++i )); do
a+=( "$str:i:1" )
done


$parameter:offset:length is a bash substring expansion that will result in the length number of characters of the string $parameter from offset offset.







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 5 at 21:33









Kusalananda

107k14209331




107k14209331











  • For your first approach it gives: Bad : modifier in $ (0) but also for 1,2 etc.
    – user309317
    Sep 6 at 12:31










  • @user309317, you used the "bash" tag in your question, but it looks like you're actually using csh or tcsh. Please clarify your question with details of which shell you're using.
    – Stéphane Chazelas
    Sep 6 at 15:01










  • @user309317 Make sure that you are actually using the bash shell (the question was tagged with bash).
    – Kusalananda
    Sep 6 at 16:00
















  • For your first approach it gives: Bad : modifier in $ (0) but also for 1,2 etc.
    – user309317
    Sep 6 at 12:31










  • @user309317, you used the "bash" tag in your question, but it looks like you're actually using csh or tcsh. Please clarify your question with details of which shell you're using.
    – Stéphane Chazelas
    Sep 6 at 15:01










  • @user309317 Make sure that you are actually using the bash shell (the question was tagged with bash).
    – Kusalananda
    Sep 6 at 16:00















For your first approach it gives: Bad : modifier in $ (0) but also for 1,2 etc.
– user309317
Sep 6 at 12:31




For your first approach it gives: Bad : modifier in $ (0) but also for 1,2 etc.
– user309317
Sep 6 at 12:31












@user309317, you used the "bash" tag in your question, but it looks like you're actually using csh or tcsh. Please clarify your question with details of which shell you're using.
– Stéphane Chazelas
Sep 6 at 15:01




@user309317, you used the "bash" tag in your question, but it looks like you're actually using csh or tcsh. Please clarify your question with details of which shell you're using.
– Stéphane Chazelas
Sep 6 at 15:01












@user309317 Make sure that you are actually using the bash shell (the question was tagged with bash).
– Kusalananda
Sep 6 at 16:00




@user309317 Make sure that you are actually using the bash shell (the question was tagged with bash).
– Kusalananda
Sep 6 at 16:00












up vote
2
down vote













It's easier with zsh:



string=WER
array=($(s::)string)
printf '<%s>n' $array


(note that array indicies in zsh start at 1 like in most other shells, not 0 like in bash/ksh).



Or with fish (arrays also start at 1):



set string WER
set array (string split '' $string)
printf '<%s>n' $array


(assumes $string doesn't contain newline characters though).



POSIXly (the POSIX shell has one array: $@ (also starts at 1: $1)), so would also work in bash or zsh and your system's standard sh:



string=WER
set --
while [ -n "$string" ]; do
tmp=$string#?
set -- "$@" "$string%%"$tmp""
string=$tmp
done
printf '<%s>n' "$@"


With csh or tcsh which you seem to be using:



set string = WER
set array = "`printf '%sn' $string:q | fold -w1`"
printf '<%s>n' $array:q


Like for fish, it also assumes the string doesn't contain newline characters. Also note that some fold implementations will fold on bytes instead of characters which would mean it wouldn't work properly if the string contained multi-byte characters.






share|improve this answer






















  • When I try your first approach I always receive: Illegal variable name. Second one: Variable name must begin with a letter. Third one: set -- does not work. Sorry, but I am not really experienced with bash
    – user309317
    Sep 6 at 12:29










  • @user309317, I think you missed the with zsh and with fish which are different shells from bash. The last one should work with bash as indicated.
    – Stéphane Chazelas
    Sep 6 at 12:35










  • Thanks alot for your answer, but I still get the error: set: Variable name must begin with a letter when I try the POSIXIy
    – user309317
    Sep 6 at 14:09







  • 1




    @user309317, you tried the POSIXly in csh or tcsh, not bash nor any other POSIX shell.
    – Stéphane Chazelas
    Sep 6 at 14:11










  • Now it works. Is there also a possibility in csh?
    – user309317
    Sep 6 at 14:56














up vote
2
down vote













It's easier with zsh:



string=WER
array=($(s::)string)
printf '<%s>n' $array


(note that array indicies in zsh start at 1 like in most other shells, not 0 like in bash/ksh).



Or with fish (arrays also start at 1):



set string WER
set array (string split '' $string)
printf '<%s>n' $array


(assumes $string doesn't contain newline characters though).



POSIXly (the POSIX shell has one array: $@ (also starts at 1: $1)), so would also work in bash or zsh and your system's standard sh:



string=WER
set --
while [ -n "$string" ]; do
tmp=$string#?
set -- "$@" "$string%%"$tmp""
string=$tmp
done
printf '<%s>n' "$@"


With csh or tcsh which you seem to be using:



set string = WER
set array = "`printf '%sn' $string:q | fold -w1`"
printf '<%s>n' $array:q


Like for fish, it also assumes the string doesn't contain newline characters. Also note that some fold implementations will fold on bytes instead of characters which would mean it wouldn't work properly if the string contained multi-byte characters.






share|improve this answer






















  • When I try your first approach I always receive: Illegal variable name. Second one: Variable name must begin with a letter. Third one: set -- does not work. Sorry, but I am not really experienced with bash
    – user309317
    Sep 6 at 12:29










  • @user309317, I think you missed the with zsh and with fish which are different shells from bash. The last one should work with bash as indicated.
    – Stéphane Chazelas
    Sep 6 at 12:35










  • Thanks alot for your answer, but I still get the error: set: Variable name must begin with a letter when I try the POSIXIy
    – user309317
    Sep 6 at 14:09







  • 1




    @user309317, you tried the POSIXly in csh or tcsh, not bash nor any other POSIX shell.
    – Stéphane Chazelas
    Sep 6 at 14:11










  • Now it works. Is there also a possibility in csh?
    – user309317
    Sep 6 at 14:56












up vote
2
down vote










up vote
2
down vote









It's easier with zsh:



string=WER
array=($(s::)string)
printf '<%s>n' $array


(note that array indicies in zsh start at 1 like in most other shells, not 0 like in bash/ksh).



Or with fish (arrays also start at 1):



set string WER
set array (string split '' $string)
printf '<%s>n' $array


(assumes $string doesn't contain newline characters though).



POSIXly (the POSIX shell has one array: $@ (also starts at 1: $1)), so would also work in bash or zsh and your system's standard sh:



string=WER
set --
while [ -n "$string" ]; do
tmp=$string#?
set -- "$@" "$string%%"$tmp""
string=$tmp
done
printf '<%s>n' "$@"


With csh or tcsh which you seem to be using:



set string = WER
set array = "`printf '%sn' $string:q | fold -w1`"
printf '<%s>n' $array:q


Like for fish, it also assumes the string doesn't contain newline characters. Also note that some fold implementations will fold on bytes instead of characters which would mean it wouldn't work properly if the string contained multi-byte characters.






share|improve this answer














It's easier with zsh:



string=WER
array=($(s::)string)
printf '<%s>n' $array


(note that array indicies in zsh start at 1 like in most other shells, not 0 like in bash/ksh).



Or with fish (arrays also start at 1):



set string WER
set array (string split '' $string)
printf '<%s>n' $array


(assumes $string doesn't contain newline characters though).



POSIXly (the POSIX shell has one array: $@ (also starts at 1: $1)), so would also work in bash or zsh and your system's standard sh:



string=WER
set --
while [ -n "$string" ]; do
tmp=$string#?
set -- "$@" "$string%%"$tmp""
string=$tmp
done
printf '<%s>n' "$@"


With csh or tcsh which you seem to be using:



set string = WER
set array = "`printf '%sn' $string:q | fold -w1`"
printf '<%s>n' $array:q


Like for fish, it also assumes the string doesn't contain newline characters. Also note that some fold implementations will fold on bytes instead of characters which would mean it wouldn't work properly if the string contained multi-byte characters.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 6 at 14:59

























answered Sep 5 at 21:59









Stéphane Chazelas

286k53528866




286k53528866











  • When I try your first approach I always receive: Illegal variable name. Second one: Variable name must begin with a letter. Third one: set -- does not work. Sorry, but I am not really experienced with bash
    – user309317
    Sep 6 at 12:29










  • @user309317, I think you missed the with zsh and with fish which are different shells from bash. The last one should work with bash as indicated.
    – Stéphane Chazelas
    Sep 6 at 12:35










  • Thanks alot for your answer, but I still get the error: set: Variable name must begin with a letter when I try the POSIXIy
    – user309317
    Sep 6 at 14:09







  • 1




    @user309317, you tried the POSIXly in csh or tcsh, not bash nor any other POSIX shell.
    – Stéphane Chazelas
    Sep 6 at 14:11










  • Now it works. Is there also a possibility in csh?
    – user309317
    Sep 6 at 14:56
















  • When I try your first approach I always receive: Illegal variable name. Second one: Variable name must begin with a letter. Third one: set -- does not work. Sorry, but I am not really experienced with bash
    – user309317
    Sep 6 at 12:29










  • @user309317, I think you missed the with zsh and with fish which are different shells from bash. The last one should work with bash as indicated.
    – Stéphane Chazelas
    Sep 6 at 12:35










  • Thanks alot for your answer, but I still get the error: set: Variable name must begin with a letter when I try the POSIXIy
    – user309317
    Sep 6 at 14:09







  • 1




    @user309317, you tried the POSIXly in csh or tcsh, not bash nor any other POSIX shell.
    – Stéphane Chazelas
    Sep 6 at 14:11










  • Now it works. Is there also a possibility in csh?
    – user309317
    Sep 6 at 14:56















When I try your first approach I always receive: Illegal variable name. Second one: Variable name must begin with a letter. Third one: set -- does not work. Sorry, but I am not really experienced with bash
– user309317
Sep 6 at 12:29




When I try your first approach I always receive: Illegal variable name. Second one: Variable name must begin with a letter. Third one: set -- does not work. Sorry, but I am not really experienced with bash
– user309317
Sep 6 at 12:29












@user309317, I think you missed the with zsh and with fish which are different shells from bash. The last one should work with bash as indicated.
– Stéphane Chazelas
Sep 6 at 12:35




@user309317, I think you missed the with zsh and with fish which are different shells from bash. The last one should work with bash as indicated.
– Stéphane Chazelas
Sep 6 at 12:35












Thanks alot for your answer, but I still get the error: set: Variable name must begin with a letter when I try the POSIXIy
– user309317
Sep 6 at 14:09





Thanks alot for your answer, but I still get the error: set: Variable name must begin with a letter when I try the POSIXIy
– user309317
Sep 6 at 14:09





1




1




@user309317, you tried the POSIXly in csh or tcsh, not bash nor any other POSIX shell.
– Stéphane Chazelas
Sep 6 at 14:11




@user309317, you tried the POSIXly in csh or tcsh, not bash nor any other POSIX shell.
– Stéphane Chazelas
Sep 6 at 14:11












Now it works. Is there also a possibility in csh?
– user309317
Sep 6 at 14:56




Now it works. Is there also a possibility in csh?
– user309317
Sep 6 at 14:56


Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?