Forgetting to add the #!/bin/bash, the Hash-Bang, is one of the most common mistakes to make for the beginner scripter. The Hash-Bang is the interpreter that tells the system what shell to use and that what you’re running is in fact a shell script.
Remember, Linux scripts don’t run when you type the name of the file in. If you simply enter MyScript into the Terminal, it will attempt to execute a built-in command called MyScript – which doesn’t exist. Instead, you need to place ./ at the start: ./MyScript.
The second most common issue for beginners, when their script doesn’t want to work, is that they’ve forgotten to give it executable permissions. After you’ve created your script you need to chmod +x it to allow the system to flag the file as executable. Once it’s an executable you can then run the script.
Most of us automatically continue writing script code without entering whitespaces after a variable but if you’ve been using other programming languages then it can become habit to hit the space bar frequently. In scripting, there’s no spaces on either side of the equals sign for variables. So, word = Hello is wrong, whereas word=Hello is correct.
Setting a script that copies files from one place to another, such as a backup script, can be tricky. The part that stumps most folks is the naming of some files. If you set variables as the file and target, you need to encapsulate them in quotes. This way any whitespaces and extensions are considered. Such as, cp — “$file” “$target”.
Always be cautious when copying scripts you’ve found online into your system and executing them. There are some Linux commands that kill your system beyond repair, forcing you to reinstall the OS. The rm -rf command, for example, wipes all the files and folders off your system. Always research script contents before executing.
It’s all too easy to mistype a command in the Terminal. When you do it in a script though the end result can be failure or something totally different. Before you save and execute the script, have a quick look through to check you’ve not mistyped a command.
Following on from the previous common mistake, never blindly enter a website into a script or the Terminal that downloads and executes a script. There’s a good chance it could contain something malicious or wipe your files. The command wget http://somewebsite -O | sh downloads a script and automatically executes it.
While frustrating to the coder, it’s always amusing to see someone who’s written a script and inserted a Microsoft command instead of a Linux command. Clearing the screen is popular, where in Linux you use clear, someone who’s got their DOS head on uses cls.
A popular mistake with most Bash scripters is missing out vital operators in their code. Missing quotations marks in an echo command, or square brackets when using loop, and even flags for external commands can have undesired results when you execute the script. Best to check through your code before running it.
The guide above is a sample of the content in this high-quality guidebook. Our guidebooks are all written by experts, and designed to help anyone learn more about the tech around them. If you would like to see more guides like this, and continue to expand your knowledge of the subject, click the image to see more information and viewing options.