Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting environment variables in Linux using Bash

In tcsh, I have the following script working:

#!/bin/tcsh setenv X_ROOT /some/specified/path  setenv XDB    ${X_ROOT}/db setenv PATH   ${X_ROOT}/bin:${PATH}  xrun -d xdb1 -i $1 > $2 

What is the equivalent to the tcsh setenv function in Bash?

Is there a direct analog? The environment variables are for locating the executable.

like image 646
pbh101 Avatar asked Oct 24 '08 18:10

pbh101


People also ask

How do I set an environment variable in shell?

To set an environment variable everytime, use the export command in the . bashrc file (or the appropriate initialization file for your shell). To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work.

Can you set variables in bash?

You can use variables as in any programming languages. There are no data types. A variable in bash can contain a number, a character, a string of characters. You have no need to declare a variable, just assigning a value to its reference will create it.

How do I see environment variables in bash?

The “printenv” command displays the currently active environment variables and the previously specified environment variables in the shell. You can see the output of using the “printenv” command to display all the environment variables in the shell as per the snapshot below.


1 Answers

export VAR=value will set VAR to value. Enclose it in single quotes if you want spaces, like export VAR='my val'. If you want the variable to be interpolated, use double quotes, like export VAR="$MY_OTHER_VAR".

like image 177
mipadi Avatar answered Sep 19 '22 07:09

mipadi