How to Resolve the ‘/bin/sh: 1: source: not found’ Issue

When working with shell scripts, you may encounter an error message like ‘/bin/sh: 1: source: not found.’ This error usually occurs when a script relies on the ‘source’ command, and your system’s default shell does not support it. In this blog post, we will explore the causes of this issue and discuss various solutions to resolve it.

Understanding the Problem:

The error message ‘/bin/sh: 1: source: not found’ indicates that the ‘source’ command is not available in the shell you are using. This can be confusing, especially if you expect the ‘source’ command to work. To understand and fix this issue, consider the following aspects:

  1. Shell Compatibility: The ‘source’ command is a bashism, meaning it is specific to the Bash shell. If your script uses ‘/bin/sh’ as the interpreter and relies on ‘source,’ you may encounter this problem. Many systems use different shells for ‘/bin/sh,’ and some do not support ‘source’ at all.
  2. Ubuntu and Dash: On Ubuntu, the default ‘/bin/sh’ shell is often Dash, which does not support the ‘source’ command. This is a deliberate choice to create a lightweight shell, but it can cause issues when running scripts designed for Bash.

You Might Like This :

Solutions to Resolve the Issue:

Now, let’s explore some solutions to resolve the ‘/bin/sh: 1: source: not found’ issue:

  1. Modify the Shebang Line:
  • Open your script in a text editor.
  • At the top of the script, find the shebang line (the first line), which looks like ‘#!/bin/sh.’
  • Change it to ‘#!/bin/bash’ to ensure that the script is executed with Bash.
  • Save the script.
  1. Use the ‘.’ Command:
  • Instead of using ‘source,’ you can use the ‘.’ command to achieve the same effect.
  • Modify the lines in your script that use ‘source’ to use the ‘.’ command. For example, replace ‘source myscript.sh’ with ‘. myscript.sh.’
  • This change makes your script more compatible with different shells.
  1. Change the Shell Symbolically:
  • On some systems, ‘/bin/sh’ is symbolically linked to a specific shell (e.g., Dash). You can change the symbolic link to point to Bash instead.
  • Run the command: sudo ln -sf /bin/bash /bin/sh to make ‘/bin/sh’ point to Bash.
  1. Update the Jenkins Execute Shell:
  • If you encounter this issue in Jenkins, add the line #!/bin/bash at the top of your Execute Shell section. This will ensure that your Jenkins job runs with Bash.

Conclusion:

Resolving the ‘/bin/sh: 1: source: not found’ issue is crucial for ensuring your shell scripts work as expected. By modifying the shebang line, using the ‘.’ command, or changing the symbolic link, you can make your scripts compatible with a wider range of shells and avoid this common error. Understanding the nuances of shell compatibility is essential for smooth script execution on different systems.

Bipul author of nerdy tutorial
Bipul

Hello my name is Bipul, I love write solution about programming languages.

Articles: 146

Leave a Reply

Your email address will not be published. Required fields are marked *