Handle escape characters in a string
echo -n "Please enter filename(s) in regular expression: "
read -r option # assume that the user typed in: .*\.txt echo $option # prints out .*\.txt How can I change the content of $option to be .*\\.txt? Why can't I just do: echo $option | tr '\' '\\' ? I appreciate any help! -Helene |
Code:
#!/bin/bash |
Excellent, digiot! Now I'm soon ready to deliver my Uni assignment ;)
|
Uni assignment? Argh. I can usually tell when it's a homework thing, but I didn't realize it in this case. You do know you're not supposed to ask for homework help, right? I mean, you can ask about principles and I or others would offer hints and principles, but just straight questions and answers are kind of a no-no.
Ah well, bit past the point now. Good luck with the classwork, anyway. |
Well, I'm only asking about principles, digiot. I would never have asked for direct answers when doing assignments! :study: If you are still willing to help me, I have one more question:
test=`echo $option | sed 's/\\/\\\\/''` This results in Unterminated `s' command. Why? I did find another way aronud (not pretty), but I'm just curious :rolleyes: |
Actually, what you posted has two single quotes, so gets 'unexpected EOF' for me, but
Code:
test=`echo $option | sed 's/\\/\\\\/'` The original 'backticks' method is treated differently and the newer way works: Code:
test=$(echo $option | sed 's/\\/\\\\/') Code:
~ Just out of curiosity, what was the way around? (If you don't want to say, that's okay. I, too, know the shame of ugly workarounds. :) ) |
This one seems to be fine! I didn't know/think of using $().
test=$(echo $option | sed 's/\\/\\\\/') And yes, I had a typo. I didn't mean to write two single quotes, only one. My ugly solution was to write the variable to a file, and write it back into a variable using awk... echo $option | sed 's/\\/\\\\/' > tmpFile ..... :rolleyes: |
Yeah, I try to avoid temp files but sometimes it does seem like the least troublesome route. :) Glad the other suits.
|
All times are GMT -5. The time now is 01:17 AM. |