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.

Take your Linux skills to the next level!

The Linux Command Line

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

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources