Understanding Variable Labels/Values in BASH: The “Newbie” Guide

December 15, 2016 | Views: 3822

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

(Note: This article assumes you know what a terminal is, and are familiar with some basic CLI commands.)

Hello Everyone,

In this topic, I am going to distinguish between Variable labels and Variable Values. This is a very basic fundamental concept. However, thinking back to my first BASH scripting class, many of my fellow students had trouble understanding the difference. So I’m going to my best to explain the difference using examples in laymen’s terms.

I am choosing to use BASH for this example, as it doesn’t require anything more than assigning a value to a label. Now, just what am I talking about.

When you open a terminal you can assign variables like this:  $> MeatDrawer="STEAK"

What the above does is assigns the VALUE “Juicy Steak” to a variable with the LABEL “MeatDrawer”

Now you can ECHO that variable.

$>ECHO "You pulled a nice juicy MeatDrawer out of the fridge"
result
$>You pulled a nice juicy MeatDrawer out of the fridge

What the heck! That’s not what I wanted!

It’s supposed to say “You pulled a nice juicy STEAK out of the fridge!”

Dumb Computer…I said MeatDrawer=”STEAK”…What’s wrong I’m quitting this stupid class. It doesn’t make any sense, and it doesn’t do what I wanted, I don’t get it.

Well, don’t quit just yet. There is a trick…and it’s an important one.

 

To get VALUE out of a VARIABLE you need to include a ‘$’ before the variable name.

$>ECHO $MeatDrawer
result
$>STEAK

The dollar sign before the variable label tells the computer to reach inside the variable with that label and get VALUE out of it.

Now how you would fix our original ECHO line below?

$>ECHO "You pulled a nice juicy MeatDrawer out of the fridge"

 

If you said

$>ECHO "You pulled a nice juicy $MeatDrawer out of the fridge" You’d be correct!

You can think of variables like drawers or even jars with labels on the outside. You can label them whatever you want, but the value is what you decide to put in them. And again, if you want to tell the computer to get the value out of a variable, in BASH you simply put a ‘$’ before the label and it will give you the value.

Here are a few more examples.

$>Jar1="Salt"
$>ECHO "The first jar now contains $Jar1"
(We used the $ to tell the computer to look inside jar1)
$>The first jar now contains Salt

$>Jar2="Pepper"
$>ECHO "The second jar now contains Jar2"

$>The second jar now contains Jar2

(oops, we forgot the dollar sign, computer doesn’t know to look inside and get the value of what’s inside, let’s fix that)

$>ECHO "The second jar now contains $Jar2" (We remembered the dollar sign this time!)
$>The second jar now contains Pepper  (Yay! it worked).

To reiterate, Variable labels are just that, LABELS! The value of a variable is what you put inside that variable.

Hopefully, this has made it a little clearer for some of you and been a refresher for others. Thanks for reading.

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
10 Comments
  1. Aren’t they called “varianle names” not “labels”?

    • I mean “variable names”.

      • It depends on what source you are looking at, my first teacher called them labels, my second called them names. I personally like the use of the word label, because it can help with the concept of an ’empty label’ on a jar. But I’ve seen them used both ways.

  2. Easy and well explained tutorial. Guys please dont think its just a simple tutorial. This is a basic understanding and this “idea” of labeling and pulling out variable from that label works with all the programming languages. What I meant was, the syntax of each programming language is different but this idea applies to all.

    • Thank you. Glad you thought it was easy that’s my goal!. I find in IT often people teach “how” without understanding the concepts.

      A perfect example of that is subnetting, so many people will tell you HOW to do it, and they may give some example of WHEN you do it, but never really answer “why” it works the way it does.

      For example a question I like to ask is “why does a /30 only give you 4 total addresses?”

      you’d be surprised how many people can’t answer that simple question. The answer is

      because it only allows you to play with the last 2 bits. Meaning there is only 4 possible combinations.

      00
      01
      10
      and 11.

      BTW I’m working on a subnetting concepts post as well, where binary is introduced LAST. But that’s a larger post 🙂

  3. Correction

    When you open a terminal you can assign variables like this: $> MeatDrawer=”STEAK”

    What the above does is assigns the VALUE “STEAK” to a variable with the LABEL “MeatDrawer”

  4. Wait a sec… 🙁 i dont get this. and how do you use that?

  5. awesome thank you for sharing this.

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

Get it on Google Play
 

Support Cybrary

Donate Here to Get This Month's Donor Badge

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel