Create BASH Scripts
So you want to create BASH scripts? In this first lesson we will look at the BASH script fundamentals. How to we make a start on the journey and head off in the right direction. We will be looking at:
- Where do I create my scripts?
- How do I name them?
- How can I execute and debug them as required?
When you are developing and testing scripts if you place them within your own bin directory they are then likely to be in your PATH statement. Linux will only look for scripts within the PATH statement and not in the current directory. Adding the scripts to your own bin directory, ~/bin, means that you can execute the script from any directory without having to use the full path to the script.
Adding a .sh extension helps in the uniqueness of the script name and identifies the file type easily to yourself. Linux itself does not need the .sh extension but makes sense for us to add it.
Scripts will not be executable once they are created. We can use the “change mode” or chmod program to add in the execute permissions:
- chmod +x scriptname #adds execute to all users
- chmod u+x scriptname #just adds the execute permission to the user owner
If you need to debug the script using bash -x scriptname will show you the debug information which is especially useful to see how conditional statements where evaluated within the script.
My Book on Shell Scripting is available from good book shops and the publisher Packt.