Spaces missing from strings in an Bash array











up vote
0
down vote

favorite












I found a strange thing and will be nice if somebody can help to fix it.



#!/bin/bash
#

dockerdir="docker"


step_1=("nginx" "create" "default.conf")
step_2=("nginx" "use" "default.conf" "replace" "server tplphp:9000" "server bbaphp1:9000;")
#step_3=("nginx" "use" "default.conf" "add" " server bbaphp1:9000" " server bbaphp2:9000")
#step_4=("nginx" "use" "default.conf" "add" "server bbaphp2:9000" "server bbaphp3:9000")
#step_5=("nginx" "use" "default.conf" "delete" " server bbaphp3:9000")
#step_6=("php" "value0")
#step_7=("php" "value1")


declare -a buildfolders=(
step_1[@]
step_2[@]
)

#

echo "Start:"
arrayleght=${#buildfolders[@]}
for (( i=1; i<${arrayleght}+1; i++ ));
do
folder=${!buildfolders[$i-1]:0:1}
file_task=${!buildfolders[$i-1]:1:1}
file=${!buildfolders[$i-1]:2:1}
task=${!buildfolders[$i-1]:3:1}
variable_name=${!buildfolders[$i-1]:4:1}
variable_value=${!buildfolders[$i-1]:5:1}
echo "Folder ${folder} "
#" leght "${#folder}
echo "Task ${file_task}"
# " leght "${#file_task}
echo "File ${file}"
# " leght "${#file}
echo "Task for file ${task}"
# " leght "${#task}
echo "Variable name ${variable_name}"
# " leght "${#variable_name}
echo "Variable value ${variable_value}"
# " leght "${#variable_value}
echo " "
# filemanipulations ./${dockerdir}/${folder}/ ${file_task} ${file} ${task} "${variable_name}" "${variable_value}"
done

echo "Done."


The result is:



Start:
Folder nginx
Task create
File default.conf
Task for file
Variable name
Variable value

Folder nginx
Task use
File default.conf
Task for file replace
Variable name server tplphp:9000
Variable value server bbaphp1:9000;

Done.


Question: Where are spaces? Compare the result with the input:



step_2=("nginx" "use" "default.conf" "replace" "server               tplphp:9000" "server      bbaphp1:9000;")


Only one space left.










share|improve this question
























  • please fix your formatting, use the buttons. And your script is somehow missing some parts at the end...
    – RoVo
    Nov 27 at 9:59

















up vote
0
down vote

favorite












I found a strange thing and will be nice if somebody can help to fix it.



#!/bin/bash
#

dockerdir="docker"


step_1=("nginx" "create" "default.conf")
step_2=("nginx" "use" "default.conf" "replace" "server tplphp:9000" "server bbaphp1:9000;")
#step_3=("nginx" "use" "default.conf" "add" " server bbaphp1:9000" " server bbaphp2:9000")
#step_4=("nginx" "use" "default.conf" "add" "server bbaphp2:9000" "server bbaphp3:9000")
#step_5=("nginx" "use" "default.conf" "delete" " server bbaphp3:9000")
#step_6=("php" "value0")
#step_7=("php" "value1")


declare -a buildfolders=(
step_1[@]
step_2[@]
)

#

echo "Start:"
arrayleght=${#buildfolders[@]}
for (( i=1; i<${arrayleght}+1; i++ ));
do
folder=${!buildfolders[$i-1]:0:1}
file_task=${!buildfolders[$i-1]:1:1}
file=${!buildfolders[$i-1]:2:1}
task=${!buildfolders[$i-1]:3:1}
variable_name=${!buildfolders[$i-1]:4:1}
variable_value=${!buildfolders[$i-1]:5:1}
echo "Folder ${folder} "
#" leght "${#folder}
echo "Task ${file_task}"
# " leght "${#file_task}
echo "File ${file}"
# " leght "${#file}
echo "Task for file ${task}"
# " leght "${#task}
echo "Variable name ${variable_name}"
# " leght "${#variable_name}
echo "Variable value ${variable_value}"
# " leght "${#variable_value}
echo " "
# filemanipulations ./${dockerdir}/${folder}/ ${file_task} ${file} ${task} "${variable_name}" "${variable_value}"
done

echo "Done."


The result is:



Start:
Folder nginx
Task create
File default.conf
Task for file
Variable name
Variable value

Folder nginx
Task use
File default.conf
Task for file replace
Variable name server tplphp:9000
Variable value server bbaphp1:9000;

Done.


Question: Where are spaces? Compare the result with the input:



step_2=("nginx" "use" "default.conf" "replace" "server               tplphp:9000" "server      bbaphp1:9000;")


Only one space left.










share|improve this question
























  • please fix your formatting, use the buttons. And your script is somehow missing some parts at the end...
    – RoVo
    Nov 27 at 9:59















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I found a strange thing and will be nice if somebody can help to fix it.



#!/bin/bash
#

dockerdir="docker"


step_1=("nginx" "create" "default.conf")
step_2=("nginx" "use" "default.conf" "replace" "server tplphp:9000" "server bbaphp1:9000;")
#step_3=("nginx" "use" "default.conf" "add" " server bbaphp1:9000" " server bbaphp2:9000")
#step_4=("nginx" "use" "default.conf" "add" "server bbaphp2:9000" "server bbaphp3:9000")
#step_5=("nginx" "use" "default.conf" "delete" " server bbaphp3:9000")
#step_6=("php" "value0")
#step_7=("php" "value1")


declare -a buildfolders=(
step_1[@]
step_2[@]
)

#

echo "Start:"
arrayleght=${#buildfolders[@]}
for (( i=1; i<${arrayleght}+1; i++ ));
do
folder=${!buildfolders[$i-1]:0:1}
file_task=${!buildfolders[$i-1]:1:1}
file=${!buildfolders[$i-1]:2:1}
task=${!buildfolders[$i-1]:3:1}
variable_name=${!buildfolders[$i-1]:4:1}
variable_value=${!buildfolders[$i-1]:5:1}
echo "Folder ${folder} "
#" leght "${#folder}
echo "Task ${file_task}"
# " leght "${#file_task}
echo "File ${file}"
# " leght "${#file}
echo "Task for file ${task}"
# " leght "${#task}
echo "Variable name ${variable_name}"
# " leght "${#variable_name}
echo "Variable value ${variable_value}"
# " leght "${#variable_value}
echo " "
# filemanipulations ./${dockerdir}/${folder}/ ${file_task} ${file} ${task} "${variable_name}" "${variable_value}"
done

echo "Done."


The result is:



Start:
Folder nginx
Task create
File default.conf
Task for file
Variable name
Variable value

Folder nginx
Task use
File default.conf
Task for file replace
Variable name server tplphp:9000
Variable value server bbaphp1:9000;

Done.


Question: Where are spaces? Compare the result with the input:



step_2=("nginx" "use" "default.conf" "replace" "server               tplphp:9000" "server      bbaphp1:9000;")


Only one space left.










share|improve this question















I found a strange thing and will be nice if somebody can help to fix it.



#!/bin/bash
#

dockerdir="docker"


step_1=("nginx" "create" "default.conf")
step_2=("nginx" "use" "default.conf" "replace" "server tplphp:9000" "server bbaphp1:9000;")
#step_3=("nginx" "use" "default.conf" "add" " server bbaphp1:9000" " server bbaphp2:9000")
#step_4=("nginx" "use" "default.conf" "add" "server bbaphp2:9000" "server bbaphp3:9000")
#step_5=("nginx" "use" "default.conf" "delete" " server bbaphp3:9000")
#step_6=("php" "value0")
#step_7=("php" "value1")


declare -a buildfolders=(
step_1[@]
step_2[@]
)

#

echo "Start:"
arrayleght=${#buildfolders[@]}
for (( i=1; i<${arrayleght}+1; i++ ));
do
folder=${!buildfolders[$i-1]:0:1}
file_task=${!buildfolders[$i-1]:1:1}
file=${!buildfolders[$i-1]:2:1}
task=${!buildfolders[$i-1]:3:1}
variable_name=${!buildfolders[$i-1]:4:1}
variable_value=${!buildfolders[$i-1]:5:1}
echo "Folder ${folder} "
#" leght "${#folder}
echo "Task ${file_task}"
# " leght "${#file_task}
echo "File ${file}"
# " leght "${#file}
echo "Task for file ${task}"
# " leght "${#task}
echo "Variable name ${variable_name}"
# " leght "${#variable_name}
echo "Variable value ${variable_value}"
# " leght "${#variable_value}
echo " "
# filemanipulations ./${dockerdir}/${folder}/ ${file_task} ${file} ${task} "${variable_name}" "${variable_value}"
done

echo "Done."


The result is:



Start:
Folder nginx
Task create
File default.conf
Task for file
Variable name
Variable value

Folder nginx
Task use
File default.conf
Task for file replace
Variable name server tplphp:9000
Variable value server bbaphp1:9000;

Done.


Question: Where are spaces? Compare the result with the input:



step_2=("nginx" "use" "default.conf" "replace" "server               tplphp:9000" "server      bbaphp1:9000;")


Only one space left.







bash scripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 7:29









Melebius

4,23251837




4,23251837










asked Nov 27 at 9:42









Vladimir Izmalkov

104




104












  • please fix your formatting, use the buttons. And your script is somehow missing some parts at the end...
    – RoVo
    Nov 27 at 9:59




















  • please fix your formatting, use the buttons. And your script is somehow missing some parts at the end...
    – RoVo
    Nov 27 at 9:59


















please fix your formatting, use the buttons. And your script is somehow missing some parts at the end...
– RoVo
Nov 27 at 9:59






please fix your formatting, use the buttons. And your script is somehow missing some parts at the end...
– RoVo
Nov 27 at 9:59












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










The problem is that the Bash shell's field splitting feature is removing the multiple spaces. You can prevent this by quoting all of your variable/array expansions. When you're building your compound array:



declare -a buildfolders=(
"${step_1[@]}"
"${step_2[@]}"
)


Also when you're putting parts of that array into variables:



echo "Start:"
arrayleght=${#buildfolders[@]}
for (( i=1; i<${arrayleght}+1; i++ ));
do
folder="${!buildfolders[$i-1]:0:1}"
file_task="${!buildfolders[$i-1]:1:1}"
file="${!buildfolders[$i-1]:2:1}"
task="${!buildfolders[$i-1]:3:1}"
variable_name="${!buildfolders[$i-1]:4:1}"
variable_value="${!buildfolders[$i-1]:5:1}"


This will keep all of the multiple spaces in place.






share|improve this answer





















  • Thanks, now it`s working.
    – Vladimir Izmalkov
    Nov 27 at 11:34










  • Glad to help! If this answers your question you can click on the check mark to the left of the answer to accept it.
    – Arronical
    Nov 27 at 11:38






  • 1




    @VladimirIzmalkov: If this answer was helpful to you, then please consider marking it as the accepted answer (by click on the grey tick ✓ left to it) so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out.
    – pa4080
    Nov 28 at 9:00











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1096429%2fspaces-missing-from-strings-in-an-bash-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










The problem is that the Bash shell's field splitting feature is removing the multiple spaces. You can prevent this by quoting all of your variable/array expansions. When you're building your compound array:



declare -a buildfolders=(
"${step_1[@]}"
"${step_2[@]}"
)


Also when you're putting parts of that array into variables:



echo "Start:"
arrayleght=${#buildfolders[@]}
for (( i=1; i<${arrayleght}+1; i++ ));
do
folder="${!buildfolders[$i-1]:0:1}"
file_task="${!buildfolders[$i-1]:1:1}"
file="${!buildfolders[$i-1]:2:1}"
task="${!buildfolders[$i-1]:3:1}"
variable_name="${!buildfolders[$i-1]:4:1}"
variable_value="${!buildfolders[$i-1]:5:1}"


This will keep all of the multiple spaces in place.






share|improve this answer





















  • Thanks, now it`s working.
    – Vladimir Izmalkov
    Nov 27 at 11:34










  • Glad to help! If this answers your question you can click on the check mark to the left of the answer to accept it.
    – Arronical
    Nov 27 at 11:38






  • 1




    @VladimirIzmalkov: If this answer was helpful to you, then please consider marking it as the accepted answer (by click on the grey tick ✓ left to it) so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out.
    – pa4080
    Nov 28 at 9:00















up vote
2
down vote



accepted










The problem is that the Bash shell's field splitting feature is removing the multiple spaces. You can prevent this by quoting all of your variable/array expansions. When you're building your compound array:



declare -a buildfolders=(
"${step_1[@]}"
"${step_2[@]}"
)


Also when you're putting parts of that array into variables:



echo "Start:"
arrayleght=${#buildfolders[@]}
for (( i=1; i<${arrayleght}+1; i++ ));
do
folder="${!buildfolders[$i-1]:0:1}"
file_task="${!buildfolders[$i-1]:1:1}"
file="${!buildfolders[$i-1]:2:1}"
task="${!buildfolders[$i-1]:3:1}"
variable_name="${!buildfolders[$i-1]:4:1}"
variable_value="${!buildfolders[$i-1]:5:1}"


This will keep all of the multiple spaces in place.






share|improve this answer





















  • Thanks, now it`s working.
    – Vladimir Izmalkov
    Nov 27 at 11:34










  • Glad to help! If this answers your question you can click on the check mark to the left of the answer to accept it.
    – Arronical
    Nov 27 at 11:38






  • 1




    @VladimirIzmalkov: If this answer was helpful to you, then please consider marking it as the accepted answer (by click on the grey tick ✓ left to it) so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out.
    – pa4080
    Nov 28 at 9:00













up vote
2
down vote



accepted







up vote
2
down vote



accepted






The problem is that the Bash shell's field splitting feature is removing the multiple spaces. You can prevent this by quoting all of your variable/array expansions. When you're building your compound array:



declare -a buildfolders=(
"${step_1[@]}"
"${step_2[@]}"
)


Also when you're putting parts of that array into variables:



echo "Start:"
arrayleght=${#buildfolders[@]}
for (( i=1; i<${arrayleght}+1; i++ ));
do
folder="${!buildfolders[$i-1]:0:1}"
file_task="${!buildfolders[$i-1]:1:1}"
file="${!buildfolders[$i-1]:2:1}"
task="${!buildfolders[$i-1]:3:1}"
variable_name="${!buildfolders[$i-1]:4:1}"
variable_value="${!buildfolders[$i-1]:5:1}"


This will keep all of the multiple spaces in place.






share|improve this answer












The problem is that the Bash shell's field splitting feature is removing the multiple spaces. You can prevent this by quoting all of your variable/array expansions. When you're building your compound array:



declare -a buildfolders=(
"${step_1[@]}"
"${step_2[@]}"
)


Also when you're putting parts of that array into variables:



echo "Start:"
arrayleght=${#buildfolders[@]}
for (( i=1; i<${arrayleght}+1; i++ ));
do
folder="${!buildfolders[$i-1]:0:1}"
file_task="${!buildfolders[$i-1]:1:1}"
file="${!buildfolders[$i-1]:2:1}"
task="${!buildfolders[$i-1]:3:1}"
variable_name="${!buildfolders[$i-1]:4:1}"
variable_value="${!buildfolders[$i-1]:5:1}"


This will keep all of the multiple spaces in place.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 at 10:58









Arronical

13k84790




13k84790












  • Thanks, now it`s working.
    – Vladimir Izmalkov
    Nov 27 at 11:34










  • Glad to help! If this answers your question you can click on the check mark to the left of the answer to accept it.
    – Arronical
    Nov 27 at 11:38






  • 1




    @VladimirIzmalkov: If this answer was helpful to you, then please consider marking it as the accepted answer (by click on the grey tick ✓ left to it) so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out.
    – pa4080
    Nov 28 at 9:00


















  • Thanks, now it`s working.
    – Vladimir Izmalkov
    Nov 27 at 11:34










  • Glad to help! If this answers your question you can click on the check mark to the left of the answer to accept it.
    – Arronical
    Nov 27 at 11:38






  • 1




    @VladimirIzmalkov: If this answer was helpful to you, then please consider marking it as the accepted answer (by click on the grey tick ✓ left to it) so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out.
    – pa4080
    Nov 28 at 9:00
















Thanks, now it`s working.
– Vladimir Izmalkov
Nov 27 at 11:34




Thanks, now it`s working.
– Vladimir Izmalkov
Nov 27 at 11:34












Glad to help! If this answers your question you can click on the check mark to the left of the answer to accept it.
– Arronical
Nov 27 at 11:38




Glad to help! If this answers your question you can click on the check mark to the left of the answer to accept it.
– Arronical
Nov 27 at 11:38




1




1




@VladimirIzmalkov: If this answer was helpful to you, then please consider marking it as the accepted answer (by click on the grey tick ✓ left to it) so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out.
– pa4080
Nov 28 at 9:00




@VladimirIzmalkov: If this answer was helpful to you, then please consider marking it as the accepted answer (by click on the grey tick ✓ left to it) so others may more easily find it in the future. This is also a polite way to thank the person answering your question for helping you out.
– pa4080
Nov 28 at 9:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1096429%2fspaces-missing-from-strings-in-an-bash-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

 ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕