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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 stackoverflow.com/questions/10067266/…)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.