1

I have the string as follows:

$ echo "$val1"
"$29.95 Carryover Plan 1GB"

Now I can use this method to replace the spaces with underscores

$ echo "NAME_"${val1// /_}"_NAME"
NAME_"$29.95_Carryover_Plan_1GB"_NAME

And can do the same for replacing " double quotes with nothing

$ echo "NAME_"${val1//'"'/}"_NAME"
NAME_$29.95 Carryover Plan 1GB_NAME

Now I can use sed to do the same - to replace the spaces with underscores

$ echo "$val1" | sed s/" "/_/g
"$29.95_Carryover_Plan_1GB"

And I can then remove the double quotes by adding this sed s/'"'/''/g

$ echo "$val1" | sed s/" "/_/g | sed s/'"'/''/g
$29.95_Carryover_Plan_1GB

So this is what I want (spaces repalaced with underscore and double quotes removed) but can i achieve it using my first approach above e.g.

$ echo "NAME_"${val1// /_}"_NAME"
NAME_"$29.95_Carryover_Plan_1GB"_NAME

This is my attempt

$ echo "NAME_"${${val1// /_//'"'/}}"_NAME"
-sh: "NAME_"${${val1// /_//'"'/}}"_NAME": bad substitution

edit1 this is the sed I can achieve but I want to be able to achieve this by my first approach

$ echo "NAME_"$val1"_NAME" | sed s/" "/_/g | sed s/'"'/''/g
NAME_$29.95_Carryover_Plan_1GB_NAME
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98
  • see [here](http://stackoverflow.com/a/38559775/2392358) `a="NAME_${${val1// /_}//\"/}_NAME"` can be done in zsh – HattrickNZ Jul 26 '16 at 01:51

2 Answers2

3

There is no way to nest string manipulation operations on a variable because the first parameter in the ${var/sub/rep} is a variable name, not a string.

NAME='"$29.95 Carryover Plan 1GB"'
NAME=${NAME// /_}
NAME=${NAME//\"/}

Coincidentally, sed is a scripting language; you can trivially perform multiple substitutions in one script. See combining 2 sed commands.

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318
2

Sorry, you can't. Also, the quoting is wrong in all of your commands:

Wrong:

$ echo "NAME_"${val1// /_}"_NAME"
NAME_"$29.95_Carryover_Plan_1GB"_NAME

Right:

$ echo "NAME_${val1// /_}_NAME"
NAME_"$29.95_Carryover_Plan_1GB"_NAME

Wrong:

$ echo "NAME_"${val1//'"'/}"_NAME"
NAME_$29.95 Carryover Plan 1GB_NAME

Right:

$ echo "NAME_${val1//\"/}_NAME"
NAME_$29.95 Carryover Plan 1GB_NAME

Wrong:

$ echo "$val1" | sed s/" "/_/g
"$29.95_Carryover_Plan_1GB"

Right:

$ echo "$val1" | sed 's/ /_/g'
"$29.95_Carryover_Plan_1GB"

Wrong:

$ echo "$val1" | sed s/" "/_/g | sed s/'"'/''/g
$29.95_Carryover_Plan_1GB

Right:

$ echo "$val1" | sed 's/ /_/g; s/"//g'
$29.95_Carryover_Plan_1GB

Wrong:

$ echo "NAME_"${val1// /_}"_NAME"
NAME_"$29.95_Carryover_Plan_1GB"_NAME

Right:

$ echo "NAME_${val1// /_}_NAME"
NAME_"$29.95_Carryover_Plan_1GB"_NAME

Wrong:

$ echo "NAME_"${${val1// /_//'"'/}}"_NAME"
-sh: "NAME_"${${val1// /_//'"'/}}"_NAME": bad substitution

Right:

There is no right way to write that.

Wrong:

$ echo "NAME_"$val1"_NAME" | sed s/" "/_/g | sed s/'"'/''/g
NAME_$29.95_Carryover_Plan_1GB_NAME

Right:

$ echo "NAME_${val1}_NAME" | sed 's/ /_/g; s/"//g'
NAME_$29.95_Carryover_Plan_1GB_NAME

See if you can find a tutorial on shell quoting before it bites you.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • when you say wrong, specifically in your first wrong/right, do you mean bad syntax? I accept your point with the quotes in `sed` this looks better, `echo "$val1" | sed 's/ /_/g'`, but it does give the same result. maybe you could brifly comment.tks – HattrickNZ Jul 25 '16 at 22:30
  • 1
    I mean potentially dangerous and will fail in weird and wonderful ways given various values of the shell variable and/or the directory you execute the command from and/or your environment settings. Google something like "always quote shell variables" to see articles like http://unix.stackexchange.com/a/171347/133219 that discuss the issue. – Ed Morton Jul 25 '16 at 22:39