UNIX, get environment variable
Asked Answered
R

7

67

I have a ridiculous question due to a ridiculous problem.

Normally if I want to get the contents of an environment variable in a 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).

Racialism answered 19/3, 2010 at 13:32 Comment(5)
Mike, are you ok? Are you being held with a gun to your head by the mafia in some warehouse? Is this a plea for help?Arciniega
What's the problem ? A keyboard where '{' is invisible / inaccessible ?Jalbert
@Tim .. my situation was almost that bad. But now, I'm OKRacialism
short answer, emergency bug fix at work. the practices at this place are terrible (understatement of the year). the purpose was to ensure a user wasn't seeing a password that was supposed to be hidden. the previous developer had set up the design so that it passed a plain-text password through stdin to the application. i get sick just thinking of it. anyway, i was able to use skwllsps suggestion. a horrible fix to a horrible problem resulting in a horrible company making horrible software.Racialism
Another situation where this is useful: on Windows, environment variables may contain parentheses, and you cannot do e.g. echo ${ProgramFiles(x86)} because the shell will try to attempt a subsitution. However printenv 'ProgramFiles(x86)' works.Latton
E
89

You can do:

printenv VARIABLE_NAME
Exceptional answered 19/3, 2010 at 13:33 Comment(1)
Ooh, something out of /usr/ucb. Impressive.Muller
G
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

Gumboil answered 1/8, 2014 at 11:18 Comment(0)
C
11

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

> FOO=BAR123
> VAR_NAME=FOO
> echo ${VAR_NAME}
FOO
> echo ${!VAR_NAME}
BAR123
Coactive answered 26/3, 2018 at 17:58 Comment(0)
G
4

How about this:

myVariable=$(env  | grep VARIABLE_NAME | grep -oe '[^=]*$');
Greg answered 19/3, 2010 at 13:36 Comment(1)
actually you're probably better off using cut at the end e.g. ... | cut -d '=' -f2-Greg
B
4

Do you mean something like this:

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

This works in plain old Bourne shell.

Bridget answered 19/3, 2010 at 13:40 Comment(1)
I'd use more quoting myself: ENV() { printf 'printf "%%s\\n" "${%s}"\n' "$1" | sh; }Moss
B
3

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'
Barbwire answered 19/3, 2010 at 13:56 Comment(0)
B
1
( set -o posix ; set ) | grep $var

search for all unix-compatible format variables been used

Bobcat answered 13/2, 2018 at 4:25 Comment(2)
Code only answers are discouraged on StackOverflow. Please try to elaborate a little as to why this is a correct answerBelshazzar
Always remember to add explaination to the answer you post, so the users can understand the use of it. @BobcatHettiehetty

© 2022 - 2024 — McMap. All rights reserved.