Create and Run Bash Scripts with Command Line Arguments

Cameron Nokes
InstructorCameron Nokes

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 5 years ago

In the lesson, we’ll learn how to encapsulate some logic we want to easily run later in a script. After creating the script, we have to use chmod u+x to give it execute permissions. If we want our script to make use of parameters passed to it when it's invoked, we can reference them using the syntax $1, $2, etc. (also known as positional parameters).

We’ll also learn about what $PATH is, and move our script to a folder already on the $PATH so that it can be executed in any folder in our shell.

Instructor: [00:00] Let's start by creating our Bash script. I'll call mine I'll just use touch to create that. I'm going to use nano to edit it. Nano is a simple Bash text editor, but you can use whatever you want to create the file.

[00:16] In here, I'm just going to do echo hello world. To run a Bash script, we do ./ and then the script name, which is Let's try and run this and see what happens. Permission denied. This is expected.

[00:33] What's going on here? If we do a long listing, this means it's a file. These are the user permissions. My user has permission to read and write to this file, but it doesn't have permission to execute. That might seem kind of weird because my user created the file. That's the default permission set up.

[00:51] To fix that, we're going to run a command called change mode CH mod. We're going to do u+x. The u+x means add to the user execute permissions and then we'll pass our file. If we do that and then we long list it out now, we can see we have that little X there. That means we do have execute permissions.

[01:13] Let's try to run it now again. You can see it works. Now, let's modify it to make it accept command line parameters. I'm going to jump back in here. The way variables work in Bash is that you reference a variable using the dollar sign and then the variable name there.

[01:32] We want to make the greeting here customizable. We'll reference our variable here. In Bash, you pass parameters to your scripts just like you pass parameters to any other command. They're just a space separated list after the command name. We reference each of those parameters that are passed by the argument number. To get the first argument that's passed to our scripts, we're going to use $1.

[01:55] Let's save that. Let's see. We do and then I'll do howdy. Yeah, it works. You can pass as many parameters to your scripts as you want and those will all be referenced by the number argument that they are.

[02:09] Let's do something a little more realistic. Let's write a script that scaffolds out a new JavaScript project for us. We'll call it initJS. For our first line, let's do some logging. We'll do echo initializing JS project at. I want to pass the current working directory that this script is being run in.

[02:29] To do that, I'm going to do dollar sign-because it's going to be a variable. What I'm going to do here print working directory is a command that just outputs the current working directory of your Bash shell. If you do dollar sign and then the parentheses with a command in it, that is basically saying, "Execute this command and just put the output in inline."

[02:48] Now, I want to git init to initialize a git repository. I'll do NPM init. NPM init creates a package.json and the Y flag tells it to do it with just all the defaults and it doesn't prompt me for any input. Actually, we can make a note of that here. In Bash, you can leave a comment with the hashtag symbol. I'll leave a comment here that says create package.json with all the defaults.

[03:13] I like to have a source directory in my projects, make dir source. We'll start things off with an index.js file in the source directory. If you have Visual Studio Code installed, it installs a command line tool called code. If you run code and you pass a directory it will open that in Visual Studio Code.

[03:32] The current working directory can be referenced using the period character. This could be whatever you want. You could also open the source/index.js file using open and open that and just whatever your default application is for JavaScript files.

[03:47] This is looking pretty good. I'm going to save it. Let's add execute permissions to our scripts. U+x and then if we run that, let's just run it in our current folder to see if it works. It looks like it works and it opened up code for me. This is great, but what would make it better is if you could run this command at any folder on your file system that you wanted.

[04:10] Right now if you wanted to run this in a different folder, you'd have to copy the script there and then execute it there and then probably remove the script afterwards. We can add the scripts to our path. By path, I mean that path. That will make our scripts available to run in our Bash shell anywhere we want.

[04:26] Let's see what a path is. If you do echo path, it will output this big long string for you. What this is is a colon separated list of folders of where your shell looks for executables. For example, if I run which node, node meaning node.js. Which is a command that just tells you where this executable lives. You can see that it lives in this NVM folder. You can see that that same folder is right here above. That's a place where Bash sees that that's an executable. When I run node it knows to run node.js.

[05:04] There's a lot of different ways you can add something to your path to make it executable anywhere you want. I'm going to do what I think is the simplest, which is I'm just going to copy our sh file and I'm going to put it in user local bin. I'm just going to call it initJS. If we do which initJS, you can see now that it's seeing it in our user local bin folder which is on our path.

[05:29] Let's create a test directory. We'll cd into it and let's do initJS. We can see that our script runs and it opens again in code there.

Dominik Loncar
Dominik Loncar
~ 4 years ago

Nice! What other bash scripts are usfeul for devs?

Cameron Nokes
Cameron Nokesinstructor
~ 4 years ago

Nice! What other bash scripts are usfeul for devs?

The possibilities are endless! For example: or are just recent examples I came across.