Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Store and Use Values with Bash Variables

    Cameron NokesCameron Nokes

    Bash, like any programming language, can set and unset variables. Global environment variables also play a big role in bash scripting. In this lesson, we'll learn how to declare and use variables, export them to other processes spawned from our current shell, and go through a quick example of how to leverage them in a script.

    bashBash
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 00:00 To set a variable in Bash, we just give it a name. Then we give it an equals sign, and then the value. To inspect to value of that variable, the easiest way is to use echo. Then we reference the variable with the dollar sign and the variable name. Then there's our value.

    00:18 Know that there cannot be any spaces. This is intentional. If we have spaces, like this, it's going to throw a syntax error. The scope of the var variable that we defined is for this Bash session only. This var variable won't be visible to any scripts that are executed in the context of this shell.

    00:37 Let's see that in action. Let's just create a simple script, and we'll just do echo var. Let's give it execute permissions. If we run that, we can see that it echoed something out here, but it was just an empty value.

    00:52 In Bash, you can reference an undefined variable without any problems. It'll just be a blank value. It won't throw any errors, though. Let's see how we would make our var variable visible to our script. The way we do that is we use the export command, and we run export var.

    01:10 This will make the var variable visible to all child processes, to all commands, all scripts that are executed in the context of this shell. If we run script now, we can see that it's visible, and that it echoed it out correctly.

    01:23 We can also unset a variable in Bash. If we did unset var, and then we ran our script again, we can see that it's back to being empty. Bash sets a lot of global environment variables that can be used. To see what all of those are, you run the env command, and it logs each of the variables out.

    01:42 For example, a common one that's used is the user variable. We can see that that's set to the currently logged in user. You can also note that by convention, each of these variables are all uppercased. To reference a global environment variable, it's just the same as any other variable.

    02:01 Let's see how we could use variables in a more realistic script. Let's say that we want to create a script that clones a branch of a local Git repository to a temporary folder. You could use something like this, for example, if you wanted to run some tests, or run a build on a different branch without interrupting your current workflow.

    02:21 Let's create a script we'll call cloneToTemp. The first thing we'll do is we'll create a temp variable. We'll use the command substitution syntax for that, because we're going to run a command in here. We're going to run makeTemp, and we'll pass it the D flag.

    02:37 makeTemp will create a temporary file or folder. The file path to the temporary directory to we created will be stored in this temp variable. Now, we're going to run git clone branch. This tells Git to clone from a branch or a tag.

    02:55 For the branch or tag, we'll use user input. This'll be the first parameter that's passed to this script. Then we'll clone it from the current working directly. We'll use the PWD global environment variable. This is always set to your current working directory.

    03:11 We're cloning from a local Git repository, not from a remote one, so that's why we're just passing in the current working directory here. Then we're going to clone it into our temp folder. Then let's just add an echo statement that just helps verify that it worked.

    03:26 We'll say the branch name was clone to the temporary folder. At this point, in a more realistic scenario, maybe we would run some tasks here, tests, or something like that. Let's give this execute permissions. Now, let's try it out.

    03:45 I do have a Git repository already initialized. Let's just add a commit, and then I'll create a branch that I'll just call new. Now, let's try it out and do a cloneToTemp. Then I'll say clone the new branch, and there you have it. It worked.

    Discuss

    Discuss