Open In App

.sh | File Format

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A .sh file is a scripting language command file containing a computer program that can be run by the Unix shell. It can contain a series of commands that run sequentially to perform operations such as file processing, program execution, and other similar tasks. These are run from the command line interface by the user or in batch mode to perform multiple operations simultaneously. Script files can be opened in text editors such as Notepad, Notepad++, Vim, Apple Terminal, and other similar applications on Windows, MacOS, and Linux.

Brief History

The .sh file extension is often associated with shell scripts, particularly those written for Unix-like operating systems. It originated with the development of Unix in the early 1970s. Stephen Bourne developed the original Unix shell, known as the Bourne Shell (sh). It became the default shell on Unix systems and included a scripting language for automating tasks. Users can begin creating shell scripts by writing a series of shell commands in a text file. It is still a fundamental component of Unix-like operating systems and is used extensively for a variety of scripting tasks, including automation and system administration. On many systems, Bash has emerged as the factory standard for shell scripting.

Here are some key points about shell scripts and .sh files:

  • In Linux, the shell is a text-based interface that allows users to enter command names, execute programmes, and manipulate files like file handles, file permissions, and directories.
  • Users can access the shell via the terminal window or console, which is a window with a command prompt where they can enter commands.
  • The Linux shell is based on the scripting language Bash (Bourne-Again Shell), which is the default shell on most Linux systems.
  • Some basic commands in the Linux shell include ls, cd, mkdir, touch, cp, mv, and rm.
  • With these basic commands, the Linux shell offers a plethora of advanced commands and features that enable users to complete more complex tasks.

SH File Format

  • Comments – Comments start with a # and are ignored by the shell.
  • Shortcuts – You can rename a command to make it easier to execute quickly.
  • Batch Jobs – You can automate the execution of multiple commands that would otherwise require human input. This eliminates the requirement to wait for a user to initiate each step of the process.
  • Generalization – Using simple loops, much more generalization is achieved for operations such as conversion of images from one fromat to another.

Here’s a simple example 1 of a Bash script:

/* #!/bin/bash

echo “Hello, World!”

# Other commands can go here */

How to run the .sh file

To run this script, you would save it to a file, make it executable (chmod +x script.sh), and then execute it (./script.sh).

Output of the following code

Hello, World!

Example 2

/* #!/bin/bash

# This is a simple Bash script

# Prompt the user for their name

echo “Hello! What’s your name?”

read name

# Print a personalised greeting

echo “Nice to meet you, $name!” */

Save this code in a file with a .sh extension, for example, greeting.sh. To make it executable, run the following command in the terminal:

/* chmod +x greeting.sh */

Now you can execute the script:

/* ./greeting.sh */

Output

Hello! What’s your name?

John

Nice to meet you, John!

Conclusion

At last, we can say that shell scripting languages are widely used by developers when they are working directly with the operating system using the command line tools. That will help you to automate the many tasks that otherwise require human input which will increase the resource cost.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads