Ubuntu Server 18.04 – Understanding variables

How to Create a Droplet with DigitalOcean

Bash is more than just a shell. You could argue that it is very similar to a complete programming language, and you wouldn’t be wrong. Bash is a scripting engine (we will get into scripting later) and there are many debates as far as what separates a scripting language from a programming language, and that line is blurred more and more as new languages come out. As with any scripting language, Bash supports variables. The concept of variables is very easy in Bash, but I figured I’d give it its own (relatively short) section to make sure you understand the basics. You can set a variable with a command such as the following:

myvar='Hello world!'

When Bash encounters an equal sign after a string, it assumes you’re creating a variable. Here, we’re creating a variable named myvar and setting it equal to Hello world!¬†Whenever we refer to a variable, though, we need to specifically clarify to Bash that we’re requesting a variable, and we do that by prefixing it with a dollar symbol ($). Consider this command:

echo $myvar

If you’ve set the variable as I have, executing that command will print Hello world! to stdout. The echo command is very useful for printing the contents of variables. The key thing to remember here is that when you set a variable, you don’t include the dollar symbol, but you do when you retrieve it.

You will see variations of variable name formats as you work with various Linux servers. For example, you may see variable names in all caps, camel case (MyVar), as well as other variations. These variations are all valid and depending on the background of the individual creating them (developers, administrators, and so on), you may see different forms of variable naming.

Variables work in other aspects of the shell as well, not just with echo. Consider this:

mydir="/etc"
ls $mydir

Here, we’re storing a directory name in a variable, and using the ls command against it to list the contents of it. This may seem relatively useless, but when you’re scripting, this will save you time. Anytime you need to refer to something more than once, it should be in a variable. That way, in a script, you can change the contents of that variable and everywhere in the script will reference it.

There are also variables that are automatically present in your shell, that you did not explicitly set yourself. Enter this command for fun:

env

Wow! That’s a lot of variables, especially if you enter it in a desktop version of Ubuntu. These variables are set by the system, but can still be accessed via echo as you would any other. Some notable ones include $SHELL (stores the name of the binary that currently handles your shell), $USER (stores your current username), and $HOST (stores the hostname for your server). Any of these variables can be accessed at any time, and may even prove beneficial in scripts.

We’ve already gone over standard output and standard error, but there’s also another we haven’t gone over yet, and that is Standard Input (stdin). Basically, stdin is just a fancy way of describing the process of taking input from a user and storing it in a variable. Try this command:

read age

When you run this command, you’ll just be brought to a blank line, with no indication as to what you should be doing. Go ahead and enter your age, then press Enter. Next, run this:

echo $age

In a script, you would want to inform the user what they should be entering, so you would probably use something similar to these commands:

echo "Please enter your age"
read age
echo "Your age is $age"

We’re getting a bit ahead of ourselves here, but I wanted to at least make sure you understand the concept of standard input, since it directly relates to setting variables as we’re discussing here.

Comments are closed.