4

I'd like to replace a string within a variable, so e.g.

Test=Today, 12:34

I'd like to replace the "Today" within the variable Test with a Date variable I declared before.

I tried using the "-sed" command,

sed -i -e "s/Today/$Date" '$Test'

but it would just print out an error and the file '$Test' is not known. So using sed is only possible using text files?

Kind regards, X3nion

X3nion
  • 91
  • 1
  • 7
  • 2
    `$Test` is not a file. You can use: `sed '....' <<< "$Test"` or better do it in bash itself using `"${Test/Today/$Date}"` – anubhava Sep 02 '20 at 14:26
  • 1
    In addition to what @anubhava said, note that in bash, variable expansion will not work in single quotes. If you want to expand the Test variable, you can either leave it unquoted `$Test` or put it in double-quotes `"$Test"`. If you use single quotes it will be treated as a string literal. – antun Sep 02 '20 at 14:33
  • When I write only the command "${Test/Today/$Date}", this doesn't work. What am I missing? – X3nion Sep 02 '20 at 14:44
  • 1
    Does this answer your question? [Replace one substring for another string in shell script](https://stackoverflow.com/questions/13210880/replace-one-substring-for-another-string-in-shell-script) – Let's try Sep 02 '20 at 14:58
  • 3
    `"${Test/Today/$Date}"` isn't a command, it's an expression that produces a string. You need to do something with that string, like set a variable to it (`Test="${Test/Today/$Date}"` would replace the current value of `Test` with the modified version), print it (`echo "${Test/Today/$Date}"`), or something like that. – Gordon Davisson Sep 02 '20 at 15:01

2 Answers2

7

First, that's a syntax error.

$: Test=Today, 12:34
bash: 12:34: command not found

Put some quoting on it.

$: Test="Today, 12:34"
$: Test='Today, 12:34'
$: Test=Today,\ 12:34

Then you can just use built-n bash parameter parsing.

$: Test='Today, 12:34'
$: Date=12.12.2000
$: Test="${Test/Today/$Date}"
$: echo "$Test"
12.12.2000, 12:34
Paul Hodges
  • 10,927
  • 1
  • 16
  • 30
3

This works for me:

Test="Today, 12:34"
Date=12.12.2000
sed 's/Today/'"$Date"'/g' <<<"$Test"

Edit: If you would like to change the variable Test, like mentioned in the comment, you need the assignment:

Test="Today, 12:34"
Date=12.12.2000
Test=$(sed 's/Today/'"$Date"'/g' <<<"$Test")
TheSlater
  • 576
  • 1
  • 3
  • 11