Home BASH Fix “Exec format error” When Running Scripts With run-parts Command

Fix “Exec format error” When Running Scripts With run-parts Command

By sk
Published: Last Updated on 171.5K views

When I was trying to run all scripts in a directory using run-parts command, I encountered with an error - "run-parts: failed to exec script.sh: Exec format error". The scripts worked just fine when I directly execute them by running either "./script.sh" or "sh script.sh". But they didn't work when I ran them with run-parts command. For those wondering, the run-parts command will run all scripts in a directory. If you got an error like this while running a script, this quick tip will help you to fix "Exec format error" when running scripts with run-parts command in Linux.

Fix "Exec format error" When Running Scripts With run-parts Command

To run all scripts in the Documents folder, I ran:

$ run-parts --regex '^s.*\.sh

I got the following error message:

run-parts: failed to exec Documents/script1.sh: Exec format error
run-parts: Documents/script1.sh exited with return code 1
run-parts: failed to exec Documents/script2.sh: Exec format error
run-parts: Documents/script2.sh exited with return code 1
run-parts: failed to exec Documents/script3.sh: Exec format error
run-parts: Documents/script3.sh exited with return code 1
run-parts: failed to exec Documents/script4.sh: Exec format error
run-parts: Documents/script4.sh exited with return code 1
run-parts: failed to exec script.sh: Exec format error
run-parts: failed to exec script.sh: Exec format error

To fix "Exec format error" in Linux, you need to add a shebang at the start of your scripts, so the kernel will know how to run them.

For those wondering, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!) at the beginning of a script. When you add the shebang at the start of a text file, it is interpreted as an executable file.

Most scripts starts with a shebang. Here are some typical shebang examples.

Bourne shell, or a compatible shell:

#!/bin/sh

Bash:

#!/bin/bash

Perl:

#!/usr/bin/perl

Python 2.x:

#!/usr/bin/python

Python 3.x:

#!/usr/bin/python3

This is what we call a shebang.

Now, let us get back to the topic. Edit your scripts using your favorite editor:

$ nano Documents/ostechnix.sh

Add the following shebang at the beginning of the script:

#!/bin/sh
Fix "Exec format error" When Running Scripts With run-parts
Fix "Exec format error" When Running Scripts With run-parts

Press CTRL+ followed by CTRL+X to save the file and close it.

Now you can be able to run the scripts with the run-parts command without any issues.

Update:

As one of our reader Mr.Danesh mentioned in the comment section below, Instead of hard-coding the path of the interpreter, e.g.

#!/usr/bin/python3

We can use:

#!/usr/bin/env python3

This is more portable in case the interpreter is installed in some other (non-default) directory. env is a shell command for Linux and Unix-like operating systems. It is often used by shell scripts to launch the correct interpreter.

You can also use ShellCheck utility to find problems in your shell scripts.

Hope this helps.

You May Also Like

3 comments

DANESH FOROUHARI April 7, 2020 - 7:53 am

Instead of hard-coding the path of the interpreter, e.g.
#!/usr/bin/python3

you could use
#!/usr/bin/env python3

This is more portable in case the interpreter is installed in some other (non-default) directory

Reply
sk April 7, 2020 - 10:26 am

Yes, I am aware of it, but forgot to include in the guide Thanks for the input.

Reply
Jalal April 7, 2020 - 8:53 am

Hi,
Thanks a lot…
it is better to use shebang in every script.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More