read bash environment variable
@FatalError — Actually there are two lines with this string. The first one assigns a value, but the second one lacks the assignment operator, so it should do nothing.
The brackets [] seem to cause the problem. Do you really need them in the name? I don’t think Bash supports arrays with a non-numeric index.
yeah the environment variables are given out by the proxy, so it has the brackets..i can read all other non-bracket fields fine..
What do you mean exactly with «given out by the proxy»? Does the proxy run bash for you? Does it assign to the environment? Or does it return a buch of text?
4 Answers 4
You need to source your script instead of running it.
this didnt work, the scenario is that the proxy dumps all these environment variables and i am trying to read them..
You can not use braces in form $ . But you can always extract value of these variables to variables with VALID names:
$ set | sed -n '/CLIENT\[user-agent]=/'
Why you don’t use Perl/Python for scripting? This resolve your problem:
import os print(os.environ['CLIENT[user-agent]'])
Probably you should declare the array CLIENT before assigning to its elements:
Can you edit the script assigning all the values to the variables marked for export to subshells? Can you create a temporary file. Bash can’t export arrays, associative or indexed; but you can use the BASH_ENV variable to name a set-up file for non-interactive subshells. Be sure to use declare -A CLIENT before assigning values. Then add something like this after all assignments to the CLIENT array
declare -A -p CLIENT > /tmp/bash_env_tmp export BASH_ENV=/tmp/bash_env_tmp
A new non-interactive subshell will read and execute the file before processing its commands. If the shell is invoked with the -c option, then you can use a command substitution with on the line calling the subshell to supply the output of the same declare operation as the first commands in the new shell.
How to Print Environment Variables in Linux
Wondering what environment variables are set on your current shell? Learn how to print environment variables in Linux.
Environment variables are specific to certain environments. A generic answer, right?
But really, those are the variables that are just specific to your current system environment such as the currently logged-in user will be stored inside the «USER» variable.
Still confused? no worries. I will walk you through a brief understanding of environment variables and then jump to various ways to print them.
What are Environment Variables?
Nope, it’s not related to your Desktop Environment.
One of the most basic environment variables that you encounter on a daily basis is Hostname. So have you ever wondered why it’s always written in all caps?
Because most of the environment variables are pre-defined by the system and are global variables so you’ll encounter them often written in all caps.
So why use environment variables in the first place?
Suppose you are a programmer and your code needs to have access to your database key which should never be shared in public.
So what options do you have while sharing the entire code on Git? Wrap the database key to the environment variable.
This way you can set instructions on Git as «if you want to make this code work, interchange this variable with your database key.»
Of course, this was the one way of using environment variables. So let’s have a look at some common environment variables in Linux.
Environment Variable | Description |
---|---|
HOME | Shows home directory for the current user |
HOSTNAME | Contains Hostname of your system |
UID | Stores unique ID of the user |
SHELL | Shows the path to the shell which is being used currently |
BASH_VERSION | Contains version currently used bash instance |
HISTFILE | Path of the file where the history of commands is saved |
TERM | Shows the type of log-in terminal you’re using |
PATH | Shows path of commands and directories are separated by columns |
Print Environment Variables in Linux
There are various ways through which you can print environment variables in Linux and I’ll try to cover each of them. So let’s start with the easiest one.
Using printenev Command
The printenv utility is used to print environment variables for the current shell.
Suppose I want to print the value of USERNAME using printenv so my command will be as follows:
Similarly, you can also use the printenv utility to print more than one environment variable by separating them with .
So how about printing the value of HOME and USERNAME at once using printenv?
First, it’ll print the home directory of the currently logged-in user and in the second line, the hostname will be there.
But what if you are looking for a way by which you can print each environment variable available in the current shell?
You can easily accomplish this by executing the printenv command without any options or arguments whatsoever.
Using echo Command
If you’ve been using Linux for a while, the chances of using this method to print environment variables are quite high (even if you were unaware that it’s not an environment variable).
Now, let me show you how you’re supposed to print the value of USERNAME with echo:
But what if you want to print multiple environment variables using echo? just follow the given syntax:
So how about printing values of HOME, USERNAME, and HOSTNAME all at once using echo?
echo -e "$USERNAME \n$HOME \n$HOSTNAME"
Using env Command
The env command is generally used by shell scripts to launch the correct interpreter but you can also use the env command to list available environment variables.
The env command if applied without any arguments and options, will print all the available environment variables.
But what if you want to get the value of a specific environment variable? you can easily filter results with the grep command.
For demonstration, I’ll be printing out the value of HOME with the echo command:
Using declare Command
The declare command is used to declare and print the value of variables in the shell.
Like any other example shown above, you can use declare without any argument and will bring the list of available variables.
Previously, I showed you how you can use the grep command to filter results but what if you want to filter more than one result?
First, let’s have a look at the syntax for filtering multiple environment variables:
For example, let’s print the value of HOSTNAME and USERNAME:
declare | grep 'HOSTNAME\|USERNAME'
Using set Command
Generally, the set command is used to set and unset flags in the shell so that you determine the exact behavior for the upcoming process.
But you can also use the set command to print environment variables for the current shell.
To print each environment variable, use the given command:
You can use the same approach used in the above method to filter results from the set command.
But still, let’s look at a simple example using the grep command to print multiple environment variables.
set | grep 'HISTFILESIZE\|HISTFILE\|GNOME_SHELL_SESSION_MODE'
Wrapping up
Though there are various methods for printing environment variables, I’d highly suggest you use the first method the syntax is the least complicated in terms of filtering specific variables.
And if you are still confused, the comments section is open to you.