Print all variables used within a bash script
Is there any way to print all the variables declared and used within a Bash script?
For example printf "%sn" "${FUNCNAME[@]}"
will print the names of all shell functions currently in the execution call stack.
Ex: This is my script content (there must be a lot of declared variables):
#!/bin/bash
say_hello() {
name="$1"
echo "Hello $name"
}
my_name="Luis Daniel"
my_age="29"
say_hello "$my_name"
Then, I need to log something like this:
my_name = Luis Daniel
my_age = 29
name = Luis Daniel
bash shell-script
add a comment |
Is there any way to print all the variables declared and used within a Bash script?
For example printf "%sn" "${FUNCNAME[@]}"
will print the names of all shell functions currently in the execution call stack.
Ex: This is my script content (there must be a lot of declared variables):
#!/bin/bash
say_hello() {
name="$1"
echo "Hello $name"
}
my_name="Luis Daniel"
my_age="29"
say_hello "$my_name"
Then, I need to log something like this:
my_name = Luis Daniel
my_age = 29
name = Luis Daniel
bash shell-script
It's unclear whether you're interested in creating that particular output using e.g. three calls toecho
, or whether you're interested in a more generic solution (possibly some form of shell script debugging tool?).
– Kusalananda♦
Mar 5 at 22:18
add a comment |
Is there any way to print all the variables declared and used within a Bash script?
For example printf "%sn" "${FUNCNAME[@]}"
will print the names of all shell functions currently in the execution call stack.
Ex: This is my script content (there must be a lot of declared variables):
#!/bin/bash
say_hello() {
name="$1"
echo "Hello $name"
}
my_name="Luis Daniel"
my_age="29"
say_hello "$my_name"
Then, I need to log something like this:
my_name = Luis Daniel
my_age = 29
name = Luis Daniel
bash shell-script
Is there any way to print all the variables declared and used within a Bash script?
For example printf "%sn" "${FUNCNAME[@]}"
will print the names of all shell functions currently in the execution call stack.
Ex: This is my script content (there must be a lot of declared variables):
#!/bin/bash
say_hello() {
name="$1"
echo "Hello $name"
}
my_name="Luis Daniel"
my_age="29"
say_hello "$my_name"
Then, I need to log something like this:
my_name = Luis Daniel
my_age = 29
name = Luis Daniel
bash shell-script
bash shell-script
edited Mar 5 at 22:16
Luis Daniel
asked Mar 5 at 21:50
Luis DanielLuis Daniel
1012
1012
It's unclear whether you're interested in creating that particular output using e.g. three calls toecho
, or whether you're interested in a more generic solution (possibly some form of shell script debugging tool?).
– Kusalananda♦
Mar 5 at 22:18
add a comment |
It's unclear whether you're interested in creating that particular output using e.g. three calls toecho
, or whether you're interested in a more generic solution (possibly some form of shell script debugging tool?).
– Kusalananda♦
Mar 5 at 22:18
It's unclear whether you're interested in creating that particular output using e.g. three calls to
echo
, or whether you're interested in a more generic solution (possibly some form of shell script debugging tool?).– Kusalananda♦
Mar 5 at 22:18
It's unclear whether you're interested in creating that particular output using e.g. three calls to
echo
, or whether you're interested in a more generic solution (possibly some form of shell script debugging tool?).– Kusalananda♦
Mar 5 at 22:18
add a comment |
1 Answer
1
active
oldest
votes
The commands set
or declare
by themselves will print all shell variables and their values (and would additionally output function definitions). declare -p
would not output function definitions but would annotate each variable with its type (e.g. -r
for read-only, -a
for array, etc.) The export
command by itself will print environment variables (exported shell variables), as would env
and printenv
.
Whether the variables are used or not within the current shell session will not necessarily be detected in the output of these commands. The bash
shell has a number of variables that exists in any shell session, such as RANDOM
and EUID
, regardless of whether these are used or not. A variable may also not be available in the current scope when declare
is called, for example if it's a local variable in a function, or if it has been unset
, or if it was declared in a sub-shell that is no longer active.
Would you want to see the variables created in a particular script, you would have to save the output of e.g. declare -p
at the start of the script and later compare that with another invocation of the same command at the end of the script (or wherever you'd like to investigate the currently declared variables).
Example:
#!/bin/bash
tmpfile=$(mktemp)
declare -p >"$tmpfile"
say_hello() {
name="$1"
echo "Hello $name"
}
my_name="Luis Daniel"
my_age="29"
say_hello "$my_name"
declare -p | diff "$tmpfile" -
rm -f "$tmpfile"
Running it:
$ bash script.sh
Hello Luis Daniel
46c46,49
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
> declare -- name="Luis Daniel"
Note that declaring name
as a local
variable in the function would, since it's not available at the second call to declare -p
, produce
Hello Luis Daniel
46c46,48
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
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%2f504583%2fprint-all-variables-used-within-a-bash-script%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
The commands set
or declare
by themselves will print all shell variables and their values (and would additionally output function definitions). declare -p
would not output function definitions but would annotate each variable with its type (e.g. -r
for read-only, -a
for array, etc.) The export
command by itself will print environment variables (exported shell variables), as would env
and printenv
.
Whether the variables are used or not within the current shell session will not necessarily be detected in the output of these commands. The bash
shell has a number of variables that exists in any shell session, such as RANDOM
and EUID
, regardless of whether these are used or not. A variable may also not be available in the current scope when declare
is called, for example if it's a local variable in a function, or if it has been unset
, or if it was declared in a sub-shell that is no longer active.
Would you want to see the variables created in a particular script, you would have to save the output of e.g. declare -p
at the start of the script and later compare that with another invocation of the same command at the end of the script (or wherever you'd like to investigate the currently declared variables).
Example:
#!/bin/bash
tmpfile=$(mktemp)
declare -p >"$tmpfile"
say_hello() {
name="$1"
echo "Hello $name"
}
my_name="Luis Daniel"
my_age="29"
say_hello "$my_name"
declare -p | diff "$tmpfile" -
rm -f "$tmpfile"
Running it:
$ bash script.sh
Hello Luis Daniel
46c46,49
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
> declare -- name="Luis Daniel"
Note that declaring name
as a local
variable in the function would, since it's not available at the second call to declare -p
, produce
Hello Luis Daniel
46c46,48
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
add a comment |
The commands set
or declare
by themselves will print all shell variables and their values (and would additionally output function definitions). declare -p
would not output function definitions but would annotate each variable with its type (e.g. -r
for read-only, -a
for array, etc.) The export
command by itself will print environment variables (exported shell variables), as would env
and printenv
.
Whether the variables are used or not within the current shell session will not necessarily be detected in the output of these commands. The bash
shell has a number of variables that exists in any shell session, such as RANDOM
and EUID
, regardless of whether these are used or not. A variable may also not be available in the current scope when declare
is called, for example if it's a local variable in a function, or if it has been unset
, or if it was declared in a sub-shell that is no longer active.
Would you want to see the variables created in a particular script, you would have to save the output of e.g. declare -p
at the start of the script and later compare that with another invocation of the same command at the end of the script (or wherever you'd like to investigate the currently declared variables).
Example:
#!/bin/bash
tmpfile=$(mktemp)
declare -p >"$tmpfile"
say_hello() {
name="$1"
echo "Hello $name"
}
my_name="Luis Daniel"
my_age="29"
say_hello "$my_name"
declare -p | diff "$tmpfile" -
rm -f "$tmpfile"
Running it:
$ bash script.sh
Hello Luis Daniel
46c46,49
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
> declare -- name="Luis Daniel"
Note that declaring name
as a local
variable in the function would, since it's not available at the second call to declare -p
, produce
Hello Luis Daniel
46c46,48
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
add a comment |
The commands set
or declare
by themselves will print all shell variables and their values (and would additionally output function definitions). declare -p
would not output function definitions but would annotate each variable with its type (e.g. -r
for read-only, -a
for array, etc.) The export
command by itself will print environment variables (exported shell variables), as would env
and printenv
.
Whether the variables are used or not within the current shell session will not necessarily be detected in the output of these commands. The bash
shell has a number of variables that exists in any shell session, such as RANDOM
and EUID
, regardless of whether these are used or not. A variable may also not be available in the current scope when declare
is called, for example if it's a local variable in a function, or if it has been unset
, or if it was declared in a sub-shell that is no longer active.
Would you want to see the variables created in a particular script, you would have to save the output of e.g. declare -p
at the start of the script and later compare that with another invocation of the same command at the end of the script (or wherever you'd like to investigate the currently declared variables).
Example:
#!/bin/bash
tmpfile=$(mktemp)
declare -p >"$tmpfile"
say_hello() {
name="$1"
echo "Hello $name"
}
my_name="Luis Daniel"
my_age="29"
say_hello "$my_name"
declare -p | diff "$tmpfile" -
rm -f "$tmpfile"
Running it:
$ bash script.sh
Hello Luis Daniel
46c46,49
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
> declare -- name="Luis Daniel"
Note that declaring name
as a local
variable in the function would, since it's not available at the second call to declare -p
, produce
Hello Luis Daniel
46c46,48
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
The commands set
or declare
by themselves will print all shell variables and their values (and would additionally output function definitions). declare -p
would not output function definitions but would annotate each variable with its type (e.g. -r
for read-only, -a
for array, etc.) The export
command by itself will print environment variables (exported shell variables), as would env
and printenv
.
Whether the variables are used or not within the current shell session will not necessarily be detected in the output of these commands. The bash
shell has a number of variables that exists in any shell session, such as RANDOM
and EUID
, regardless of whether these are used or not. A variable may also not be available in the current scope when declare
is called, for example if it's a local variable in a function, or if it has been unset
, or if it was declared in a sub-shell that is no longer active.
Would you want to see the variables created in a particular script, you would have to save the output of e.g. declare -p
at the start of the script and later compare that with another invocation of the same command at the end of the script (or wherever you'd like to investigate the currently declared variables).
Example:
#!/bin/bash
tmpfile=$(mktemp)
declare -p >"$tmpfile"
say_hello() {
name="$1"
echo "Hello $name"
}
my_name="Luis Daniel"
my_age="29"
say_hello "$my_name"
declare -p | diff "$tmpfile" -
rm -f "$tmpfile"
Running it:
$ bash script.sh
Hello Luis Daniel
46c46,49
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
> declare -- name="Luis Daniel"
Note that declaring name
as a local
variable in the function would, since it's not available at the second call to declare -p
, produce
Hello Luis Daniel
46c46,48
< declare -- _=""
---
> declare -- _="Luis Daniel"
> declare -- my_age="29"
> declare -- my_name="Luis Daniel"
edited Mar 5 at 23:12
answered Mar 5 at 22:05
Kusalananda♦Kusalananda
138k17258428
138k17258428
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%2f504583%2fprint-all-variables-used-within-a-bash-script%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
It's unclear whether you're interested in creating that particular output using e.g. three calls to
echo
, or whether you're interested in a more generic solution (possibly some form of shell script debugging tool?).– Kusalananda♦
Mar 5 at 22:18