can bash show one array item id and value using `declare -p`?
$ str="a'"b"
$ declare -p str
declare -- str="a'"b" # see " was escaped, possibly other chars will too
$ astr=("$str" "c")
$ declare -p astr
declare -ax astr='([0]="a'''"b" [1]="c")'
so, is there some way to do something like declare -p astr[0]
and retrieve something like this: declare -- astr[0]="a'"b"
?
I could use sed
or something else, but I would like to know if bash allow accessing astr[n] thru declare -p
in some way I couldn't guess yet?
bash array
add a comment |
$ str="a'"b"
$ declare -p str
declare -- str="a'"b" # see " was escaped, possibly other chars will too
$ astr=("$str" "c")
$ declare -p astr
declare -ax astr='([0]="a'''"b" [1]="c")'
so, is there some way to do something like declare -p astr[0]
and retrieve something like this: declare -- astr[0]="a'"b"
?
I could use sed
or something else, but I would like to know if bash allow accessing astr[n] thru declare -p
in some way I couldn't guess yet?
bash array
add a comment |
$ str="a'"b"
$ declare -p str
declare -- str="a'"b" # see " was escaped, possibly other chars will too
$ astr=("$str" "c")
$ declare -p astr
declare -ax astr='([0]="a'''"b" [1]="c")'
so, is there some way to do something like declare -p astr[0]
and retrieve something like this: declare -- astr[0]="a'"b"
?
I could use sed
or something else, but I would like to know if bash allow accessing astr[n] thru declare -p
in some way I couldn't guess yet?
bash array
$ str="a'"b"
$ declare -p str
declare -- str="a'"b" # see " was escaped, possibly other chars will too
$ astr=("$str" "c")
$ declare -p astr
declare -ax astr='([0]="a'''"b" [1]="c")'
so, is there some way to do something like declare -p astr[0]
and retrieve something like this: declare -- astr[0]="a'"b"
?
I could use sed
or something else, but I would like to know if bash allow accessing astr[n] thru declare -p
in some way I couldn't guess yet?
bash array
bash array
asked 5 hours ago
Aquarius PowerAquarius Power
1,67232036
1,67232036
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If you are just looking for a way of displaying the data with escaped special characters, then the %q
format string of printf
in bash
would do that for you:
printf '%qn' "${astr[0]}"
To replicate the declare -p
-like output that you suggest:
printf 'declare -- astr[0]="%q"n' "${astr[0]}"
This is from the bash
manual, regarding the %q
format string of printf
:
%q
causes
printf
to output the corresponding argument in a format that can be reused
as shell input.
it is a lot simpler than using sed and does the same thing I was looking for, thx!
– Aquarius Power
4 hours ago
add a comment |
With bash v4.4 you can use the @A
parameter expansion operator to get similar results, but not effectively for a single array element:
A
The expansion is a string in the form of an assignment statement or declare command that, if evaluated, will recreate parameter with its attributes and value.
$ str="a'"b"
$ astr=("$str" "c")
$ echo ${astr[0]@A}
declare -a astr='a'''"b'
$ echo ${astr[@]@A}
declare -a astr=([0]="a'"b" [1]="c")
Or similar to the %q
printf format you can use the @Q
operator:
$ echo ${astr[0]@Q}
'a'''"b'
$ echo "declare -- astr[0]=${astr[0]@Q}"
declare -- astr[0]='a'''"b'
1
a new feature, interesting! mine is v4.3.48, I am almost installing 18.04 (16.04 here), I will check then :)
– Aquarius Power
4 hours ago
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%2f494175%2fcan-bash-show-one-array-item-id-and-value-using-declare-p%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
If you are just looking for a way of displaying the data with escaped special characters, then the %q
format string of printf
in bash
would do that for you:
printf '%qn' "${astr[0]}"
To replicate the declare -p
-like output that you suggest:
printf 'declare -- astr[0]="%q"n' "${astr[0]}"
This is from the bash
manual, regarding the %q
format string of printf
:
%q
causes
printf
to output the corresponding argument in a format that can be reused
as shell input.
it is a lot simpler than using sed and does the same thing I was looking for, thx!
– Aquarius Power
4 hours ago
add a comment |
If you are just looking for a way of displaying the data with escaped special characters, then the %q
format string of printf
in bash
would do that for you:
printf '%qn' "${astr[0]}"
To replicate the declare -p
-like output that you suggest:
printf 'declare -- astr[0]="%q"n' "${astr[0]}"
This is from the bash
manual, regarding the %q
format string of printf
:
%q
causes
printf
to output the corresponding argument in a format that can be reused
as shell input.
it is a lot simpler than using sed and does the same thing I was looking for, thx!
– Aquarius Power
4 hours ago
add a comment |
If you are just looking for a way of displaying the data with escaped special characters, then the %q
format string of printf
in bash
would do that for you:
printf '%qn' "${astr[0]}"
To replicate the declare -p
-like output that you suggest:
printf 'declare -- astr[0]="%q"n' "${astr[0]}"
This is from the bash
manual, regarding the %q
format string of printf
:
%q
causes
printf
to output the corresponding argument in a format that can be reused
as shell input.
If you are just looking for a way of displaying the data with escaped special characters, then the %q
format string of printf
in bash
would do that for you:
printf '%qn' "${astr[0]}"
To replicate the declare -p
-like output that you suggest:
printf 'declare -- astr[0]="%q"n' "${astr[0]}"
This is from the bash
manual, regarding the %q
format string of printf
:
%q
causes
printf
to output the corresponding argument in a format that can be reused
as shell input.
edited 5 hours ago
answered 5 hours ago
KusalanandaKusalananda
124k16232382
124k16232382
it is a lot simpler than using sed and does the same thing I was looking for, thx!
– Aquarius Power
4 hours ago
add a comment |
it is a lot simpler than using sed and does the same thing I was looking for, thx!
– Aquarius Power
4 hours ago
it is a lot simpler than using sed and does the same thing I was looking for, thx!
– Aquarius Power
4 hours ago
it is a lot simpler than using sed and does the same thing I was looking for, thx!
– Aquarius Power
4 hours ago
add a comment |
With bash v4.4 you can use the @A
parameter expansion operator to get similar results, but not effectively for a single array element:
A
The expansion is a string in the form of an assignment statement or declare command that, if evaluated, will recreate parameter with its attributes and value.
$ str="a'"b"
$ astr=("$str" "c")
$ echo ${astr[0]@A}
declare -a astr='a'''"b'
$ echo ${astr[@]@A}
declare -a astr=([0]="a'"b" [1]="c")
Or similar to the %q
printf format you can use the @Q
operator:
$ echo ${astr[0]@Q}
'a'''"b'
$ echo "declare -- astr[0]=${astr[0]@Q}"
declare -- astr[0]='a'''"b'
1
a new feature, interesting! mine is v4.3.48, I am almost installing 18.04 (16.04 here), I will check then :)
– Aquarius Power
4 hours ago
add a comment |
With bash v4.4 you can use the @A
parameter expansion operator to get similar results, but not effectively for a single array element:
A
The expansion is a string in the form of an assignment statement or declare command that, if evaluated, will recreate parameter with its attributes and value.
$ str="a'"b"
$ astr=("$str" "c")
$ echo ${astr[0]@A}
declare -a astr='a'''"b'
$ echo ${astr[@]@A}
declare -a astr=([0]="a'"b" [1]="c")
Or similar to the %q
printf format you can use the @Q
operator:
$ echo ${astr[0]@Q}
'a'''"b'
$ echo "declare -- astr[0]=${astr[0]@Q}"
declare -- astr[0]='a'''"b'
1
a new feature, interesting! mine is v4.3.48, I am almost installing 18.04 (16.04 here), I will check then :)
– Aquarius Power
4 hours ago
add a comment |
With bash v4.4 you can use the @A
parameter expansion operator to get similar results, but not effectively for a single array element:
A
The expansion is a string in the form of an assignment statement or declare command that, if evaluated, will recreate parameter with its attributes and value.
$ str="a'"b"
$ astr=("$str" "c")
$ echo ${astr[0]@A}
declare -a astr='a'''"b'
$ echo ${astr[@]@A}
declare -a astr=([0]="a'"b" [1]="c")
Or similar to the %q
printf format you can use the @Q
operator:
$ echo ${astr[0]@Q}
'a'''"b'
$ echo "declare -- astr[0]=${astr[0]@Q}"
declare -- astr[0]='a'''"b'
With bash v4.4 you can use the @A
parameter expansion operator to get similar results, but not effectively for a single array element:
A
The expansion is a string in the form of an assignment statement or declare command that, if evaluated, will recreate parameter with its attributes and value.
$ str="a'"b"
$ astr=("$str" "c")
$ echo ${astr[0]@A}
declare -a astr='a'''"b'
$ echo ${astr[@]@A}
declare -a astr=([0]="a'"b" [1]="c")
Or similar to the %q
printf format you can use the @Q
operator:
$ echo ${astr[0]@Q}
'a'''"b'
$ echo "declare -- astr[0]=${astr[0]@Q}"
declare -- astr[0]='a'''"b'
edited 4 hours ago
answered 4 hours ago
Jesse_bJesse_b
12k23064
12k23064
1
a new feature, interesting! mine is v4.3.48, I am almost installing 18.04 (16.04 here), I will check then :)
– Aquarius Power
4 hours ago
add a comment |
1
a new feature, interesting! mine is v4.3.48, I am almost installing 18.04 (16.04 here), I will check then :)
– Aquarius Power
4 hours ago
1
1
a new feature, interesting! mine is v4.3.48, I am almost installing 18.04 (16.04 here), I will check then :)
– Aquarius Power
4 hours ago
a new feature, interesting! mine is v4.3.48, I am almost installing 18.04 (16.04 here), I will check then :)
– Aquarius Power
4 hours ago
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%2f494175%2fcan-bash-show-one-array-item-id-and-value-using-declare-p%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