Accessing system wide environment variables inside a bash script - bash

I've added a system wide environment variable in /etc/environment. I want to access this variable in a bash script. Is there any way to access it?

Assuming that PATH is an environmental variable, also in /etc/environment, I can access path in a script like this:
#!/bin/sh
echo $PATH
So what's wrong with your variable?

Related

Access custom environment variable from Jenkins file

I need to access system environment variable from my Jenkins file. I know that there are some predefined variables (e.g. JOB_NAME or BUILD_NUMBER), but I need to access custom environment variable which I set previously. What are the way to do this? It seems that env.MY_VARIABLE and env['MY_VARIABLE'] but those don't work. I need this to have access to the variable which would be specified during the pipeline build inside a bash script. Probably there are more convenient ways to pass information from bash script to Jenkins file, which called this bash script.
You access environment variables like ${DB_ENGINE} or $DB_ENGINE from bash or in your Groovy job/pipeline DSL script where DB_ENGINE is the custom environment variable you set.
Check documentation.

Finding all the Enviorment Variables in Bash

I recently got a book on shell scripting in bash. It states to list all the Environment Variables using the printenv command. I've noticed though that this doesn't list all variables, for example $PWD or $REPLY or $SSH_TTY. Is their a complete list I can reference for all of these Environment Variables and their Functions?
Within a shell like bash there are two types of variables; environment variables (Wikipedia) and shell variables. There are a number of predefined shell variables.
You can use the export built-in to "promote" a shell variable to an environment variable, which has the effect of making that variable available to any subprocesses launched from the shell.
As the name implies, printenv only reports the process' environment variables. Variables like PWD or REPLY are shell variables, and thus aren't displayed. As suggested in the comments, invoking set with no arguments will print all variables (environment and shell) available in your current session.
To display a list of the Environment Variables you can use
set
#if you want to see it nicely you can pipe it to more like this
set | more

Can't get environment variable from /etc/profile in GoLang

I've set some environment variables in /etc/profile, I can access them from bash, but for some reason I cant get them from Go.
/etc/profile:
...
TEST_ENV=test_me
I can access it from bash:
echo $TEST_ENV
test_me
I can't access this variable from GO
os.Getenv("TEST_ENV") // returns ""
If I list the available environment variables with
os.Environ()
I don't see the variable I'm looking for, but there a few variables that might help:
SHELL=/bin/sh
USER=root
LOGNAME=root
I guess my problem is related to different sessions and shells, so I even tried running
exec.Command("source /etc/profile")
and get the variables after, but it still returns nothing.
Can you give me some tips how to get environment variables if they're set in /etc/profile? I'd prefer getting them from that file, but if necessary, I can put the variables in a different place as well.
When you set an environment variable in bash, by default it isn't exported. Only exported environment variables are passed along to processes created by the shell (i.e., programs that you run). Try export TEST_ENV=test_me.

How to display jenkins default environment variable from shell command?

I am not able to get output of the Environment-Variable BUILD_URL through Shell command.
Configured Manage Jenkins-->Configure System-->Jenkins Location-->Jenkins URL as http://xxx.xxx.xxx.xxx:8080/
Using below script within Shell command.
#!/bin/bash
echo ${BUILD_URL}
Do i have to set jenkins_url within the script?
In a Shell you can't access environment variables like that. you will need to read here: https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps.
What you can do is in the Jenkins's job session you can send a parameter to your script to gain access to the env parameter. in the Shell section you will have access to the environment parameter, in the script itself it'll work differently.
Good luck

Restrict Specific Variable In Bash

I have a script that sets various variables.
maxMemSize=8g
poolThreadCount=4
...
I have to execute this script on multiple system, all of them running CentOS. Now what is required is to restrict setting of some of these variable on some of the systems. For example on system1, maxMemSize should not be set, while on system2 setting poolThreadCount should be restricted.
Like the way we can set readonly environmental variables to restrict export for those specific variables, can we do some similar trick to restrict setting specific variables in bash scripts too?
To restrict variable re-definition you can set it as read only in your ~/.bashrc or in any other file, lets say ~/.readonly:
readonly maxMemSize=3g
readonly poolThreadCount=2
Now you need to tell your bash which init file should run before it start execution. This is defined by a environmental variable $BASH_ENV:
export BASH_ENV=~/.bashrc
or
export BASH_ENV=~/.readonly
Now execute your script under bash
bash initThreadRead.sh

Resources