Skip to content

06 Shell Scripts

Ryan edited this page Aug 11, 2023 · 3 revisions

Shell Scripts

Shell scripts are a great way to document every step you've performed in an analysis. If you use them to run every step in your pipeline, then there is a much greater chance you can run every step again and get the exact same output.

So what is a shell script?

It's just a text file that holds the code you would have typed into the shell prompt. Every command we've learned up to this point can be saved into a shell script.

It is good practice to start the very first line of a text file with a shebang line that indicates which shell you'd like to run the shell script. It probably will look something like this:

#!/usr/bin/bash

We're using bash in nearly every case, so we'll specify that. Often, but not always, bash is stored in /usr/bin/bash, but this isn't always the case. To find where it is located, you might want to use the "which" command to see what directory your system defaults to when calling bash:

which bash

Another common practice is to save the file with the suffix ".sh", so it's obvious what type of file it is.

A comment on comments

In bash, the "#" symbol (pound? number? ... hashtag?) can be used to make comments. Comments are lines of code you want to keep, typically for documentation purposes.

How can I input information into a shell script?

Just like with many of the commands we've been using, shell scripts can take positional arguments to store and use values from stdin. For example, if we create a shell script and want to input a filename to be used inside the script, we can use this general format when calling our script:

bash cool_shell_script.sh file.txt

This tells bash that cool_shell_script.sh takes a first positional argument called "file.txt", but this also needs to be indicated inside of the shell script itself using the notation for positional arguments. Fortunately, this is very easy, and the first positional argument (in this case 'file.txt') is represented in the script using $1. Further positional arguments just use the next highest number (e.g.; $2, $3, and so on).

#!/usr/bin/bash

# count the lines in a single file

wc -l $1

If you're not sure how many input files you want to use as positional arguments, you can just use the "@" symbol:

#!/usr/bin/bash

# count the lines in any number of files

wc -l $@