To create a shell script:
- Use a text editor such as vi. Put required Linux commands and logic in the file
- Save and close the file (exit form vi).
- Make the script executable.
- You should then of course test the script, and once satisfied with the output, move it to the production environment.
- The simplest program in Bash consists of a line that tells the computer a command. Start up your favorite text editor (such as vi):
- Open a file:
- vi filename
- To go into edit mode:
- press ESC and type I
- To go into command mode:
- press ESC
- To save a file
- press ESC and type :w filename
- To save a file and quit:
- press ESC and type :wq
Simple programming:
#!/bin/bash
echo “Hello, World!”
echo “Knowledge is power.”
- Save and close the file. You can run the script as follows:
- ./hello.sh
- Sample outputs:
- bash: ./hello.sh: Permission denied
Saving and Running Your Script:
The command ./hello.sh displayed an error message on the screen. It will not run script since you’ve not set execute permission for your script hello.sh. To execute this program, type the following command:
chmod +x hello.sh
./hello.sh
Sample Outputs:
Hello, World!
Knowledge is power.
Starting a Script With #!
- It is called a shebang or a “bang” line.
- It is nothing but the absolute path to the Bash interpreter.
- It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.
- All scripts under Linux execute using the interpreter specified on a first line
- Almost all bash scripts often begin with #!/bin/bash (assuming that Bash has been installed in /bin)
- This ensures that Bash will be used to interpret the script, even if it is executed under another shell .
- The shebang was introduced by Dennis Ritchie between Version 7 Unix and 8 at Bell Laboratories. It was then also added to the BSD line at Berkeley.
Shell Comments
Take look at the following shell script:
#!/bin/bash
# A Simple Shell Script To Get Linux Network Information
# Vivek Gite – 30/Aug/2009
echo “Current date : $(date) @ $(hostname)”
echo “Network configuration”
/sbin/ifconfig
The first line is called a shebang or a “bang” line. The following are the next two lines of the program:
# A Simple Shell Script To Get Linux Network Information
# Vivek Gite – 30/Aug/2009
- A word or line beginning with # causes that word and all remaining characters on that line to be ignored.
- These lines aren’t statements for the bash to execute. In fact, the bash totally ignores them.
- These notes are called comments.
- It is nothing but explanatory text about script.
- It makes source code easier to understand.
- These notes are for humans and other sys admins.
- It helps other sys admins to understand your code, logic and it helps them to modify the script you wrote.
Conclusion
Linux shell scripting is a fundamental skill that empowers you to automate tasks, manage systems efficiently, and write your own tools tailored to your workflow. By understanding how to create, edit, and execute shell scripts—along with concepts like the shebang line and proper commenting—you lay a strong foundation for system-level programming and automation.
Whether you’re a beginner learning to print “Hello, World!” or an aspiring system admin writing advanced scripts, mastering shell scripting opens up a powerful layer of control over Linux environments. Keep practicing, experiment with real-time scripts, and remember—great scripts don’t just work, they’re also readable and well-documented.
If you’re eager to deepen your understanding of advanced C++ and gain practical programming experience, the Indian Institute of Embedded Systems (IIES) provides high-quality education and hands-on training in embedded systems. Backed by experienced faculty and an industry-relevant curriculum, IIES is a great choice for students looking to build a successful career in embedded technology.