LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script to replace char in variable (https://www.linuxquestions.org/questions/programming-9/bash-script-to-replace-char-in-variable-251992/)

gmartin 11-06-2004 10:36 PM

bash script to replace char in variable
 
I have a variable $ipath that contains a path "/usr/local" which I need to convert to "_usr_local"

I built the following at the shell prompt to do this

Code:

export tpath=`echo $ipath | sed -e 's/\//_/g'`
That worked.

Now I need to move it into a script so I dropped the export and tried:

Code:

for ipath in $backupdirs
#replace slashes with underscores
tpath=`echo $ipath| sed -e 's/\//_/g'`

I get an error:
line 30: syntax error near unexpected token `tpath=`echo $ipath| sed -e 's/\//_/g'`'

Can you help me fix this or suggest a better approach?

gmartin 11-06-2004 10:46 PM

I hate answering my own posts. Problem was I added the sed code between the 'for' and the 'do'. Moving it to after the 'do' worked.

Sorry

ranger_nemo 11-06-2004 10:48 PM

This _should_ work...
Code:

do
#replace slashes with underscores
  tpath=$(echo $ipath | sed -e 's/\//_/g')
  echo $tpath
done


gmartin 11-06-2004 10:55 PM

Thanks ranger_nemo

/bin/bash 11-08-2004 11:21 AM

tpath=`echo -e ${ipath//:/\_}`

HTH :)

<EDIT> Oops...

tpath=`echo ${ipath//\//_}`

gmartin 11-08-2004 01:46 PM

Thanks, folks - problem solved AND I learned something


All times are GMT -5. The time now is 08:46 AM.