What is an SH File?
An SH file, also known as a shell script file, is a plain text file with the .sh
extension that contains a sequence of commands to be executed by the Unix shell. It is commonly used for automating tasks, managing system operations, and executing batch jobs. SH files can handle file processing, program execution, and other administrative tasks, making them essential for Linux and macOS environments. However, with the help of Linux terminal emulators like Putty or Cygwin, SH files can also be run on Windows.
These script files support comments (using #
), shortcuts for command execution, and loop-based generalization for tasks like file conversion. They can be executed directly from the command line or in batch mode to handle multiple operations simultaneously. Additionally, SH files can be edit
SH File Format
SH files are written in plain text following the defined syntax. These script files support:
Comments
- Comments start with a # and are ignored by the shell.Shortcuts
- These can be used to rename a command for short and easy execution.Batch Jobs
- Several commands can be be executed automatically that would otherwise need to be entered manually. This removes the need to wait for a user to trigger each stage of the sequence.Generalization
- Using simple loops, much more generalization is achieved for operations such as conversion of images from one fromat to another.
SH File Example
$ echo '#!/bin/sh' > my-script.sh
$ echo 'echo Hello World' >> my-script.sh
$ cat my-script.sh
#!/bin/sh
echo Hello World
$ chmod 755 my-script.sh
$ ./my-script.sh
Hello World
Simple SH File and How to Execute It
#!/bin/sh
echo "Hello World"
To make the script executable, run this command:
chmod +x myscript.sh
To execute the script:
./myscript.sh
How to Run SH File on Linux and Windows?
On Linux:
- Open the terminal
- Navigate to the directory where the
.sh
file is located - Set executable permission:
chmod +x filename.sh
- Run the script:
./filename.sh
On Windows:
- Install Windows Subsystem for Linux (WSL) or Putty
- Open the Linux terminal
- Navigate to the file location
- Run the script as shown above
How to run SH file?
The SH files usually run on Linux, even in Windows you need to connect with a Linux terminal using softwares such as Putty to run the sh files. Following are the steps to run an SH file on a Linux terminal.
- Open the Linux terminal and go to the directory where the SH file is located.
- By Using
chmod
command, set execute permission on your script (if not set already). - Run script using one of the following
./filename.sh
sh filename.sh
bash script-name-here.sh
FAQs
1. How do I make an SH file executable?
Use the command: chmod +x filename.sh
2. Can I run SH files on Windows?
Yes, with the help of Linux Subsystem (WSL) or Putty Terminal.
3. What is the difference between .sh and .bash files?.sh
files are executed in the Bourne shell, while .bash
files are for the Bash shell, which is more advanced.