Pass Array Elements To Heredoc
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to pass an array elements to heredoc, the goal is to produce a file, for example:
declare -a box=("element1" "element2" "element3")
cat > test.txt <<-EOF
some text, insert first element
some text, insert second element
some text, insert third element
EOF
Is this possible?, How can i achieve this?
linux bash heredoc
add a comment |
I'm trying to pass an array elements to heredoc, the goal is to produce a file, for example:
declare -a box=("element1" "element2" "element3")
cat > test.txt <<-EOF
some text, insert first element
some text, insert second element
some text, insert third element
EOF
Is this possible?, How can i achieve this?
linux bash heredoc
add a comment |
I'm trying to pass an array elements to heredoc, the goal is to produce a file, for example:
declare -a box=("element1" "element2" "element3")
cat > test.txt <<-EOF
some text, insert first element
some text, insert second element
some text, insert third element
EOF
Is this possible?, How can i achieve this?
linux bash heredoc
I'm trying to pass an array elements to heredoc, the goal is to produce a file, for example:
declare -a box=("element1" "element2" "element3")
cat > test.txt <<-EOF
some text, insert first element
some text, insert second element
some text, insert third element
EOF
Is this possible?, How can i achieve this?
linux bash heredoc
linux bash heredoc
asked Jan 3 at 21:19
Haroldo Payares SalgadoHaroldo Payares Salgado
3310
3310
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You certainly can
cat > test.txt <<-EOF
some text, ${box[0]}
some text, ${box[1]}
some text, ${box[2]}
EOF
add a comment |
You can nest a loop with $(..)
:
declare -a box=("element1" "element2" "element3")
cat > test.txt <<-EOF
Greetings,
Here are the elements you wanted:
$(
for s in "${box[@]}"
do
echo "some text, $s"
done
)
Happy New Year from $USER
EOF
When executed, this produces a test.txt
containing:
Greetings,
Here are the elements you wanted:
some text, element1
some text, element2
some text, element3
Happy New Year from myusername
Thank you, What does $(..) do?
– Haroldo Payares Salgado
Jan 3 at 22:04
It's called command substitution. It runs the command inside the$(..)
, captures its stdout, and substitutes that in. For example:echo "The time is now $(date)"
will rundate
, captureThu Jan 3 14:06:27 PST 2019
, and insert that into the string
– that other guy
Jan 3 at 22:06
I appreciate the time you took to explain this, thank you.
– Haroldo Payares Salgado
Jan 3 at 22:08
Is it posibble to build a heredoc dynamically inside a loop using a string element inside an array?
– Haroldo Payares Salgado
Jan 3 at 22:29
A heredoc is meant as a template where you can insert the result of variables and commands. If you don't really have a template and want build everything dynamically, don't use a here doc. Just write a function that outputs anything you want in any order, and redirect the function to your file
– that other guy
Jan 3 at 22:56
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029951%2fpass-array-elements-to-heredoc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You certainly can
cat > test.txt <<-EOF
some text, ${box[0]}
some text, ${box[1]}
some text, ${box[2]}
EOF
add a comment |
You certainly can
cat > test.txt <<-EOF
some text, ${box[0]}
some text, ${box[1]}
some text, ${box[2]}
EOF
add a comment |
You certainly can
cat > test.txt <<-EOF
some text, ${box[0]}
some text, ${box[1]}
some text, ${box[2]}
EOF
You certainly can
cat > test.txt <<-EOF
some text, ${box[0]}
some text, ${box[1]}
some text, ${box[2]}
EOF
answered Jan 3 at 21:27
tinktink
6,94432835
6,94432835
add a comment |
add a comment |
You can nest a loop with $(..)
:
declare -a box=("element1" "element2" "element3")
cat > test.txt <<-EOF
Greetings,
Here are the elements you wanted:
$(
for s in "${box[@]}"
do
echo "some text, $s"
done
)
Happy New Year from $USER
EOF
When executed, this produces a test.txt
containing:
Greetings,
Here are the elements you wanted:
some text, element1
some text, element2
some text, element3
Happy New Year from myusername
Thank you, What does $(..) do?
– Haroldo Payares Salgado
Jan 3 at 22:04
It's called command substitution. It runs the command inside the$(..)
, captures its stdout, and substitutes that in. For example:echo "The time is now $(date)"
will rundate
, captureThu Jan 3 14:06:27 PST 2019
, and insert that into the string
– that other guy
Jan 3 at 22:06
I appreciate the time you took to explain this, thank you.
– Haroldo Payares Salgado
Jan 3 at 22:08
Is it posibble to build a heredoc dynamically inside a loop using a string element inside an array?
– Haroldo Payares Salgado
Jan 3 at 22:29
A heredoc is meant as a template where you can insert the result of variables and commands. If you don't really have a template and want build everything dynamically, don't use a here doc. Just write a function that outputs anything you want in any order, and redirect the function to your file
– that other guy
Jan 3 at 22:56
|
show 1 more comment
You can nest a loop with $(..)
:
declare -a box=("element1" "element2" "element3")
cat > test.txt <<-EOF
Greetings,
Here are the elements you wanted:
$(
for s in "${box[@]}"
do
echo "some text, $s"
done
)
Happy New Year from $USER
EOF
When executed, this produces a test.txt
containing:
Greetings,
Here are the elements you wanted:
some text, element1
some text, element2
some text, element3
Happy New Year from myusername
Thank you, What does $(..) do?
– Haroldo Payares Salgado
Jan 3 at 22:04
It's called command substitution. It runs the command inside the$(..)
, captures its stdout, and substitutes that in. For example:echo "The time is now $(date)"
will rundate
, captureThu Jan 3 14:06:27 PST 2019
, and insert that into the string
– that other guy
Jan 3 at 22:06
I appreciate the time you took to explain this, thank you.
– Haroldo Payares Salgado
Jan 3 at 22:08
Is it posibble to build a heredoc dynamically inside a loop using a string element inside an array?
– Haroldo Payares Salgado
Jan 3 at 22:29
A heredoc is meant as a template where you can insert the result of variables and commands. If you don't really have a template and want build everything dynamically, don't use a here doc. Just write a function that outputs anything you want in any order, and redirect the function to your file
– that other guy
Jan 3 at 22:56
|
show 1 more comment
You can nest a loop with $(..)
:
declare -a box=("element1" "element2" "element3")
cat > test.txt <<-EOF
Greetings,
Here are the elements you wanted:
$(
for s in "${box[@]}"
do
echo "some text, $s"
done
)
Happy New Year from $USER
EOF
When executed, this produces a test.txt
containing:
Greetings,
Here are the elements you wanted:
some text, element1
some text, element2
some text, element3
Happy New Year from myusername
You can nest a loop with $(..)
:
declare -a box=("element1" "element2" "element3")
cat > test.txt <<-EOF
Greetings,
Here are the elements you wanted:
$(
for s in "${box[@]}"
do
echo "some text, $s"
done
)
Happy New Year from $USER
EOF
When executed, this produces a test.txt
containing:
Greetings,
Here are the elements you wanted:
some text, element1
some text, element2
some text, element3
Happy New Year from myusername
answered Jan 3 at 21:26
that other guythat other guy
75k886124
75k886124
Thank you, What does $(..) do?
– Haroldo Payares Salgado
Jan 3 at 22:04
It's called command substitution. It runs the command inside the$(..)
, captures its stdout, and substitutes that in. For example:echo "The time is now $(date)"
will rundate
, captureThu Jan 3 14:06:27 PST 2019
, and insert that into the string
– that other guy
Jan 3 at 22:06
I appreciate the time you took to explain this, thank you.
– Haroldo Payares Salgado
Jan 3 at 22:08
Is it posibble to build a heredoc dynamically inside a loop using a string element inside an array?
– Haroldo Payares Salgado
Jan 3 at 22:29
A heredoc is meant as a template where you can insert the result of variables and commands. If you don't really have a template and want build everything dynamically, don't use a here doc. Just write a function that outputs anything you want in any order, and redirect the function to your file
– that other guy
Jan 3 at 22:56
|
show 1 more comment
Thank you, What does $(..) do?
– Haroldo Payares Salgado
Jan 3 at 22:04
It's called command substitution. It runs the command inside the$(..)
, captures its stdout, and substitutes that in. For example:echo "The time is now $(date)"
will rundate
, captureThu Jan 3 14:06:27 PST 2019
, and insert that into the string
– that other guy
Jan 3 at 22:06
I appreciate the time you took to explain this, thank you.
– Haroldo Payares Salgado
Jan 3 at 22:08
Is it posibble to build a heredoc dynamically inside a loop using a string element inside an array?
– Haroldo Payares Salgado
Jan 3 at 22:29
A heredoc is meant as a template where you can insert the result of variables and commands. If you don't really have a template and want build everything dynamically, don't use a here doc. Just write a function that outputs anything you want in any order, and redirect the function to your file
– that other guy
Jan 3 at 22:56
Thank you, What does $(..) do?
– Haroldo Payares Salgado
Jan 3 at 22:04
Thank you, What does $(..) do?
– Haroldo Payares Salgado
Jan 3 at 22:04
It's called command substitution. It runs the command inside the
$(..)
, captures its stdout, and substitutes that in. For example: echo "The time is now $(date)"
will run date
, capture Thu Jan 3 14:06:27 PST 2019
, and insert that into the string– that other guy
Jan 3 at 22:06
It's called command substitution. It runs the command inside the
$(..)
, captures its stdout, and substitutes that in. For example: echo "The time is now $(date)"
will run date
, capture Thu Jan 3 14:06:27 PST 2019
, and insert that into the string– that other guy
Jan 3 at 22:06
I appreciate the time you took to explain this, thank you.
– Haroldo Payares Salgado
Jan 3 at 22:08
I appreciate the time you took to explain this, thank you.
– Haroldo Payares Salgado
Jan 3 at 22:08
Is it posibble to build a heredoc dynamically inside a loop using a string element inside an array?
– Haroldo Payares Salgado
Jan 3 at 22:29
Is it posibble to build a heredoc dynamically inside a loop using a string element inside an array?
– Haroldo Payares Salgado
Jan 3 at 22:29
A heredoc is meant as a template where you can insert the result of variables and commands. If you don't really have a template and want build everything dynamically, don't use a here doc. Just write a function that outputs anything you want in any order, and redirect the function to your file
– that other guy
Jan 3 at 22:56
A heredoc is meant as a template where you can insert the result of variables and commands. If you don't really have a template and want build everything dynamically, don't use a here doc. Just write a function that outputs anything you want in any order, and redirect the function to your file
– that other guy
Jan 3 at 22:56
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029951%2fpass-array-elements-to-heredoc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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