-1

I have tried assigning my bash variable and it works fine when i echo it. But as soon as i use the variable to assign another variable, it gives an error, that the command not exists.

dbFileName=hello123.mv.*
dbFile=$($dbFileName | sed -e 's|'.mv.*'|''|')
echo "$dbFile"

I want to alter the string stored in one variable and remove mv.* from the end.

This gives out the error that "hello123.mv.* : Command not found"

V.Bhanderi
  • 47
  • 8
  • also; echo $dbFileName | sed -e 's|'.mv.*'|''|' gives proper result – V.Bhanderi May 19 '20 at 12:09
  • $dbFileName contains the value "hello123.mv.something" You cannot just pass a string through a pipe, you'll have to use echo $dbFileName – athul.sure May 19 '20 at 12:21
  • This is an easier way to do it: https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash – athul.sure May 19 '20 at 12:23
  • @V.Bhanderi : You are running the content of `dbFileName` as a program. Since no filename in the working directory seems to match the pattern `hello123.mv.*`, this string is taken as the name of the program to be executed. Since you also don't have a program namedn `hello123.mv.*` in your PATH, you end up with this error message. – user1934428 May 19 '20 at 12:55
  • @V.Bhanderi please take a look at https://stackoverflow.com/help/someone-answers – Mahmoud Odeh May 21 '20 at 13:10

1 Answers1

0

you can use parameter expansion with % which will remove characters from the end of the string or # which will remove characters from the beginning of the string. If you use a single one of those characters, the smallest matching string will be removed. If you double the character, the longest will be removed.

dbFileName="hello123.mv.whateverhere"
echo ${dbFileName%.mv*} # hello123
dbFile=${dbFileName%.mv*}
echo ${dbFile} # hello123
Mahmoud Odeh
  • 942
  • 1
  • 7
  • 19