Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

perfectionist needs help with ${//} substitution

2 views
Skip to first unread message

Todd

unread,
Mar 16, 2012, 9:27:02 PM3/16/12
to
Hi Guys,

Okay, I know I am being a perfectionist here, but this
keeps bugging me. How do I do the following ${//} substitution
with a single statement, instead of two?

Before: My\x20Screwball\x20Label
After: My_Screwball_Label

#!/bin/bash
b=$'\134' # backslash
L1="My\x20Screwball\x20Label"
L2="${L1//$b$b/}"
Label="${L2//x20/_}"
echo "$Label"

Frustrated,
-T

Chris Davies

unread,
Mar 17, 2012, 5:29:44 AM3/17/12
to
Todd <To...@invalid.invalid> wrote:
> Okay, I know I am being a perfectionist here, but this
> keeps bugging me. How do I do the following ${//} substitution
> with a single statement, instead of two?

> Before: My\x20Screwball\x20Label
> After: My_Screwball_Label

#!/bin/bash
#
L1="My\x20Screwball\x20Label"
Label="${L1//\\x20/_}"
echo "$Label"


Am I missing something?
Chris

Anonymous Remailer (austria)

unread,
Mar 17, 2012, 7:56:42 AM3/17/12
to
Chris Davies <chris-use...@roaima.co.uk> [CD]:

CD> Am I missing something?

An extra backslash, or twenty...


Todd

unread,
Mar 17, 2012, 4:46:43 PM3/17/12
to
From the command line:

$ b=$'\134' # backslash
$ L1="My\x20Screwball\x20Label"

$ echo "$L1"
My\x20Screwball\x20Label

echo "${L1//$b$bx20/_}"
My\x20Screwball\x20Label

$ echo "${L1//$b$b$bx20/_}"
My_x20Screwball_x20Label

Can't win.

-T

marrgol

unread,
Mar 17, 2012, 7:14:37 PM3/17/12
to
On 2012-03-17 21:46, Todd wrote:
>>> Before: My\x20Screwball\x20Label
>>> After: My_Screwball_Label
>>
>> #!/bin/bash
>> #
>> L1="My\x20Screwball\x20Label"
>> Label="${L1//\\x20/_}"
>> echo "$Label"
>>
>> Am I missing something?
>
> From the command line:
>
> $ b=$'\134' # backslash
> $ L1="My\x20Screwball\x20Label"
>
> $ echo "$L1"
> My\x20Screwball\x20Label
>
> echo "${L1//$b$bx20/_}"
> My\x20Screwball\x20Label
>
> $ echo "${L1//$b$b$bx20/_}"
> My_x20Screwball_x20Label
>
> Can't win.

$ echo "${L1//$b${b}x20/_}"
My_Screwball_Label

--
mrg

Wayne C. Morris

unread,
Mar 17, 2012, 7:57:13 PM3/17/12
to
In article <jk2t7l$fio$1...@dont-email.me>, Todd <To...@invalid.invalid> wrote:

> On 03/17/2012 02:29 AM, Chris Davies wrote:
> >
> > #!/bin/bash
> > #
> > L1="My\x20Screwball\x20Label"
> > Label="${L1//\\x20/_}"
> > echo "$Label"
>
> From the command line:
>
> $ b=$'\134' # backslash
> $ L1="My\x20Screwball\x20Label"
>
> $ echo "$L1"
> My\x20Screwball\x20Label
>
> echo "${L1//$b$bx20/_}"
> My\x20Screwball\x20Label
>
> $ echo "${L1//$b$b$bx20/_}"
> My_x20Screwball_x20Label
>
> Can't win.
>
> -T

The reason your attempts aren't working is that $bx20 is interpreted as a
variable. Use braces to separate the variable name from the "x20":

$ b=$'\134' # backslash
$ L1="My\x20Screwball\x20Label"
$ echo "${L1//$b${b}x20/_}"
My_Screwball_Label

But a much simpler solution is to use a literal backslash character instead of a
variable, as Chris suggested above:

$ L1="My\x20Screwball\x20Label"
$ echo "${L1//\\x20/_}"
My_Screwball_Label

Todd

unread,
Mar 17, 2012, 8:27:16 PM3/17/12
to
On 03/17/2012 04:57 PM, Wayne C. Morris wrote:
>> From the command line:
>> >
>> > $ b=$'\134' # backslash
>> > $ L1="My\x20Screwball\x20Label"
>> >
>> > $ echo "$L1"
>> > My\x20Screwball\x20Label
>> >
>> > echo "${L1//$b$bx20/_}"
>> > My\x20Screwball\x20Label
>> >
>> > $ echo "${L1//$b$b$bx20/_}"
>> > My_x20Screwball_x20Label
>> >
>> > Can't win.
>> >
>> > -T
> The reason your attempts aren't working is that $bx20 is interpreted as a
> variable. Use braces to separate the variable name from the "x20":
>
> $ b=$'\134' # backslash
> $ L1="My\x20Screwball\x20Label"
> $ echo "${L1//$b${b}x20/_}"
> My_Screwball_Label
>
> But a much simpler solution is to use a literal backslash character instead of a
> variable, as Chris suggested above:
>
> $ L1="My\x20Screwball\x20Label"
> $ echo "${L1//\\x20/_}"
> My_Screwball_Label


${b} mumble mumble mumble

Thank you all!

-T

Chris Davies

unread,
Mar 18, 2012, 5:09:39 AM3/18/12
to
Chris Davies <chris-use...@roaima.co.uk> [CD]:
CD> Am I missing something?

"Anonymous Remailer (austria)" <mixm...@remailer.privacy.at> wrote:
> An extra backslash, or twenty...

Huh?
Chris

Anonymous Remailer (austria)

unread,
Mar 18, 2012, 1:51:28 PM3/18/12
to
Chris Davies <chris-use...@roaima.co.uk> [CD]:
CD>> An extra backslash, or twenty...
CD> Huh?

The solution you suggested doesn't seem to achieve what the OP
wants (ie. the backslashes survive your string substitution
expression).


Chris Davies

unread,
Mar 18, 2012, 3:01:30 PM3/18/12
to
"Anonymous Remailer (austria)" <mixm...@remailer.privacy.at> wrote:
> The solution you suggested doesn't seem to achieve what the OP
> wants (ie. the backslashes survive your string substitution
> expression).

Not over here they didn't
Chris

Anonymous Remailer (austria)

unread,
Mar 19, 2012, 4:50:42 PM3/19/12
to
Chris Davies <chris-use...@roaima.co.uk> [CD]:
CD> Not over here they didn't

What is your bash version?

My tests were done under bash 2.05.


Todd

unread,
Mar 19, 2012, 5:11:54 PM3/19/12
to
Hi CD,

$ rpm -qa bash
bash-4.1.2-8.el6.x86_64

Back slashed work for me in a bash xterm, but do
not work so well inside a bash script. This is
why I use the

b=$'\134' # backslash

work around. See my prior post about sed being
evil.

And, I can never tell when \ will and won't work inside
a script. So, I use $b a lot

-T

I am still burning over ${b}. Had I know that before,
it would have saved me a lot of work in certain string
manipulations.

Chris Davies

unread,
Mar 20, 2012, 5:07:00 AM3/20/12
to
Todd <To...@invalid.invalid> wrote:
> $ rpm -qa bash
> bash-4.1.2-8.el6.x86_64

4.1.5 and 4.0.28(1) here. The script I posted works for me on both
versions; here's a run-time output:

chris:~$ cat /tmp/l
#!/bin/bash
#
L1="My\x20Screwball\x20Label"
Label="${L1//\\x20/_}"
echo "L1: $L1"
echo "Label: $Label"

chris:~$ /tmp/l
L1: My\x20Screwball\x20Label
Label: My_Screwball_Label


> I use the
> b=$'\134' # backslash

b='\' # Works for me (CD)

I'm still confused why (at least two of) you are seeing problems with
'\' at all. (Mind you, I don't have ready access to bash version 2.x so
I can't validate against that version.)

Put '\' inside single quotes and it will always be '\'. Put it
inside double quotes and it becomes a potential candidate for shell
expansion. Leave it outside any quotes and it will be interpreted as
an escape character. This is clearly documented in the manpage for bash
under the section for QUOTING.

Really puzzled,
Chris

Todd

unread,
Mar 20, 2012, 2:00:28 PM3/20/12
to
Hi Chris,

From a prior post (which is solved):
> I need two back slashes added to the a string:
>
> This works if I run it from the command line:
> $ FileName="z:\home\todd\abc.lpw"
> $ echo $FileName | sed -e 's/\\/\\\\/g'
>
> z:\\home\\todd\\abc.lpw
>
>
> But if I try to run it from a bash script:
>
> #!/bin/bash
> FileName="z:\home\todd\abc.lpw"
> FileName2="`echo $FileName | sed -e 's/\\/\\\\/g'`"
> echo "FileName <$FileName>"
> echo "FileName2 <$FileName2>"
>
> I get
> $ ./eraseme
> sed: -e expression #1, char 8: unterminated `s' command
> FileName <z:\home\todd\abc.lpw>
> FileName2 <>
>
>
> What am I doing wrong? What "unterminated "s"?
>
> [editorial comment] AAAAAHHHH. sed is evil. [/editorial comment]


The solution was the
b=$'\134' # backslash
And that is when I started using $b

-T

Chris Davies

unread,
Mar 20, 2012, 2:37:10 PM3/20/12
to
Todd <To...@invalid.invalid> wrote:
> The solution was the
> b=$'\134' # backslash
> And that is when I started using $b

I heard you in the other post, thank you.

But I don't understand WHY you need to jump through such complicated
hoops. As I offered in my first post, it "just works" here - and so should
"just work" for you, too.

Chris

Todd

unread,
Mar 21, 2012, 2:01:15 PM3/21/12
to
Oh. Easy. I was sticking with what I knew. I can not always tell
when the back slash will and won't work.

Don't feel dissed. Your suggestion got written down in my bash
substitution "keeper" file. You even got your name (and date)
attributed.

Thank you for the help.

-T

0 new messages