Basic Shell Script Example
Basic Shell Script Example
Here’s a simple Bash shell script example that prints a Hello World message onto the screen. A shell script is a text file containing a series of commands that are executed by a Unix-like shell. These commands are written in a scripting language that the shell can interpret, such as Bash (Bourne Again Shell).
Shell scripts often have a .sh file extension, but this is not mandatory. The important thing is that the script has executable permissions.
Code
#!/bin/bash
# This script print Hello World
# Linux Tutorials - www.TestingDocs.com
echo "Hello, World!"
Output
When executed, this shell script will print “Hello, World!” to the terminal.
The first line of a shell script usually starts with #! followed by the path to the interpreter (e.g., #!/bin/bash). This is known as a “shebang” and tells the system which interpreter to use to execute the script.
For a shell script to be executable, you need to set the appropriate permissions using the chmod command. For example, to give executable permission to script.sh file, use the below command:
$ chmod +x script.sh
Shell scripts can be executed directly from the command line or scheduled to run at specific times using tools like cron.