Basic Tips for New Linux Users

Every shell is different. For the answers below, I'm assuming the user is using the Bourne-again shell (BASH).

What are environment variables and how do I use them?

Environment variables are variables exposed to the shell by the OS. Users can observe the value in an environment variable by using the syntax, $VARNAME, where VARNAME is the variable of interest. Typically, environment variables will contain details about the system on which the shell is running as well as information about the user and their account. This is useful to allow scripts to identify the user running the script, where their home directory is, as well as the type of system it's running on.

Users can modify the variable's setting in bash using simple assignment: MY_TEMP=$HOME/tmp

This assignment would create a variable, MY_HOME with the value of $HOME/tmp in it. When later the user referred to $MY_TEMP, the she would return their home directory/tmp.

cd $MY_TEMP

would attempt to change the current working directory to the directory tmp inside the user's home directory.

Simple assignment is only valid for the current process. If the user establishes a child process they would like their new setting to be available, they should use export: export MY_TEMP=$HOME/tmp

In this case, if the user were to launch a script which used the variable MY_TEMP, the variable would be available.

Users can make even more permanent changes to their environment variables by adding an export for the correct setting in their $HOME/.bash_profile or $HOME/.bashrc file.

How do I set it up so I don't have to type the complete path each time I wish to use a program on a linux machine?

The OS uses the user's PATH variable to determine where to look for executables that don't have an explicit path in the filename. This variable is expected to be a list of one or more complete directories separated by a colon character (":"). It will check each directory in the order of their presence in PATH's value until it either finds a suitable program name or it reaches the end of the list of directories.

Users can modify the PATH variable just like any other environment variable either directly using "export" or by adding an export to their bashrc or bash_profile files.

Why is it bad to put "." in my PATH?

In linux terminology, the dot, ".", represents the current directory. Many new users find it annoying that a program that resides in the same directory in which they are working won't run without appending the ./ to the front. Once they learn about the PATH variable, the very next thing they want to do is add "." to their PATH to avoid typing in those extra two characters.

While it won't cause trouble for most, it could result in accidental execution of scripts or programs or scripts that reside in the current directory that happen to share the name of a program that the system has elsewhere.

-- EricTorstenson - 02 May 2012
Topic revision: r2 - 03 May 2012, EricTorstenson
 

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback