Join Scott Simpson for an in-depth discussion in this video System basics: Bash scripting basics, part of Linux Tips Weekly.
- [Scott] The Bash shell is a useful interactive terminal. But you can also put together Bash commands and operators into scripts that can be run as programs. These collections of commands, when they're put together, are called Bash scripts. Bash scripts can be really helpful for packaging a lot of different commands all together to reduce errors in a procedure that you have to do over and over. You can also set them up to take particular input in order to do a procedure that might have some variation to it. A Bash script starts out a line called shebang. It's a special directive that tells the shell where to find the program that's going to run the rest of the script.
The shebang starts with a pound or hash sign and an exclamation mark, and then the path to the interpreter, in this case, usr/bin/env and bash. I'll create a script with a text editor. I'll write nano myscript.sh. And first thing, I'll put in the shebang, pound sign exclamation mark, usr/bin/env bash. Then, on the next line, we can start to build the script. A script can be as simple as a regular command if, for some reason, you want to wrap up something like listing a folder in a script, all the way up to hundreds or thousands of lines to accomplish a very complex procedure.
Let's start simply and just write ls dash l bin, which will show us a long list of the bin directory. I'll save that, and I'll exit. To run the script, I need to tell something to run it. I can write bash and the name of the script to tell Bash to run this script. And there's the output of the list command of the bin directory, and that's a perfectly fine way to run a Bash script. But we can also make the script act more like a program by setting the execute bit to allow users to execute the script. I'll do that with chmod plus x to give everyone permission to execute the script.
And then, to run it from this directory, I'll write dot, slash, and the name of the script, and it runs. The shebang line is telling the system what program to use to execute the script. In this case, it's Bash, and if you need to make other scripts for other shells, you can change to something different in the shebang. You can also write Python scripts or Ruby scripts that execute on their own like this. Let's open up the script and make it a little bit more complex. There's a nearly limitless number of things you can do with a Bash script, and we'll do a few basic things to help you get familiar with how a script can work.
You can use a Bash script to print some output or logic to decide which path to go down. You can assign a value to a variable to use throughout a script. You can ask the user for input in any particular point. And you can get information or arguments from the standard input when the script runs. Let's modify this script to use a variable to ask the user for their name and then to print out some information. First, I'll set a variable by declaring a variable name. I'll call it myvar equal to, let's say, 42. Now, throughout the script, we can use this variable called myvar and get the value, 42, wherever we use it.
Variables are helpful for things like this because you can change the value once, where it's declared, and then have the same value used throughout the script without having to manually update every occurrence. On the next line, let's have the script ask the user for their name with the read command. I'll write read dash p, What is your name? And then, I'll add a space and the variable that I want to store this name in. And then we can use the echo command to write out a line of text. I'll write echo, Hello, and dollar sign name.
This is how we retrieve the value of a variable, with a dollar sign in front of its name. I'll continue the sentence, the meaning of life is, and then we'll use the other variable, myvar. And I'll write, and the date is, and here, I'll use a command substitution with dollar sign, parenthesis, date, and a closing parenthesis. And then, I'll add a closing quote. Here, we're using two variables and command substitution to get the output of the date command and use it in text.
I'll save this script, and then I'll run it with dot slash myscript.sh. I'll type my name, and then I can see that the program as used the variable we set inside of the program and has also used the variable for my name that I just gave it. And it's used the date command to give the current date. I can go back into the script and edit it to change the variable to something a little more tasty. If I run it again, I can see that that change has been incorporated.
Because there's so much you can do with Bash scripting, I want to direct you to our courses that cover it in more depth. You can put together a script using just regular typed commands or you can use Bash language features like conditional statements, logic, and more. If you're going to be administering systems or working at the command line, Bash scripts are a great tool to have on your belt.
Note: Because this is an ongoing series, there is no certificate of completion available for this course.