Set Default Arguments with Bash Shell Parameter Expansions

Cameron Nokes
InstructorCameron Nokes

Share this video with your friends

Send Tweet
Published 4 years ago
Updated 3 years ago

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.

Instructor: [00:00] 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.

[00:15] 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.

[00:31] 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.

[00:52] 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.

[01:07] 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.

[01:26] 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.

[01:43] 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.

[02:01] 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.

[02:20] 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.

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

Eliezer Steinbock
Eliezer Steinbock
~ 3 years ago

Why does dir=$1 turn into: dir=${1:-$PWD}. I'm a little confused by the brace ordering. I would have expected something like ${$1 instead of ${1

Cameron Nokes
Cameron Nokesinstructor
~ 3 years ago

Why does dir=$1 turn into: dir=${1:-$PWD}. I'm a little confused by the brace ordering. I would have expected something like ${$1 instead of ${1

I agree the syntax is strange and I don't think I can give you a satisfactory answer without knowing how the bash interpreter works 🤷‍♂️. Remember that the syntax $var is basically equivalent to ${var}, so syntactically, a variable in bash isn't always preceded by a $. The "why" might be covered here more: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html. Sorry, I couldn't be of more help and that's definitely a good question!