Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3343962
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:00:53+00:00 2026-05-18T01:00:53+00:00

On Linux (BusyBox, QNAP-NAS) I want to add some extra path to my .bashrc

  • 0

On Linux (BusyBox, QNAP-NAS) I want to add some extra path to my .bashrc file via script using sed. The relevant part of the file looks as follows:

[...]
export PATH=\
/bin:\
/sbin:\
/usr/bin:\
/usr/sbin:\
/usr/local/bin
[...]

The extra line to be inserted (at position 4 in the original file) is /opt/bin:/opt/sbin:\. To get this done my sed one-liner looks like this:

sed '4i/opt/bin:/opt/sbin:\\' .bashrc > .bashrc.tmp

, correctly escaping the trailing backslash. Somehow sed converts the remaining \' into a newline eating up the trailing backslash, resulting in:

[...]
export PATH=\
/opt/bin:/opt/sbin:

/bin:\
[...]

Adding a third backslash gives me the trailing backslash, but still adds the newline, so

sed '4i/opt/bin:/opt/sbin:\\\' .bashrc > .bashrc.tmp

results into

export PATH=\
/opt/bin:/opt/sbin:\

/bin:\

If I add an extra space in my sed command

sed '4i/opt/bin:/opt/sbin:\\ ' .bashrc > .bashrc.tmp

everything looks fine, but I get the extra space at the end of line as well.

export PATH=\
/opt/bin:/opt/sbin:\  # extra space here

/bin:\

What did the trick for now is a second sed command removing the trailing spaces

#!/bin/sh

sed -e '4i/opt/bin:/opt/sbin:\\ ' .bashrc > .bashrc.tmp
sed -e 's/[ \t]*$//' .bashrc.tmp > .bashrc.tmp2  # change \t to real tab

But still I wonder why sed is transforming \' into a newline, and how to solve the above job with a simple one-liner? How can I insert (or append) a line with a trailing backslash using sed?
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:00:54+00:00Added an answer on May 18, 2026 at 1:00 am

    Here’s a workaround:

    sed '4s|^|/opt/bin:/opt/sbin:\\\n|' .bashrc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Linux environment with java,I'm having the config file which should be configured before
linux 3.5.6-1.fc17.x86_64 GNU bash, version 4.2.37 Hello, I have the following bash script file.
I have a linux busybox based system on a chip. I want to provide
In Linux, is it possible via a bash script to take '/dev/sr0' and determine
i am running an embedded linux device with busybox. after boot i want to
i am using busybox and linux 2.6.32.4. my configuration is based on fedora 12.
I'm actually trying to emulate the linux kernel using Qemu and busybox. So far
I'm using J9 VM to run my application on a Linux machine (BusyBox). When
Linux bash script: function Print() { echo $1 } Print OK This script runs
Linux bash script: #!/bin/bash function Print() { echo $1 } var=* Print $var Execution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.