UNIX, get environment variable

UnixShellEnvironmentEcho

Unix Problem Overview


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

echo ${VAR}

Let's assume, due to my ridiculous situation, that this isn't possible.

How do I get the contents of an environment variable to stdout, without someone who is looking at the command itself (not the output), see the value of the environment variable.

I can picture the solution being something like echo env(NAME_OF_VAR) although I can't seem to find it. The solution has to work in sh.

PS I can't write a script for this, it must be a built in unix command (i know, ridiculous problem)

Thanks (and sorry for the absurdity)

Unix Solutions


Solution 1 - Unix

You can do:

printenv VARIABLE_NAME

Solution 2 - Unix

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

Solution 3 - Unix

Using ${!VAR_NAME} should be what you are looking for

> FOO=BAR123
> VAR_NAME=FOO
> echo ${VAR_NAME}
FOO
> echo ${!VAR_NAME}
BAR123

Solution 4 - Unix

Do you mean something like this:

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

This works in plain old Bourne shell.

Solution 5 - Unix

How about this:

myVariable=$(env  | grep VARIABLE_NAME | grep -oe '[^=]*$');

Solution 6 - Unix

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'

Solution 7 - Unix

( set -o posix ; set ) | grep $var

search for all unix-compatible format variables been used

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeView Question on Stackoverflow
Solution 1 - Unixuser184968View Answer on Stackoverflow
Solution 2 - UnixASRView Answer on Stackoverflow
Solution 3 - UnixRyan AnguianoView Answer on Stackoverflow
Solution 4 - UnixmouvicielView Answer on Stackoverflow
Solution 5 - Unixjens_profileView Answer on Stackoverflow
Solution 6 - UnixsthView Answer on Stackoverflow
Solution 7 - Unixuser3061097View Answer on Stackoverflow