How to Start Shell Scripting

You should think of POSIX (UNIX or Linux) system programs as building blocks which help you create bigger things. You can take a bunch of programs and put them together so that the data flows from one to another and the system takes care of the connections. The programs don’t know that they are connected – they are talking to the terminal.

webshell

Shell scripting gives everyday users the ability to program and to automate certain tasks. Shell scripts are made with common, everyday commands. If you want to get started with shell scripting, you should first familiarize yourself with a Linux or UNIX shell. The more you learn about the shell, the better your scripts will be.

There are a few shells out there, but if you are new to shell scripting, you should use bash. Bash is a good shell with plenty of features, and it is a default shell on Linux.

If you want to get good at shell scripting, you should first familiarize yourself with your text editor. The good news is that your Linux distribution comes with the grandfather of Unix text editors – vi. Vi is infamous for its non-intuitive command structure, but most of the Linux distributions come with an enhanced version of vi called vim.

When you get good at using vi or vim, you can start writing your first bash shell script. If you want to create your first script, follow the instructions:
– Open the terminal
– Use the ‘cd’ command to navigate to where you want to create the script
– Write ‘touch hello.sh’ (touch command creates a new empty file)
– vi hello.sh (opens the newly-created file in vi text editor)

Now that the file is open in vi, write the following:

#!/bin/bash
# My first script
echo “Hello World!”

– Save the file, and make the script executable by writing ‘chmod 744 hello.sh’.
– Run the script by writing ‘./hello.sh’

You should see the output “Hello World!” on your screen. If you have taken a class of programming, the result shouldn’t surprise you, but if you haven’t – here’s the explanation:

#!/bin/bash – this is the shebang (it tells the OS which program to use to run the script), you shouldn’t concern yourself too much with it now.
# My first script – anything following a hash (#) is a comment
echo “Hello World!” – the ‘echo’ command displays a line of text (in this case – Hello World!)

This is just a tiny sample of what shell scripting can do. We hope that we have sparked your interest and that you will continue to explore the world of shell scripting. Try learning on your own, don’t be afraid to make mistakes, and you will be on your way to becoming a hacker. Good luck!

About the Author

Leave a Reply 0 comments

Leave a Reply: