Join egghead, unlock knowledge.

Want more egghead?

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

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Set Default Arguments with Bash Shell Parameter Expansions

    Cameron NokesCameron Nokes
    bashBash

    In this lesson, we'll see how shell parameter expansions can be used to simply expand a variable's valuable and also provide a default value to a variable, if not set. Note that there are many more possibilities with shell parameter expansions, so check bash's documentation to view them all.

    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: The most basic form of shell parameter expansion can be used to expand variables. I run echo home. That expands to just the home variable. Of course, in most scenarios, it's easier just to do this, just echo home.

    In case you need to output text without any spaces after the variable, shell parameter expansion can be useful. For example, if I wanted to echo the current username, underscore, and then I'm going to do the current year.

    I do with the date commands like that. We can see it just echoes the year, but where is my user? The problem is that Bash is looking for a variable named user_, which doesn't exist. If we use shell parameter expansion, we can do the user in there, underscore, and then this date, and it works.

    That occasionally has its uses. Another thing we can do with shell parameter expansions is use a variable, and if it doesn't exist, fall back to a default. For example, I'll set up my shell parameter expansion here, and then I'll pass the variable name.

    Then colon-dash, and then the default value here. This is expanded just to default, because there is no string variable. That doesn't exist. It's not set, so it echoes out default. If I were to replace that with a variable that does exist, then that's used instead.

    This can be particularly useful in a script. Let's see how that would work. I'm going to create a small script I'll call count-files.sh. What this'll do is count the number of files in a directory. The first parameter that we pass to this script will be the directory.

    I'll do dir equals the first position parameter to this. Then the command here is going to be find in dir. The type is file, and max depth is one. Then I'll pipe that output to the word count command, and tell it to count the lines.

    This would work if the user of the script always passed this first parameter to it, but if we wanted to say that by default, it'll just find in the current directory, we can use shell parameter expansion here. By default, we'll say use the current working directory.

    Let's save that. We'll give it execute permissions. If we run it in our current directory, we can see we create a number of files in here. We have 14. We can see, I didn't pass a parameter to it, and it just defaulted to using the current working directory.

    Now, let's try passing something. We'll pass the packages folder that we created, and there's no files in there.

    Discuss

    Discuss