64

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

Saikat
  • 14,222
  • 20
  • 104
  • 125
Mike
  • 58,961
  • 76
  • 175
  • 221
  • 24
    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? – Tim Post Mar 19 '10 at 13:49
  • What's the problem ? A keyboard where '{' is invisible / inaccessible ? – Frédéric Grosshans Mar 19 '10 at 13:58
  • 2
    @Tim .. my situation was almost that bad. But now, I'm OK – Mike Mar 19 '10 at 13:59
  • 9
    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. – Mike Mar 19 '10 at 16:22
  • 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. – sam hocevar Jan 30 '19 at 16:32

7 Answers7

84

You can do:

printenv VARIABLE_NAME
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
13

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

ASR
  • 3,289
  • 3
  • 37
  • 65
9

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

> FOO=BAR123
> VAR_NAME=FOO
> echo ${VAR_NAME}
FOO
> echo ${!VAR_NAME}
BAR123
Ryan Anguiano
  • 355
  • 4
  • 3
4

Do you mean something like this:

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

This works in plain old Bourne shell.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
3

How about this:

myVariable=$(env  | grep VARIABLE_NAME | grep -oe '[^=]*$');
jens_profile
  • 351
  • 4
  • 11
2

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'
sth
  • 222,467
  • 53
  • 283
  • 367
1
( set -o posix ; set ) | grep $var

search for all unix-compatible format variables been used

  • 1
    Code only answers are discouraged on StackOverflow. Please try to elaborate a little as to why this is a correct answer – Mittal Patel Feb 13 '18 at 04:34
  • 1
    Always remember to add explaination to the answer you post, so the users can understand the use of it. @user3061097 – LuFFy Feb 13 '18 at 06:41