I have assigned two variables but not able to update

x=$(cat /scratch/env.properties | grep ConfigPath)

then sed to replace ConfigPath in env.properties

sed 's/$x/$y/' env.properties

This is not updating ConfigPath in env.properties as assigned in $y


First, you don't need cat with grep. That is enough:

x="$(grep ConfigPath /scratch/env.properties)"

Second, I believe this is not an assignment you want:


If you want variable y to hold ConfigPath=/scratch/a/b string it should be:


$(...) is a command substitution in Bash.

Third, you should use double quotes in sed command to make shell expand x and y:

sed "s/$x/$y/" env.properties

Also notice that / is a poor choice when working with Unix paths because it is the delimiter. Use another character, for example comma:

sed "s,$x,$y," env.properties

As noted by user Kusalananda in the comment below you make this easier and better by using sed only and making sure that ConfigPath is at the beginning of the line:

sed "s,^ConfigPath=.*$,ConfigPath=/scratch/a/b," env.properties
  • The grep step does not seem necessary. If you want to replace a line saying ConfigPath=whatever, you don't first have to grep for it. Also, you may want to anchor the pattern in the sed expression, at least to the beginning of the line (or allow for blanks using e.g. /^[[:blank:]]*ConfigPath=.*/).
    – Kusalananda
    May 22 '19 at 18:36
  • Thanks for input, I updated my answer. (BTW, I also use Dvorak layout) May 22 '19 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.