ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
recognises them as syntax
./testscript.sh: eval: line 6: unexpected EOF while looking for matching `''
./testscript.sh: eval: line 7: syntax error: unexpected end of file
Sorry I'm sure this is pretty basic stuff I should be able to get on my own, but I'm really stuck any help is greatly appreciated.
recognises them as syntax
./testscript.sh: eval: line 6: unexpected EOF while looking for matching `''
./testscript.sh: eval: line 7: syntax error: unexpected end of file
Sorry I'm sure this is pretty basic stuff I should be able to get on my own, but I'm really stuck any help is greatly appreciated.
The field inside the parameter substitution is considered a separate quoting environment from the main line, so quotes, the backslash itself, and a couple of other characters need to be individually escaped.
It may seem odd that for the single quote, the replacement string needs to be kept literal, but for the double quote the backslash needs to be escaped as well.
I believe this is due to the whole line being parsed after the substitution is made, where the outer double-quotes protect the single quotes in the variable string , but not the doubles. \" and \\ are parsed inside double quotes, but \' is not.
See, and carefully read, the QUOTING section of the bash man page for more details on this.
using FILENAME="${FILENAME//\'/\'}" instead of FILENAME="${FILENAME//'/\'}" did the trick. I would have probably never figured this out on my own thanks guys!
My question would be around what is the point of replacing an apostrophe with itself?
It's not. It's replacing the apostrophe with a backslashed apostrophe. The replacement string outputs literally. Try it out.
It appears to work similarly to sed substitutions. The LHS needs to strictly parse escapes, but the RHS does not. The only backslash interpretation that occurs on the substituted string comes after the parameter has been expanded, as I pointed out before.
So I would consider this tried out and not what has been advertised
Code:
$ bash --version
GNU bash, version 4.2.10(1)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
You didn't quote the variable, so the backslash was subsequently removed again after the expansion.
The behavior of backslashes is different inside double quotes than when unquoted. Whereas all escapes are processed in unquoted strings, soft quotes only reduce \$, \`, \", \\, and \<newline> into their literal equivalents, along with certain escape sequences like \n. All other combinations are retained as-is.
Wow, that is a tricky one. I honestly can't see any direct way to accomplish it. It looks like you may have discovered a genuine weakness in the syntax.
Quoting and backslashing doesn't work, obviously, and I even tried this, with no success:
Code:
$ echo "${x//e/$'\047'}"
bash: bad substitution: no closing `}' in "${x//e/'}"
But this does work, probably because the ascii conversion takes place after the substitution:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.