Issue
I have two variables and the outputs of them are like below.
echo $VAR_ONE
'https://url_one','https://url_two'
echo $VAR_TWO
'https://url_two'
I want to remove 'https://url_two' (Which is $VAR_TWO) from $VAR_ONE and set to another variable. I executed the below but gives an error.
VAR_THREE=$(echo $VAR_ONE | sed s/"$VAR_TWO"//)
sed: bad option in substitution expression
Added to a script as well but the same error
#!/bin/bash
VAR_ONE=$(echo "'https://url_one','https://url_two'")
VAR_TWO=$(echo "'https://url_two'")
VAR_THREE=$(echo $VAR_ONE | sed s/"$VAR_TWO"//)
Solution
With bash, no external call is needed. Its "pattern substitution" form of parameter expansion is powerful enough:
# generate a variable containing all legal 8-bit ASCII characters
# (I believe should work with utf, etc also, in appropriate locales)
all=$(perl -E 'say map chr,1..255')
# get an idea what it contains
echo "$all" | od -c
echo
# create variable containing two copies
var1="$all================$all"
new='----------------'
# replace first copy
echo "${var1/"$all"/"$new"}" | od -c
echo
# replace all copies
echo "${var1//"$all"/"$new"}"
output:
0000000 001 002 003 004 005 006 \a \b \t \n \v \f \r 016 017 020
0000020 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037
0000040 ! " # $ % & ' ( ) * + , - . / 0
0000060 1 2 3 4 5 6 7 8 9 : ; < = > ? @
0000100 A B C D E F G H I J K L M N O P
0000120 Q R S T U V W X Y Z [ \ ] ^ _ `
0000140 a b c d e f g h i j k l m n o p
0000160 q r s t u v w x y z { | } ~ 177 200
0000200 201 202 203 204 205 206 207 210 211 212 213 214 215 216 217 220
0000220 221 222 223 224 225 226 227 230 231 232 233 234 235 236 237 240
0000240 241 242 243 244 245 246 247 250 251 252 253 254 255 256 257 260
0000260 261 262 263 264 265 266 267 270 271 272 273 274 275 276 277 300
0000300 301 302 303 304 305 306 307 310 311 312 313 314 315 316 317 320
0000320 321 322 323 324 325 326 327 330 331 332 333 334 335 336 337 340
0000340 341 342 343 344 345 346 347 350 351 352 353 354 355 356 357 360
0000360 361 362 363 364 365 366 367 370 371 372 373 374 375 376 377 \n
0000400
0000000 - - - - - - - - - - - - - - - -
0000020 = = = = = = = = = = = = = = = =
0000040 001 002 003 004 005 006 \a \b \t \n \v \f \r 016 017 020
0000060 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037
0000100 ! " # $ % & ' ( ) * + , - . / 0
0000120 1 2 3 4 5 6 7 8 9 : ; < = > ? @
0000140 A B C D E F G H I J K L M N O P
0000160 Q R S T U V W X Y Z [ \ ] ^ _ `
0000200 a b c d e f g h i j k l m n o p
0000220 q r s t u v w x y z { | } ~ 177 200
0000240 201 202 203 204 205 206 207 210 211 212 213 214 215 216 217 220
0000260 221 222 223 224 225 226 227 230 231 232 233 234 235 236 237 240
0000300 241 242 243 244 245 246 247 250 251 252 253 254 255 256 257 260
0000320 261 262 263 264 265 266 267 270 271 272 273 274 275 276 277 300
0000340 301 302 303 304 305 306 307 310 311 312 313 314 315 316 317 320
0000360 321 322 323 324 325 326 327 330 331 332 333 334 335 336 337 340
0000400 341 342 343 344 345 346 347 350 351 352 353 354 355 356 357 360
0000420 361 362 363 364 365 366 367 370 371 372 373 374 375 376 377 \n
0000440
---------------================---------------
Answered By - jhnc Answer Checked By - Cary Denson (WPSolving Admin)