UNIX, get environment variable

前端 未结 7 772
滥情空心
滥情空心 2020-12-30 18:41

I have a ridiculous question due to a ridiculous problem.

Normally if I want to get the contents of an environment variable in UNIX shell, I can do

         


        
相关标签:
7条回答
  • 2020-12-30 19:08
    ( set -o posix ; set ) | grep $var
    

    search for all unix-compatible format variables been used

    0 讨论(0)
  • 2020-12-30 19:12

    Do you mean something like this:

    ENV() {
        printf 'echo $%s\n' $1 | sh
    }
    

    This works in plain old Bourne shell.

    0 讨论(0)
  • 2020-12-30 19:14

    You can do:

    printenv VARIABLE_NAME

    0 讨论(0)
  • 2020-12-30 19:14

    type the following command in terminal, it will display all the list of environment variables

    printenv

    now print the wanted variable like this:

    echo $VARIABLENAME

    0 讨论(0)
  • 2020-12-30 19:19

    How about this:

    myVariable=$(env  | grep VARIABLE_NAME | grep -oe '[^=]*$');
    
    0 讨论(0)
  • 2020-12-30 19:26

    The solution really depends on what the restrictions are why you can't use a simple $VAR. Maybe you could call a shell that doesn't have the restrictions and let this sub-shell evaluate the variable:

    bash -c 'echo $VAR'
    
    0 讨论(0)
提交回复
热议问题