49

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)

Mike
  • 54,052
  • 71
  • 166
  • 213
  • 14
    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
  • 1
    @Tim .. my situation was almost that bad. But now, I'm OK – Mike Mar 19 '10 at 13:59
  • 7
    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

69

You can do:

printenv VARIABLE_NAME

8

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
  • 2,671
  • 3
  • 30
  • 57
4

Do you mean something like this:

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

This works in plain old Bourne shell.

mouviciel
  • 62,742
  • 10
  • 106
  • 135
4

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
  • 161
  • 2
  • 2
3

How about this:

myVariable=$(env  | grep VARIABLE_NAME | grep -oe '[^=]*$');
jens_profile
  • 342
  • 3
  • 10
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
  • 200,334
  • 49
  • 262
  • 354
0
( set -o posix ; set ) | grep $var

search for all unix-compatible format variables been used

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