Skip to main content

Writing Your Own Scripts

Writing Your Own Scripts

Often times you want do more than just one command at a time; you want to run many of them. Bash allows you to put many commands into one file to create a program of programs which is called a shell script.

How that work?

You have been learning a programming language this entire time. Bash is actually a programming language and you have been learning it this whole time. We have been just been running it one line at a time via a REPL. You can actually do this with Python or Node.js too.
Try just running python3 or node. It will drop you in a similar style REPL (FYI, to quit either node or python3, use CTRL+D.)

My First Bashscript

Let's make our first bash script. Let's make make a directory called temp, generate ten files, and exit. Basically we want to make a bashscript that does this:

mkdir -p ~/temp
# -p mean don't error if it exists in this case, it does other things too
cd ~/temp
touch file{1..10}.txt
echo done

So let's do that. You can use either vim or nano, both work. So run

vi gen_files.sh
# or
nano gen_files.sh

From there, put the above code in it and save.

Running a Scripts

To run a script, you need to make it executable. You can do this by running chmod +x gen_files.sh. This will make it executable.
You can then run it by running blow one of command;

./gen_files.sh
bash gen_files.sh
source gen_files.sh

Hashbang

  • The first line of a script is called a hashbang. It tells the system what program to use to run the script.
  • For bash, it is #!/bin/bash.
  • You can also use #!/usr/bin/env bash which will use the bash in your path. This is useful if you are using a different shell like zsh or fish.

Open gen_files.sh and put this as the very first line. I bold that because it must be the first line.

#! /bin/bash
Note

This line (called a shebang, hashbang, or many other things; often ! is called a bang in computing) lets bash know how to execute this file.

It must be on the first line and it must start with #!. It's then always followed by the absolute path (meaning it starts with / and gives you the full path; you cannot give it a relative e.g. ./bash).

This works with Python, for example. If you want a script to executed by python3, you can find the path of any program by saying which <command>. So if we want to know where python3 is, we can say which python3 and get the path from that.

Note

However the file now needs the executable permission

chmod 700 gen_files.sh

Path

What if we want to be able to run gen_files.sh from anywhere on our computer?

Your user a variable set called PATH. Your PATH is a series of locations of where programs live. You can see your PATH if you run

echo $PATH

In general I don't mess any of those directories. All of those are system wide bin directories (if you had more than one user they'd share them.) So a good idea is to have your own ~/bin directory. So let's do that.

cd ~
mkdir bin
mv gen_files.sh bin/gen_files
PATH=~/bin:$PATH
echo $PATH
gen_files

Now it works! We added ~/bin to our path so now bash will try to execute anything we put in there.

add this line somewhere:vi ~/.bashrc

PATH=~/bin:$PATH

Comments

If you want to add comments, you just add and then from there you can put anything after it. You've probably notice me doing it before this#