How to store (and load) a zsh String array to a file?
I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will never occur in my strings), but of course, IFS only works for single chars.
I can think of some ugly hacks to do it (e.g., store that nonsense string as a line between elements, read the file line by line and check each line against it, and rebuild the array thus.), but I will appreciate some more experienced opinions.
zsh array
add a comment |
I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will never occur in my strings), but of course, IFS only works for single chars.
I can think of some ugly hacks to do it (e.g., store that nonsense string as a line between elements, read the file line by line and check each line against it, and rebuild the array thus.), but I will appreciate some more experienced opinions.
zsh array
add a comment |
I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will never occur in my strings), but of course, IFS only works for single chars.
I can think of some ugly hacks to do it (e.g., store that nonsense string as a line between elements, read the file line by line and check each line against it, and rebuild the array thus.), but I will appreciate some more experienced opinions.
zsh array
I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will never occur in my strings), but of course, IFS only works for single chars.
I can think of some ugly hacks to do it (e.g., store that nonsense string as a line between elements, read the file line by line and check each line against it, and rebuild the array thus.), but I will appreciate some more experienced opinions.
zsh array
zsh array
edited Dec 9 '18 at 14:27
Jeff Schaller
39.3k1054125
39.3k1054125
asked Dec 5 '18 at 20:11
HappyFaceHappyFace
31811
31811
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Just do:
typeset array > file
To load:
source file
(you can also use typeset -p array
to also save the attributes of the array variable (exported, unique...)).
Alternatively:
print -rl -- ${(qq)array} > file
To load:
eval "array=($(<file))"
For your separator idea:
print -r -- ${(j:separator:)array} > file
To load:
array=("${(@s:separator:)"$(<file)")
(though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).
add a comment |
The two portable (ksh, zsh, bash) solutions AFAICT are:
typeset -p arr >./file # save array
. ./file # read array
And
printf '%qn' "${arr[@]}" >./file # save array
eval "arr=( $(< ./file) )" # read array
Note that the first solution will create a local variable if used inside a function in bash.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f486230%2fhow-to-store-and-load-a-zsh-string-array-to-a-file%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
Just do:
typeset array > file
To load:
source file
(you can also use typeset -p array
to also save the attributes of the array variable (exported, unique...)).
Alternatively:
print -rl -- ${(qq)array} > file
To load:
eval "array=($(<file))"
For your separator idea:
print -r -- ${(j:separator:)array} > file
To load:
array=("${(@s:separator:)"$(<file)")
(though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).
add a comment |
Just do:
typeset array > file
To load:
source file
(you can also use typeset -p array
to also save the attributes of the array variable (exported, unique...)).
Alternatively:
print -rl -- ${(qq)array} > file
To load:
eval "array=($(<file))"
For your separator idea:
print -r -- ${(j:separator:)array} > file
To load:
array=("${(@s:separator:)"$(<file)")
(though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).
add a comment |
Just do:
typeset array > file
To load:
source file
(you can also use typeset -p array
to also save the attributes of the array variable (exported, unique...)).
Alternatively:
print -rl -- ${(qq)array} > file
To load:
eval "array=($(<file))"
For your separator idea:
print -r -- ${(j:separator:)array} > file
To load:
array=("${(@s:separator:)"$(<file)")
(though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).
Just do:
typeset array > file
To load:
source file
(you can also use typeset -p array
to also save the attributes of the array variable (exported, unique...)).
Alternatively:
print -rl -- ${(qq)array} > file
To load:
eval "array=($(<file))"
For your separator idea:
print -r -- ${(j:separator:)array} > file
To load:
array=("${(@s:separator:)"$(<file)")
(though beware it removes all trailing newline characters from the last element of the array and it doesn't work for an empty array).
edited Dec 5 '18 at 20:49
answered Dec 5 '18 at 20:32
Stéphane ChazelasStéphane Chazelas
301k55564916
301k55564916
add a comment |
add a comment |
The two portable (ksh, zsh, bash) solutions AFAICT are:
typeset -p arr >./file # save array
. ./file # read array
And
printf '%qn' "${arr[@]}" >./file # save array
eval "arr=( $(< ./file) )" # read array
Note that the first solution will create a local variable if used inside a function in bash.
add a comment |
The two portable (ksh, zsh, bash) solutions AFAICT are:
typeset -p arr >./file # save array
. ./file # read array
And
printf '%qn' "${arr[@]}" >./file # save array
eval "arr=( $(< ./file) )" # read array
Note that the first solution will create a local variable if used inside a function in bash.
add a comment |
The two portable (ksh, zsh, bash) solutions AFAICT are:
typeset -p arr >./file # save array
. ./file # read array
And
printf '%qn' "${arr[@]}" >./file # save array
eval "arr=( $(< ./file) )" # read array
Note that the first solution will create a local variable if used inside a function in bash.
The two portable (ksh, zsh, bash) solutions AFAICT are:
typeset -p arr >./file # save array
. ./file # read array
And
printf '%qn' "${arr[@]}" >./file # save array
eval "arr=( $(< ./file) )" # read array
Note that the first solution will create a local variable if used inside a function in bash.
answered Dec 6 '18 at 9:17
IsaacIsaac
11.4k11652
11.4k11652
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f486230%2fhow-to-store-and-load-a-zsh-string-array-to-a-file%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