One of the biggest differences between tcsh and bash lies in the way they each handle variables. You have two kinds of variables to work with: shell variables and environment variables (you'll learn more about these variables in Chapter 10, "Shell Programming"). Let's take a look at how the variable types differ and where they're used. Environment VariablesNo matter which shell you're using, you always have environment variables (variables containing values that travel with you throughout your login session and are propagated to any program spawned from within that session). Environment variables dictate the behavior of certain programs that you run. For instance, the chfn program launches the text editor specified in the EDITOR environment variable, and the TERM variable tells the shell how to format text to display properly on your screen. The BLOCKSIZE variable controls what the output numbers in commands such as du and df signify (kilobytes, half-kilobytes, or whatever the value of the variable is). A program doesn't have to be a login shell to have access to environment variablesevery program has access to the same set of variables as the program that spawned it. Whichever shell you're using, you can view all your environment variables using the printenv command, as shown in Listing 9.3. Listing 9.3. Sample Output of the printenv Command
Environment variables don't follow any hard-and-fast rules; there are certain traditional variables that every program expects to be able to read, such as USER and PATH, but you can make up any variable that you want and set it to any arbitrary value, for any purpose you might dream up. To set an environment variable from within the shell, you would use one of two syntaxes, depending on the shell you're using. To set a hypothetical variable called COLOR to gold in tcsh, enter the following: # setenv COLOR gold In bash, you have to first set the variable in the local session context and then export it to the shell's environment, as shown here: # COLOR=gold # export COLOR Note that an environment variable must have a value; it can't simply exist in the environment with a null or undefined value. Shell VariablesIn addition to environment variables, another separate set of variables are available to your login session. These variables, called shell variables, apply only to the current login session and have no bearing on any other processes. They're comprehensible only to the shell process itself. In programming terms, shell variables exist in the "local" context, whereas environment variables exist in the "global" context and can be inherited by other programs. Shell variables in tcsh are traditionally named in lowercase, and shell variables in bash are traditionally named in uppercase, but this is not required. You can view shell variables using the set command. Setting a shell variable in tcsh is also done, not surprisingly, with set. It's possible to set a shell variable in tcsh without assigning a value to it: # set history=100 # set noclobber In bash, you simply set the variable without exporting it: # VISUAL=pico You can also remove a variable with unset, as shown here: # unset autologout Shell and environment variables, as well as the way their inheritance properties interact and apply to shell scripting techniques, will be described more fully in Chapter 10, where you will learn how to take your interaction with the shell to the next level: writing shell programs. |