Essential Scripting Basics: Variables, Environment Variables, and Data Types

A+ Core 2 · Operational Procedures · By Super Admin

Master the fundamentals of variables in scripting, including data types, environment variables, and the differences from strongly typed languages.

Understanding Variables in Scripting

A variable is a symbolic word or combination of letters that can be used to hold a value. The value can be defined as a number, text, Boolean, or array. Numbers can be either integers or floating-point, but the scripting languages you choose must support floating-point math. Floating-point math is a value that contains a decimal number, whereas integers are whole numbers. Text is also called a string and has no numeric equivalent. A string containing 30 will not be a value of 30; it will be only be a string of characters containing a 3 and a 0. Mathematical computations cannot be performed on a string. Boolean values are true or false values, and true values and arrays are collections of strings, numbers, and Boolean values.

The name of a variable should have some meaning and cannot be a reserved word. So if you had a variable that needed to hold a counter, you could simply create a variable called count. However, you would need to ensure that count was not used elsewhere in the program for a function; otherwise, it's a reserved word in the scripting language and cannot be used as a variable. In PowerShell, the syntax dictates that you place a $ in front of the variable in order to notify the interpreter that you are addressing a variable. Each scripting language is slightly different, but the concept remains the same.

Dynamic Typing Versus Strong Typing

You might see a programming language described as a strongly typed language. That means that the variable must be defined as to what type of value it will hold and how long the value will be. This is yet another difference between scripting languages and programming languages: scripting languages do not need variables to be defined and are normally not typed. Most of the time, you can just load a variable with a value and it is dynamically typed. This is not the most economical use of memory, but that shouldn't be an issue, because scripts are often simple ways to complete a simple task; therefore, it doesn't matter that loading a variable with a value and having it dynamically typed isn't the most economical use of memory.

Generally, you load a value by specifying the variable, followed by an equal sign, and then the value. You can also view the contents of the variable either by typing the variable name and pressing Enter or by using the echo command before the variable. This flexibility in variable handling makes scripting languages more forgiving for beginners and faster for experienced developers working on automation tasks.

Environment Variables and Their Hierarchy

As previously mentioned, variables hold values in scripts so that we can do things such as counting. Environment variables also hold values, but they are used for the environment of either the system or the current user. Environment variables often hold values like the path to an executable or the location of the temporary folder. Environment variables are inherited in a structured fashion: system, user, and then program. They can be overwritten at any underlying level, but only for that entity.

For example, if a script changes the user environment variable for the temporary folder, the change is applicable only for that script unless the user environment is changed—then it will affect all scripts run by a user. System variables are defined for use by the system, or any user or program. User variables are defined for use by a specific user, or any program executing under that specific user. Program variables are defined for use inside a script or application and can only be accessed from inside the script or application. This hierarchical structure ensures that changes at one level don't inadvertently affect other levels unless specifically intended.

0.0 (0 ratings)

Discussion (0)

No discussion on this article yet — be the first to comment.

Please sign in to join the discussion.