A .sh file is a shell script that groups multiple Linux commands into one executable file. Instead of typing each command separately, you run a single file and the shell processes everything in sequence. This guide covers how to create, permission, and run .sh file in Linux using both terminal and GUI methods.

What Is a .sh File in Linux?

Files ending in .sh are shell scripts by convention. The extension itself isn’t mandatory for execution, but it tells you (and your text editor) that the file contains shell commands. Inside, you’ll find a series of instructions the terminal interpreter reads line by line.

System administrators use shell scripts to batch repetitive tasks: updating packages, rotating logs, backing up directories, restarting services. A script you write once can run hundreds of times with identical results. You can also schedule them through cron jobs or share them with teammates who need the same workflow on their machines.

Every script should begin with a shebang line like #!/bin/bash. This tells the system which interpreter to use. Without it, the system falls back to whatever shell is currently active, and that can vary between environments.

How to Create a .sh Script File

Using the Terminal

Open your terminal. If you’re on a remote server, connect through SSH first. Create an empty file with touch:

sudo touch myscript.sh

Open it in a text editor. Nano works well for quick edits, though you can also use vi or vim if you prefer modal editing:

sudo nano myscript.sh

Add the shebang line at the top, then your commands below it:

#!/bin/bash
sudo apt update
sudo apt upgrade

Save the file in Nano with Ctrl + X, then Y, then Enter. Lines starting with # (other than the shebang) are comments. The interpreter skips them, but they help anyone reading the script understand what each section does.

Using a GUI Text Editor

On desktop Linux distributions, open your default text editor from the application menu. Type the shebang line and your commands just like you would in the terminal. Save the file with a .sh extension. Pick a location you can easily reach from the terminal later.

Setting File Permissions to Run .sh File

Linux does not grant execute permission to new files by default. Try running a freshly created script and you’ll get a “Permission denied” error. The chmod command fixes this:

sudo chmod +x myscript.sh

This grants execute permission to the file owner. Other user accounts will need sudo to run it. If you want to restrict access further or understand octal notation (like 755 or 700), the man page for chmod has the full breakdown.

Permission ScopeCommandWho Can Execute
Owner onlychmod +x script.shFile creator
All userschmod a+x script.shEveryone on the system

Granting universal execution rights is a bad idea on shared systems. Keep permissions limited to the owner unless you have a specific reason to open them up.

GUI users can right-click the file, open Properties, go to the Permissions tab, and check the option labeled “Allow executing file as program.”

How to Run .sh File in Linux

Execute with ./ Prefix

The most common method. Navigate to the directory containing the script and type:

./myscript.sh

The ./ tells the shell to look in the current directory rather than searching the system PATH. For scripts stored elsewhere, use the full path:

/home/user/scripts/myscript.sh

This method runs the script using whatever interpreter the shebang line specifies. On desktop environments, right-clicking and selecting “Run as a Program” does the same thing.

Run .sh File with Bash or Sh

You can pass the script directly to an interpreter without setting execute permission at all. The shell reads the file content instead of executing it as a standalone program:

bash myscript.sh
sh myscript.sh

This is handy for quick one-off runs. The interpreter you call here overrides whatever shebang line exists in the file. Command-level permissions still apply inside the script, so operations requiring root access will still fail without sudo.

Run .sh File as a Different User

Running scripts as root when you don’t need to is risky. A missing variable in a cleanup script could wipe critical directories. The runuser command lets administrators execute scripts under a specific account:

runuser -u cronuser -- /scripts/monitor.sh

Add -g to specify a group if needed. This approach keeps your shell script execution scoped to the right privilege level. It is a standard practice on production servers where multiple services run under dedicated accounts.

Run .sh File in the Background

Long-running scripts like monitoring tools or log watchers can block your terminal session. Append & to push execution into the background:

./myscript.sh &

The terminal prints a process ID you can later use with kill to stop it. The catch: closing the terminal also kills the process. Wrap it with nohup to prevent that:

nohup ./myscript.sh &

For more complex session management, terminal multiplexers like tmux or GNU Screen create persistent virtual shells. These survive SSH disconnections entirely, which is why they’re standard on remote servers. If you’re managing multiple scripts across sessions, you’ll also want to understand how directory management works to keep your workspace organized.

MethodCommandSurvives Terminal Close
Background./script.sh &No
nohupnohup ./script.sh &Yes
Multiplexerscreen or tmuxYes (survives SSH disconnect)

FAQs

What does the shebang line do in a .sh file?

The shebang (#!/bin/bash) tells Linux which interpreter should process the script. Without it, the system uses the current active shell, which may behave differently across environments.

Can I run a .sh file without chmod?

Yes. Pass the file to an interpreter directly with bash filename.sh or sh filename.sh. This reads the file without requiring execute permission on it.

Why does my .sh file say permission denied?

The file lacks execute permission. Run chmod +x filename.sh to add it. Some commands inside the script may also fail if the current user lacks sudo access.

How do I run a .sh file in the background on Linux?

Append an ampersand: ./script.sh &. To keep it running after closing the terminal, use nohup ./script.sh &. For persistent sessions, use tmux or screen.

Can I edit a .sh file with any text editor?

Yes. Shell scripts are plain text files. Nano, Vim, Gedit, and VS Code all work. The file needs write permission enabled for editing to succeed.

Willie has over 15 years of experience in Linux system administration and DevOps. After managing infrastructure for startups and enterprises alike, he founded Command Linux to share the practical knowledge he wished he had when starting out. He oversees content strategy and contributes guides on server management, automation, and security.