In a Bash I have a variable:


Then I have another variable, which content starts with LOCAL_PATH and it should be removed:


The result should be:


I have tried it with the String manipulation of the Bash

RESULT=$(echo "${LINE//\/this\/is\/a\/path\/}")

But how I can include the variable LOCAL_PATH there to prevent the hard-coded path in the expression above?

1 Answer 1


Just include LOCAL_PATH in pattern part:

printf '%s\n' "${LINE//"$LOCAL_PATH"/}"

If LINE always start with content of LOCAL_PATH, POSIXly:

printf '%s\n' "${LINE#"$LOCAL_PATH"}"
  • @Gilles: It doesn't. a='/*/*/*/*/../../../../*/*/*/*/../../../../*/*/*/*'; echo "${b//$a/}" work well.
    – cuonglm
    Feb 11, 2016 at 1:22
  • Your first example will remove the local_part any place it happens. To remove it only at the beggining (with that idiom ) use: `printf '%s\n' "${LINE/#"$LOCAL_PATH"/}".
    – user79743
    Feb 11, 2016 at 3:32
  • @BinaryZebra: Ah right, I was confused, it's pattern matching matter, not filename expansion or fields splitting.
    – cuonglm
    Feb 11, 2016 at 8:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .