From the course: Learning Bash Scripting
Understanding Bash script syntax - Bash Tutorial
From the course: Learning Bash Scripting
Understanding Bash script syntax
- We'll often type commands one by one in the shell, but this process can be time consuming, error prone and just annoying if you have to run the same handful of commands over and over. There's two ways to consolidate individual commands and start to build distributable repeatable ways of running Bash commands. The first of these are what are called one-liners and they're just what they sound like, a command or series of commands presented as one line of text. These are usually command pipelines or a series of separate Bash commands separated by semicolons. Often these single lines of Bash run for hundreds of characters so sometimes when we write or see a one-liner it'll wrap around in a terminal, but that's okay. It's still a one-liner because it doesn't contain any line breaks to separate the script multiple lines. This makes one-liners suitable for copying and pasting into a terminal. Many developers and CIS admins keep a collection of useful one-liners in a notes document for taking care of frequent tasks or to give themselves the opportunity to customize something they do for frequently. One-liners are also useful because they can be used with the alias built in, providing a way to make a short command that runs with some Bash commands. The other common way of combining Bash commands together in a way that's easier to work with and distribute is to make Bash scripts. A Bash script is a text file that contains one or more Bash commands intended to be run like a program. There's two ways of running a Bash script. The first is to use the Bash command and the name of the script. That tells Bash to run the contents of the text file. When we do this, it's common to add a .sh extension on the end of the file name, so we can tell at a glance that a file is a shell script, but that's optional. The other way, which is generally more common is to make an executable script which can be run just like any other program. The first step here is to add a line at the top of the script called the shebang line which tells the shell what program to use when the script is run. A shebang line starts with a pound or hash sign and an exclamation mark, and then the full path to whatever program should run the script. In this case, we'll use Bash but this can be anything like Python or Ruby or whatever interpreted scripting language you need to make into a program. It's common to see the absolute path to Bash in a shebang line in older script like, slash bin slash Bash but it's often considered a best practice to write the shebang line in a way that uses the shell's environment to locate the Bash executable. Not all systems have Bash in the same place though almost all of them do. And so instead of asking for bin, Bash and not finding it in that file path, we ask the environment to give us Bash which will work wherever Bash is installed. If we leave off a shebang, the contents of the script file will be passed to the current shell on my Linux system that would be Bash anyway but we don't know what shell other people are using at any given time, so it makes sense to include the shebang. So we guarantee that our Bash script is run by Bash. I'll a blank text file. I'll write code and the name of my script. If you're not using Code Spaces or if you prefer a different editor you might write nano in the name of your script or vi in the name of your script or something similar. I'll set the language for this script to shells script and then I'll add this shebang to my script. Following that, we write out our commands or program following that line, one command per line. We can also use blank lines to help keep things readable and we can use the pound sign to write comments in the script that act as notes to ourselves or to future readers and editors of the script. I'll save this file with Control S and if you're using a different editor, you'll need to save the file. When creating an executable script will usually leave the file extension off, but that's entirely up to you. The second step of making an executable script is to make it executable. You'll do that with chmod plus X and the name of the file. I'll click back into my terminal And I'll run that command writing chmod, plus x, my script. Now I can run this by typing dot, slash, my script and if I were to place this file somewhere the shell could find it somewhere in the path. I can just use the name of the script like a regular command. Back here in my terminal, I'll write dot, slash, myscript. I'll press enter, and there's the output of my script. In this way, wherever there's a system with Bash installed my script can run. A Bash script runs inside of a non interactive shell, which means that many of the customizations a user might have set for their Bash environment won't be read when the script is run. Unlike when we use a regular interactive shell. This gives the script a fairly clean environment in which to run, and if we want to make any customizations or specifically override other settings we'll need to use set or shopt in our script to turn on or off configurations accordingly. In most cases, we don't need to do this but it's something that's available if necessary. Writing Bash scripts allows us to avoid typos, to streamline work using Bash and they allow us to distribute our Bash code to other systems that need to run it and one-liners are useful as well.
Contents
-
-
-
-
Understanding Bash script syntax5m 18s
-
(Locked)
Choosing a text editor for Bash scripting1m 33s
-
(Locked)
Displaying text with "echo"4m 26s
-
(Locked)
Working with variables5m 21s
-
(Locked)
Working with numbers8m 44s
-
(Locked)
Comparing values with test4m 59s
-
(Locked)
Comparing values with extended test3m 34s
-
(Locked)
Formatting and styling text output8m 13s
-
(Locked)
Formatting output with printf5m 6s
-
(Locked)
Working with arrays4m 25s
-
(Locked)
Challenge: Make a script that generates a system report1m 1s
-
(Locked)
Solution: Make a script that generates a system report1m 26s
-
-
-
-
-