1

I am currently applying the below command in csh through a shell script.

sed -i "s/cb $i$/cb $i $cb/" */callback_events

Where:

  • $i - Value of Variable i
  • $cb - Value of Variable cb
  • $ - To match the end of line

However, I am getting the following error with the above command.

Variable name must contain alphanumeric characters.

My current shell is /bin/csh.

muru
  • 193,181
  • 53
  • 473
  • 722
Karan Shah
  • 283
  • 1
  • 3
  • 10

4 Answers4

4

You must escape the character $ when it is to stand for itself inside a string in doublequotes; otherwise the shell will think that it introduces a variable expansion, and will be unhappy if the character after it is not alphabetic or one of the special variables.

sed -i "s/cb $i"\$"/cb $i $cb/" */callback_events

There are two levels of character interpretation here.

  1. First the shell reads the command and applies its rules. One of the rules is that inside doublequotes $ introduces variable expansion.

  2. After the shell has finished the command looks like this:

     sed -i s/cb <value-of-i>$/cb <value-of-i> <value-of-cb/ dir1/callback_events dir2/callback_events...
    

    Note that the quotes are gone, $i and $cb are replaced with their values, and \$ became just $. Also */callback_events got replaced with a list of files.

  3. This is then passed to sed, which applies its rules. One of those rules is that a $ at the end of the search pattern means end-of-line.

AlexP
  • 10,037
  • 1
  • 32
  • 39
  • I want `$` as an end of line, not as a `$` character – Karan Shah Dec 12 '16 at 11:17
  • You want `$` as a `$` character _in the doublequoted string_. `sed` will then interpret it as end-of-line. – AlexP Dec 12 '16 at 11:19
  • With that command, now error is not coming up but the substitution also didn't happen. So may be the shell could not find the matching pattern and hence the replacement was not done – Karan Shah Dec 12 '16 at 11:26
  • Check that `cb $i cb` is actually to be found at the end of a line. – AlexP Dec 12 '16 at 11:33
0

On Ubuntu Studio 18 the $ error is due to bash expansion notice the errors when you run $0 vs $(0) vs $0

An alternative to seeking to the end of line in sed script is finding everything and using the & variable to append things after it; It's used like this:

$ echo "this was here" | sed -e 's_.*_& YOURFOOBAR_'

If you are learning sed script you can seek to front like home key with ^ and add multiple sed commands with ; character(s). Here I use _ or / interchangeably for the replacement sed script command 's///'

$ echo "this was here" | sed -e 's/^/PREFIXEDTEXT /;s_.*_& SUFFIXEDTEXT_'

NOTE: Test command is aliased like so alias [=test so this make problems I think with bash Expansion. see man bash things like &| don't work.

Lastly, I suggest buying a book off ebay rather than trying to learn here.

Simon Sudler
  • 3,771
  • 3
  • 20
  • 33
openInvent
  • 47
  • 5
0

Using sed copy all the contents and paste and insert the character at end, not efficient, but works

alias insert_single_quote_at_end ' echo hi | sed "s/.*/&'\''/" '

output:

hi'
Mana
  • 1
0

I also faced troubles, and the easiest way I could achieve this is by using sed -n l:

sed -i "s/cb `echo $i | sed -n l`/cb $i $cb/" */callback_events