Skip to main content
Shell Scripting – Complete Beginner to Advanced Guide
CHAPTER 02 Intermediate

Setting Up the Shell Environment

Updated: May 16, 2026
20 min read

# CHAPTER 2

Setting Up the Shell Environment

1. Introduction

If you double-click a .docx file on a desktop computer, Microsoft Word opens automatically. The operating system uses the file extension to guess what application should read the file. Linux and Unix are fundamentally different; they explicitly ignore file extensions. A file named script.sh is entirely meaningless to the kernel. To force the operating system to recognize a plain text document as a fully functional, executable software program, a systems administrator must configure the script's internal environment and modify its external security permissions. In this chapter, we will master the critical "Shebang" (#!), understand relative versus absolute execution paths, and conquer the chmod command.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand why Unix/Linux systems ignore file extensions.
  • Implement the Shebang (#!/bin/sh) to dictate the shell interpreter.
  • Modify file permissions to authorize execution using chmod +x.
  • Execute a script locally using a relative path (./).
  • Execute a script globally using an absolute path.

3. The Shebang (#!/bin/sh)

Because Unix ignores file extensions, you must place an explicit set of instructions inside the file to tell the operating system how to read it. This is done on the absolute first line of the file, utilizing the Shebang (Hash = #, Bang = !).
sh
12
#!/bin/sh
echo "This script is being run by the Bourne Shell."

When the operating system is asked to run this file, it reads the very first line. It sees #!/bin/sh. The kernel immediately stops reading the file and hands the entire document over to the interpreter program located at /bin/sh. *(If you were writing a Python script, your shebang would be #!/usr/bin/python3).*

4. File Permissions (chmod +x)

By default, when you create a new text file in Unix, it is created with Read and Write permissions, but the Execute permission is completely disabled. This is a brilliant security feature designed to prevent malicious text files from running as viruses. If you type ./script.sh on a new file, the terminal will instantly reply: Permission denied.

To unlock the file, you must use the Change Mode (chmod) command.

sh
12
# Add the Execute (+x) permission to the file
chmod +x script.sh

The file is now legally recognized by the kernel as a software application.

5. Script Execution (Paths)

Once unlocked, you must execute the script. However, you cannot just type script.sh and press Enter. If you do, the terminal will reply: Command not found. For security reasons, the terminal only looks for commands in explicit system folders (like /usr/bin). It refuses to look in your current folder unless you force it to.

1. The Relative Path (Dot-Slash): If you are standing in the exact same folder as the script, you use ./. The dot means "My Current Directory."

sh
1
./script.sh

2. The Absolute Path: If you are logged into the server and currently standing in the /tmp folder, but your script is in your home directory, you must give the terminal the full GPS coordinates to the file.

sh
1
/home/user/scripts/script.sh

3. The Override: If you forgot to add the Shebang, or forgot to use chmod, you can override the entire security system by calling the interpreter directly and handing it the file as an argument.

sh
1
sh script.sh

6. Diagrams/Visual Suggestions

*Visual Concept: The Execution Pathway* Draw a flowchart starting with a user executing ./script.sh. Box 1 (Decision): Does file have 'x' permission? (No -> Red Error "Permission Denied"). Box 2: Kernel opens file, reads line 1. Box 3: Kernel identifies Shebang #!/bin/sh. Box 4: Kernel routes the file to the /bin/sh program. Box 5: The Shell reads lines 2, 3, and 4 and performs the automation. This visualizes the rigid security checks that occur in milliseconds before a script actually begins working.

7. Best Practices

  • Standardize File Extensions: Even though Unix ignores extensions, humans do not. Always name your shell scripts with a .sh suffix (e.g., backup.sh). This allows other administrators to visually identify the file's purpose when running an ls command in a crowded directory.

8. Common Mistakes

  • Putting spaces in the Shebang: A beginner might type #! /bin/sh or # !/bin/sh. While some modern kernels are forgiving, strict legacy Unix systems will crash and fail to interpret the line if there are spaces. Always format it tightly: #!/bin/sh.

9. Mini Project: Execution Environment Lab

Let's deliberately create the two most common script execution errors.
  1. 1. nano test.sh
  1. 2. Write the following code (omitting the shebang!):
sh
1
echo "I am a broken script."
  1. 3. Save and exit.
  1. 4. Try to run it properly: ./test.sh. (It fails: Permission Denied).
  1. 5. Fix the permission: chmod +x test.sh.
  1. 6. Try to run it: test.sh without the dot-slash. (It fails: Command not found).
  1. 7. Run it correctly: ./test.sh. It works, but it's sloppy because there is no shebang.
  1. 8. Open the file again, add #!/bin/sh to the top line, and run it. You have now established a perfect execution environment.

10. Practice Exercises

  1. 1. Describe the exact mechanism Unix uses to determine which interpreter should execute a script. Why does it ignore the .sh file extension?
  1. 2. If you are currently working inside the /var/log directory, write the exact command required to execute a script located at /opt/tools/clean.sh.

11. MCQs with Answers

Question 1

What is the fundamental security purpose of requiring a system administrator to execute the chmod +x command on a newly created script file?

Question 2

When executing a script located in your current working directory, which syntax is strictly required to instruct the shell to look locally rather than searching standard system directories?

12. Interview Questions

  • Q: A developer writes a shell script on a macOS machine and utilizes the shebang #!/usr/local/bin/sh. They deploy the script to a standard Ubuntu server, and the execution instantly fails with "bad interpreter: No such file or directory." Explain the architectural reason for this failure and how to formulate a universally compatible shebang.
  • Q: Explain the mechanical difference between running a script via ./deploy.sh versus sh deploy.sh. Which method bypasses the need for the execute (x) permission bit on the file?
  • Q: Discuss the security implications of Linux refusing to execute programs in the current working directory unless the user explicitly prepends ./ to the command string.

13. FAQs

Q: Can I use #!/bin/bash instead of #!/bin/sh? A: Yes. If you explicitly want to use advanced features (like arrays) that are unique to the Bourne Again Shell, use bash. If you want your script to be incredibly lightweight and guaranteed to run on any Unix machine built in the last 40 years, use sh.

14. Summary

In Chapter 2, we established the strict environmental protocols required to transform static text into dynamic software. We dismantled the desktop paradigm of file extensions, learning that the Unix kernel relies entirely on the internal #!/bin/sh Shebang to route code to the correct interpreter. We addressed the operating system's inherent security barriers by explicitly unlocking execution capabilities via chmod +x. Finally, we mastered navigational syntax, ensuring we can trigger our scripts from anywhere in the filesystem by utilizing both relative (./) and absolute pathway execution.

15. Next Chapter Recommendation

Your script can successfully execute, but it is entirely static. To perform dynamic automation, we must teach the script to store data and listen to the human operator. Proceed to Chapter 3: Variables and User Input.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·