I have a Windows path in a bash variable as a string:
file='C:\Users\abcd\Downloads\testingFile.log'
I am trying to convert this path into a Linux path starting with /c/Users...
.
My attempt
The following works:
file=${file/C://c}
file=${file//\\//}
echo $file
> /c/Users/abcd/Downloads/testingFile.log
Problem
Here, I have done this for a string that contains the filepath. The reason I am asking this question is that I have to convert 20 such strings in a bash script in Ubuntu 16.04 and each time I do this I have to write 2 lines per conversion – it is taking up a lot of space!
Question
Is there a way to combine the 2 commands
file=${file/C://c}
file=${file//\\//}
into one command?
Best Answer
There would be a way to do both replacements at once using
sed
, but it's not necessary.Here's how I would solve this problem:
If you want to put the output into an array instead of printing, replace the
echo
line with an assignment: