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 7585525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:11:51+00:00 2026-05-30T19:11:51+00:00

I am trying to do a bash script that: loop over some files :

  • 0

I am trying to do a bash script that:

  • loop over some files : OK
  • check if the first line matches this pattern (#!f:\test\python.exe) : OK
  • create a new path by changing the unix style to windows style : KO

Precisely,
From: \c\tata\development\tools\virtualenvs\test2\Scripts\python.exe
I want to get: c:\tata\development\tools\virtualenvs\test2\Scripts\python.exe

  • insert the new line by appending #! and the new path : KO

Follow is my script but I’m really stuck!

for f in $WORKON_HOME/$env_name/$VIRTUALENVWRAPPER_ENV_BIN_DIR/*.py
    do
        echo "----"
        echo file=$f >&2

        FIRSTLINE=`head -n 1 $f`        
        echo firstline=$FIRSTLINE >&2

        unix_path=$WORKON_HOME/$env_name/$VIRTUALENVWRAPPER_ENV_BIN_DIR/python.exe
        new_path=`echo $unix_path | awk '{gsub("/","\\\")}1'`       
        echo new_path=$new_path >&2

        # I need to change the new_path by removing the first \ and adding : after the first letter => \c -> c:

        new_line="#!"$new_path
        echo new_line=$new_line >&2

        case "$FIRSTLINE" in 
         \#!*python.exe* )
            # Rewrite first line
            sed -i '1s,.*,'"$new_line"',' $f
        esac        
    done

Output:

file=/c/tata/development/tools/virtualenvs/test2/Scripts/pip-script.py
firstline=#!f:\test\python.exe
new_path=\c\tata\development\tools\virtualenvs\test2\Scripts\python.exe
new_line=#!\c\tata\development\tools\virtualenvs\test2\Scripts\python.exe

Line that is written in the file: (some weird characters are written I do not know why…)

#!tatadevelopment   oolsirtualenvs  est2Scriptspython.exe

Line I am expecting:

#!c:\tata\development\tools\virtualenvs\test2\Scripts\python.exe
  • 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-30T19:11:52+00:00Added an answer on May 30, 2026 at 7:11 pm

    sed is interpreting the backslashes and characters following them as escapes, so you’re getting, e.g. tab. You need to escape the backslashes.

    sed -i "1s,.*,${new_line//\\/\\\\}," "$f"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a bash script that will process a list of files
I'm trying to write a bash script that can edit itself. The line of
I am trying to write a simple bash script that will search for files
I'm trying to find a bash script that will recursively look for files with
I'm trying to write my first semi advanced bash script that will take input
I have a bash script that checks some log files created by a cron
I am trying to write a bash script that goes through a file line
I'm trying to do a bash script that will find & copy similar files
I'm trying to run a bash script that has: ssh -l <username> <compname> 'sudo
I've been trying to implement a bash script that reads from wordnet's online database

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.