0

I've been trying to write a bash script. A part of it is supposed to replace a part of a string with nothing.

Here's what I'm trying to do

$dbname=$1
$dbNameActual="${$dbname/.sql/}"

date
echo $dbNameActual

I tried a number of suggestions from stack. But got nowhere. I tried adding sed, but that didn't seem to work.

The idea is that I have a script, and it takes in a db import file name, say db250317.sql and outputs db250317 .

I'm running Ubuntu 16.04 LTS.

Eksapsy
  • 1,541
  • 3
  • 18
  • 26
Igor Q.
  • 686
  • 10
  • 21

1 Answers1

3

You don't put $ twice in the expression, and you don't put $ before the variable you're assigning to (this isn't PHP or Perl). It should be:

dbNameActual="${dbname/.sql/}"

Also, if the thing you're trying to delete is always at the end, you can use % to remove it:

dbNameActual="${dbname%.sql}"

Also remember to quote the variable when you use it later, in case the filename contains spaces. You should almost always quote variables, unless you have a specific reason not to.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Maybe also mention putting quotes around the argument to echo; `echo "$dbNameActual"` (unless you specifically require the shell to perform whitespace tokenization and wildcard expansion on the value; see https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Aug 15 '17 at 03:38