How to set environment variables in bash using setenv?

I have a file containing all the environment variables needed to run the application in the following format ...

setenv DISPLAY invest7@example.com setenv HOST example.com setenv HOSTNAME sk ... 

How to install env. variables in bash using the above file? Is there a way to somehow use the setenv command in bash?

+4
source share
3 answers

You can define a function called setenv :

 function setenv() { export "$1=$2"; } 

To install envariables, send the file:

 . your_file 
+6
source

This is an improved version.

 # Mimic csh/tsch setenv function setenv() { if [ $# = 2 ]; then export $1=$2; else echo "Usage: setenv [NAME] [VALUE]"; fi } 
+1
source

Here is a more complete version for ksh / bash. It behaves like csh / tcsh setenv regardless of the number of arguments.

 setenv () { if (( $# == 0 )); then env return 0 fi if [[ $1 == *[!A-Za-z0-9_]* ]]; then printf 'setenv: not a valid identifier -- %s\n' "$1" >&2 return 1 fi case $# in 1) export "$1" ;; 2) export "$1=$2" ;; *) printf 'Usage: setenv [VARIABLE [VALUE]]\n' >&2 return 1 esac } 
0
source

Source: https://habr.com/ru/post/1344661/


All Articles