![]() |
|
LINUX Bash Variables: Environmental and Otherwise - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Computers (https://sickgaming.net/forum-86.html) +--- Forum: Linux, BSD & Unix (https://sickgaming.net/forum-88.html) +--- Thread: LINUX Bash Variables: Environmental and Otherwise (/thread-87617.html) |
LINUX Bash Variables: Environmental and Otherwise - xSicKxBot - 12-05-2018 Bash Variables: Environmental and Otherwise Bash variables, including those pesky environment variables, have been popped up several times in previous articles, and it’s high time you get to know them better and how they can help you. So, open your terminal window and let’s get started. Environment Variables Consider Code: HOMECode: echo $HOMEThis will show the path to your home directory, usually /home/. As the name indicates, variables can change according to the context. Indeed, each user on a Linux system will have a Code: HOMECode: HOME=/home/<your username>/Documentswill make Code: HOMEThere are three things to notice here:
Changes to environment variables like the one described in point 3 above are not permanent. If you close your terminal and open it back up, or even open a new tab in your terminal window and move there, Code: echo $HOMEBefore we go on to how you make changes permanent, let’s look at another environment variable that it does make sense changing. PATH The Code: PATHCode: PATHHave a look inside Code: PATHCode: $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbinEach directory is separated by a colon ( Code: :Code: PATHCode: /home/<user name>/bin/my_program.shThis will run a program calle my_program.sh you have copied into a bin/ directory in your home directory. This is a common problem: you don’t want to clutter up your system’s bin/ directories, or you don’t want other users running your own personal scripts, but you don’t want to have to type out the complete path every time you need to run a script you use often. The solution is to create your own bin/ directory in your home directory: Code: mkdir $HOME/binAnd then tell Code: PATHCode: PATH=$PATH:$HOME/binAfter that, your /home//bin will show up in your Code: PATHTo make changes permanent for your user, instead of running them directly in the shell, put them into a file that gets run every time a shell is started. That file already exists and lives in your home directory. It is called .bashrc and the dot in front of the name makes it a hidden file — a regular Code: lsCode: ls -aYou can open it with a text editor like kate, gedit, nano, or vim (NOT LibreOffice Writer — that’s a word processor. Different beast entirely). You will see that .bashrc is full of shell commands the purpose of which are to set up the environment for your user. Scroll to the bottom and add the following on a new, empty line: Code: export PATH=$PATH:$HOME/binSave and close the file. You’ll be seeing what Code: exportCode: sourceCode: source .bashrcWhat Code: sourceFrom now on, your shell will find every program you dump in /home//bin without you having to specify the whole path to the file. DYI Variables You can, of course, make your own variables. All the ones we have seen have been written with ALL CAPS, but you can call a variable more or less whatever you want. Creating a new variables is straightforward: just set a value within it: Code: new_variable="Hello"And you already know how to recover a value contained within a variable: Code: echo $new_variableYou often have a program that will require you set up a variable for things to work properly. The variable may set an option to “on”, or help the program find a library it needs, and so on. When you run a program in Bash, the shell spawns a daughter process. This means it is not exactly the same shell that executes your program, but a related mini-shell that inherits some of the mother’s characteristics. Unfortunately, variables, by default, are not one of them. This is because, by default again, variables are local. This means that, for security reasons, a variable set in one shell cannot be read in another, even if it is a daughter shell. To see what I mean, set a variable: Code: robots="R2D2 & C3PO"… and run: Code: bashYou just ran a Bash shell program within a Bash shell program. Now see if you can read the contents of you variable with: Code: echo $robotsYou should draw a blank. Still inside your bash-within-bash shell, set Code: robotsCode: robots="These aren't the ones you are looking for"Check Code: robotsCode: $ echo $robots
These aren't the ones you are looking forExit the bash-within-bash shell: Code: exitAnd re-check the value of Code: robotsCode: $ echo $robots
R2D2 & C3P0This is very useful to avoid all sorts of messed up configurations, but this presents a problem also: if a program requires you set up a variable, but the program can’t access it because Bash will execute it in a daughter process, what can you do? That is exactly what Code: exportTry doing the prior experiment, but, instead of just starting off by setting Code: robots="R2D2 & C3PO"Code: export robots="R2D2 & C3PO"You’ll notice that, when you enter the bash-within-bash shell, Code: robotsInteresting fact: While the daughter process will “inherit” the value of an exported variable, if the variable is changed within the daughter process, changes will not flow upwards to the mother process. In other words, changing the value of an exported variable in a daughter process does not change the value of the original variable in the mother process. You can see all exported variables by running Code: export -pThe variables you create should be at the end of the list. You will also notice some other interesting variables in the list: Code: USERCode: PWDCode: OLDPWDCode: cd -You will go back to the last directory you visited and Code: cdCode: OLDPWDYou can also see all the environment variables using the Code: envTo un-export a variable, use the Code: -nCode: export -n robotsYou have now reached a level in which you are dangerous to yourself and others. It is time you learned how to protect yourself from yourself by making your environment safer and friendlier through the use of aliases, and that is exactly what we’ll be tackling in the next episode. See you then. |