0

Essentially what I am trying to do is take a string with a bunch of text and if it has a substring of "$$" to replace it with a substring of "$$$"

ex:

string="abcde\$\$fghi"
# Modify string
echo $string 
# ^ should give "abcde$$$fghi"

I have been at this for like 2 hours now and it seems like a very simple thing, so if anyone could provide some help then I would greatly appreciate it. Thanks!

EDIT: Changed original string in the question from "abcde$$fghi" to "abcde\$\$fghi"

  • Where do you get the string from? Because `string="abcde$$fghi"` won't come out as is, you'd have to do `string="abcde\$\$fghi"` – Rolv Apneseth Jul 20 '21 at 22:56
  • 1
    `$$` is a special variable in the shell, it contains the ID of the current process. The variables are expanded in double quotes, therefore `string` does not contain `$$` but a number (the PID of shell) instead. Enclose the string in apostrophes (single quotes) to get `$$` inside it. – axiac Jul 20 '21 at 22:56
  • `echo $string` can be very misleading in some situations (see ["I just assigned a variable, but `echo $variable` shows something else"](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else)). `echo "$string"` is better, and `printf '%s\n' "$string"` is better still. `declare -p string` (note the lack of `$`) can also help clarify what's actually stored in the variable (or array). – Gordon Davisson Jul 20 '21 at 23:11

2 Answers2

3

$$ is a special variable in the shell, it contains the ID of the current process. The variables are expanded in double quotes, therefore string does not contain $$ but a number (the PID of shell) instead.
Enclose the string in apostrophes (single quotes) to get $$ inside it.

The replacement you need can be done in multiple ways. The simplest way (probably) and also the fastest way (for sure) is to use / in the parameter expansion of $string:

echo "${string/'$$'/'$$$'}"

To make it work you have to use the same trick as before: wrap $$ and $$$ in single quotes to prevent the shell replace them with something else. The quotes around the entire expression are needed to preserve the space characters contained by $string, otherwise the line is split to words by whitspaces and and echo outputs these words separated by one space character.

Check it online.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • Sorry, I meant to put in the backslashes in my original question. Could you modify your answer to use double quotes instead of single? I edited it just now. – Jordan Hanley Jul 20 '21 at 23:13
  • The answer does not depend on the way you initialize `$string`. You can use the [heredoc syntax](https://www.gnu.org/software/bash/manual/bash.html#Here-Documents) as well, or read it from a file. The way to print a string that replaces `$$` with `$$$` in `$string` is the same and it is described in this answer. You need to wrap `${...}` in double quotes to preserve the spaces inside `$string` but that's the only reason. – axiac Jul 21 '21 at 09:39
2

If you quote the string with single quote marks (i.e. string='abcde$$fghi') you can do the replacement with echo "${string/'$$'/'$$$'}"

Edit: this is basically what @axiac said in their comment

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • 1
    Well, I only said about wrapping `$$` in apostrophes in my comment and it is a remark about a flaw in the code posted in the question but it does not answer the question. The rest of the answer is yours and it deserves an upvote. – axiac Jul 20 '21 at 23:06