I am creating a basic shell script to manipulate a database as a college assignment. However, How many times I try, I can't get the sed command fully correct. The functionality where sed is to be used is this:

$name is the variable. User will supply the name via the terminal. This name will be looked up in the database and will be deleted if present.

The command I used is this:

sed -i /$name/d <filename>

However, this command only works if the entered value contains no space.

I tried out my solution by using what I could salvage from this such as sed -i '/$name/d' <filename>

However, to no avail. How to go about this?

  • Adding quotes somehow doesn't work – user324028 Jun 30 '19 at 11:16
  • 1
    Really? Show me. – glenn jackman Jun 30 '19 at 11:18
  • Err.... would drive link do? – user324028 Jun 30 '19 at 11:20
  • 3
    Note that building a database that you manipulate with sed is extremely fragile. To take an example, what's stopping a user from deleting everything by entering . into the value of that variable? How would you prevent disclosing the contents of it when someone enters a value that contains actual sed code? – Kusalananda Jun 30 '19 at 11:57
  • 1
    In this particular case, you could get away with a single set of double quotes: sed -i "/$name/d" <filename>" but there are other cases where you have to mix quotes in order to get the shell to do what you want. – NickD Jun 30 '19 at 12:05