Coding⏱️ 3 min read📅 2026-06-11

How to Fix: sed Error sed: -e expression #1, char 7: unterminated `s' command

sed Error sed: -e expression #1, char 7: unterminated `s' command

Quick Answer: The error is caused by the backslash in the `s` command. Try using double quotes instead of single quotes around the `s` command.

The 'sed: -e expression #1, char 7: unterminated `s' command' error occurs when you attempt to use an invalid sed command in your shell scripts. This issue affects users who have recently updated their sed version or are using a new version of the GNU sed package.

This error can be frustrating because it prevents you from executing certain commands and scripts, which can hinder your productivity and cause delays in completing tasks. However, by following these steps, you should be able to resolve this issue and get back to working efficiently.

🔍 Why This Happens

  • The primary reason for this error is the use of an unterminated `s' command in the sed expression. The `s' command requires a closing delimiter (either '/' or ' ') to complete the substitution, but in your script, the command is left open, causing the error.
  • Another possible cause could be a mismatch between the sed version and the shell you are using. If the sed version is not compatible with the shell, it may throw an error like this.

🚀 How to Resolve This Issue

Fixing the Invalid Sed Command

  1. Step 1: To fix this issue, review your sed command to ensure that it is properly closed with a delimiter. In your case, you can add a closing '/' after the substitution pattern in the `cd2u` function.
  2. Step 2: Modify the `cd2u` function as follows: cd2u () { pushd `cat /dev/clipboard | sed -r "s'\\'/'g" | sed -r "s'^D:'/d'g" | sed -r "s'^C:'/c'g" /`; }
  3. Step 3: Save the changes to your .bashrc file and restart the shell or run `source ~/.bashrc` to apply the changes.

Alternative Fix Method

  1. Step 1: If the above fix does not work, you can try updating your sed version to a newer one that may be compatible with your shell. You can do this by running `apt-get update && apt-get install -y git-sed` (for Ubuntu-based systems) or `brew upgrade sed` (for macOS).
  2. Step 2: After updating the sed version, try running the `cd2u` function again to see if it resolves the issue.

💡 Conclusion

By following these steps and fixing the invalid sed command, you should be able to resolve the 'sed: -e expression #1, char 7: unterminated `s' command' error and get back to using your shell scripts efficiently.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions