Print all variables used within a bash script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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









share|improve this question
























  • 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
















0















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









share|improve this question
























  • 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














0












0








0








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















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











1 Answer
1






active

oldest

votes


















3














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"





share|improve this answer

























    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
    );



    );













    draft saved

    draft discarded


















    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









    3














    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"





    share|improve this answer





























      3














      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"





      share|improve this answer



























        3












        3








        3







        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"





        share|improve this answer















        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"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 5 at 23:12

























        answered Mar 5 at 22:05









        KusalanandaKusalananda

        139k17259430




        139k17259430



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay