User Agents

Web browsers and everyday web tech, explained

Create .sh Extension Files in Ubuntu Linux: A Full Guide

· 6 min read · Extensions

In this article
  1. Understanding the .sh File Extension
  2. Step-by-Step Creation Process
  3. How Shell Scripts Work Internally
  4. Common Problems and Troubleshooting
  5. Comparing Bash vs. Sh vs. Zsh
  6. Safety and Security Best Practices
  7. Frequently Asked Questions

To create a .sh extension file in Ubuntu, open a text editor, write your shell commands, and save the file with the .sh suffix. To make the file functional, you must then grant it execution permissions using the command chmod +x filename.sh in the terminal.

Understanding the .sh File Extension

In the Linux ecosystem, a file ending in .sh is a shell script. A shell script is essentially a plain text file containing a series of commands that the shell (the command-line interpreter) can execute. While the .sh extension is widely used for clarity and organization, Linux does not actually rely on file extensions to determine how to run a file, unlike Windows which uses extensions to associate files with specific applications.

The core of a shell script is the "shebang" line, which appears at the very top of the file. The shebang (#!) tells the operating system which interpreter to use to parse the script. For most Ubuntu users, this is Bash (Bourne Again SHell). A typical shebang looks like #!/bin/bash. Without this line, the system may attempt to run the script using the default shell of the current user, which can lead to compatibility issues if the script uses features specific to Bash but is executed by a simpler shell like Dash.

Step-by-Step Creation Process

Creating a script can be done through a Graphical User Interface (GUI) or directly within the Terminal. Both methods result in the same file, but the Terminal method is generally preferred by developers for speed and precision.

  1. Open a Text Editor: You can use a GUI editor like Gedit (Text Editor) or a terminal-based editor like Nano or Vim. To use Nano, type nano myscript.sh in the terminal.
  2. Add the Shebang: Start the file with #!/bin/bash on the first line. This ensures the script runs in the Bash environment regardless of the user's current shell.
  3. Write the Commands: Add the logic you want to automate. For example, a simple script to update the system might include sudo apt update and sudo apt upgrade -y.
  4. Save and Exit: In Nano, press Ctrl+O to write the file and Ctrl+X to exit.
  5. Modify Permissions: By default, new files are not executable for security reasons. Use the command chmod +x myscript.sh to change the file mode to executable.
  6. Execute the Script: Run the script by typing ./myscript.sh in the directory where the file is located.

How Shell Scripts Work Internally

When you execute a .sh file, the Linux kernel reads the first few bytes of the file. If it encounters the #! sequence, it knows it is dealing with a script and not a compiled binary. The kernel then loads the interpreter specified in the path (e.g., /bin/bash) and passes the script file to that interpreter as an argument.

The shell then reads the script line by line. It performs variable expansion, handles wildcards, and executes the commands in the order they appear. If the script encounters an error, the behavior depends on the shell settings; by default, it will usually print the error to the screen and continue to the next line. This sequential execution makes shell scripts ideal for repetitive tasks, such as backing up directories, managing logs, or installing a suite of software packages upon a fresh OS installation.

Common Problems and Troubleshooting

Even experienced users encounter issues when creating .sh files. Understanding these pitfalls can save hours of debugging.

  • Permission Denied: This is the most common error. It occurs when the chmod +x command was skipped. If you see "Permission denied," ensure the file has the execute bit set.
  • Line Ending Mismatches (CRLF vs LF): If you create a script in a Windows text editor and move it to Ubuntu, the script may fail with "command not found" or strange character errors. Windows uses Carriage Return and Line Feed (CRLF), while Linux uses only Line Feed (LF). You can fix this by installing the dos2unix utility and running dos2unix filename.sh.
  • Path Issues: If you try to run a script by typing just myscript.sh, the shell will look in the directories listed in your $PATH environment variable. Since the current directory is usually not in the path for security reasons, you must specify the path explicitly using ./myscript.sh.
  • Syntax Errors: A missing quote or a misplaced bracket in a loop can crash a script. Using a tool like shellcheck can help identify these errors before you run the script.

Comparing Bash vs. Sh vs. Zsh

While the extension is .sh, the actual language used can vary. It is important to choose the right interpreter for your needs.

  • sh (Bourne Shell): The original Unix shell. It is very fast and lightweight but lacks many modern features like arrays or advanced string manipulation. It is often used for system boot scripts where speed and universality are critical.
  • bash (Bourne Again SHell): The default for Ubuntu. It is a superset of sh and adds a vast array of features, including command history, tab completion, and better scripting logic. Most .sh files on Ubuntu are actually Bash scripts.
  • zsh (Z Shell): Popular among power users and the default on macOS. It offers even more advanced features than Bash, such as better plugin support and a more intuitive theme system. While you can write .sh files for Zsh, they may not run on systems that only have Bash installed.

Safety and Security Best Practices

Running scripts, especially those downloaded from the internet, carries significant risks. A script with root privileges can delete the entire file system or install malicious software.

Before executing any .sh file from an untrusted source, always open it in a text editor to inspect the code. Look for commands like rm -rf / or obfuscated strings that look like Base64 encoded data, which might be hiding a payload. Avoid running scripts with sudo unless you are certain the script requires administrative privileges. If a script asks for your password immediately upon execution without explaining why, exercise extreme caution.

To further enhance safety, consider running experimental scripts in a virtual machine or a container (like Docker). This isolates the script from your primary operating system, ensuring that any catastrophic errors or malicious actions do not affect your personal data or system stability.

Frequently Asked Questions

Do I have to use the .sh extension?

No, the extension is not required by Linux to execute the file. As long as the shebang is present and the file has execute permissions, you can name it anything (or give it no extension at all). However, using .sh is a best practice for human readability.

Why does my script work when I run 'bash script.sh' but not './script.sh'?

When you run bash script.sh, you are manually invoking the Bash interpreter to read the file, so execute permissions are not needed. When you run ./script.sh, you are asking the kernel to execute the file as a standalone program, which requires the execute bit to be set via chmod.

Can I run a .sh file from the Ubuntu file manager (GUI)?

Yes, but you may need to change the behavior of your file manager. In the Files (Nautilus) settings, you can choose whether to run executable text files, display them, or ask what to do. By default, many versions of Ubuntu will open the script in a text editor for safety.

What is the difference between chmod +x and chmod 755?

chmod +x simply adds the execute permission for the user, group, and others. chmod 755 is a more specific numeric assignment: the owner gets full permissions (read, write, execute), while the group and others get read and execute permissions only. Both will make a script runnable.

How do I pass arguments to my .sh file?

You can pass arguments by adding them after the script name: ./myscript.sh arg1 arg2. Inside the script, these are accessed using special variables: $1 for the first argument, $2 for the second, and $@ to refer to all arguments passed to the script.