01. Introduction to Shell Scription

Welcome to our shell scripting tutorial series! It's required that you have a basic knowledge of the Linux Command Line, so be sure to review it before you proceed.

What is a shell script?

A shell script is a file containing lines of code for the shell to execute. The shell reads each line as if you typed them in one at a time into the terminal.

Scripting languages are interpreted, meaning that they aren't compiled into machine language. This results in less efficiency, but in exchange, we are able to more easily write our scripts.

The scripts may be written in any text editor, so simply select one with syntax highlighting - you may use Vim within your terminal, or something like Sublime Text.

Learning a text editor

We recommend that you learn a text editor while learning shell scripting. Additionally, if you want to try out Vim, head on over to our in-browser Vim tutorial series, or type vimtutor into the command line. Taking the time to learn Vim keys will speed up your workflow immensely.

When not to use shell scripts

Remember that shell scripts should be short and used to manipuate files and commands. If you find that you're writing something that looks overly complex, and involves a slew of string or arithmetic operations, look into a better-suited scripting language such as Python or Perl.

Quick syntax guide

For this module, we'll be using both the command line and shell scripts to give examples of what we're learning.

Anything that comes after a $ is considered a command to be typed out on the command line. Anything following a > symbol is a continuation of the line before.

Comment and explanation lines preceed with a #.

$ cd ~
# Change to home directory
$ echo "Anything proceeding a > \
> symbol means it's a continuation \
> of the line before."
Anything proceeding a > symbol means it's a continuation of the line before.

Lastly, anything without a preceding special character will be a snippet from a shell script file.

echo 'This is a snippet from a shell script'

Great start! Now let's move on to some important symbols covered in shell scripting.

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources