Avatar of ugeb
Flag for United States of America asked on

sed one-liner for windows (bash shell)


I'm trying to do a one-liner using sed in a Cygwin bash on Windows.  I want to be able to copy a directory from Windows explorer to Cywin and have it cd to that directory without complaining about the slashes. For example, in explorer I copy the address field that looks like this:

C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\

I'd like to paste this into bash as follows:

cd C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\

and have it change directories without complaining about the orientation of the slashes.  The spaces cause an issue too, so I would probably have to do:

cd "C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\"

I've played around with sed, but I'm no expert on it and it ends up trying to cd to directory:


which doesn't exist.


Unix OSScripting LanguagesShell Scripting

Avatar of undefined
Last Comment

8/22/2022 - Mon

You can use single quotes instead of double quotes  or use the sed to replace the backslash to forward slash / double backslash.

sed 's!\\!/!g'

Open in new window

sed 's!\\!\\\\!g'

Open in new window


I'm actually looking for the whole thing.  What does the entire bash/sed line look like with the input I gave above?

echo "C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\"| sed 's!\\!\\\\!g'
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes

Thanks, but I guess I need to clarify.  I want to do this:

cdr "C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\"

where cdr is an alias for "~/scripts/cdr.sh", and cdr.sh is the shell script. What you gave is both fixed in terms of the directory string, and it doesn't change to that directory.  I already know how to change a string to the proper format, it's the changing of the directory that I can't figure out.

The following is the long way around that I took for cdr.sh in order to debug:

DIR2=`echo $DIR1 | sed -e 's:C:c:;s:\\\:\\/:g' | sed -e 's/://g'`
ls -halt $DIR2
cd $DIR2

Open in new window

I execute it as

./cdr.sh "C:\Users\Gene\AppData\Local\Google\Chrome\User Data\Default\Extensions\"

Open in new window

The 'ls' works, and the 'cd' doesn't.  That's what I can't figure out and need the help on.

Duncan Roe

You can't execute cd from a bash shell script. The script is obeyed by a separate bash process which changes directory but has no effect on the process that invoked it. You have to recode cdr as a function and source it (perhaps in .bashrc). Then it will work.
(CMD.EXE .BAT files can do CD, because the same process executes them (Windows process invocation is way slower than Linux))

That's interesting, I didn't know I couldn't cd from a shell script.

I have two issues.  

1)  This is within Cygwin, which is a Linux-like environment and so obeys that syntax.  I'm not in a windows terminal.  How do I cd from a script within Linux?

2)  I still have issues with spaces in directory names.  Even escaping the spaces doesn't work.


Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Duncan Roe

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

I'm pretty sure $* is what I wanted after reading the bash docs again, so I'm still having an issue with that.  But, I'll play around with it.

The function idea works great, though, so thank you for that!