What's wrong with the following code?
name='$filename | cut -f1 -d'.''
As is, I get the literal string $filename | cut -f1 -d'.'
, but if I remove the quotes I don't get anything. Meanwhile typing
"test.exe" | cut -f1 -d'.'
in a shell gives me the output I want, test
. I already know $filename
has been assigned the right value. What I want to do is assign to a variable the filename without the extension.
If your filename contains a dot (other than the one of the extension) then use this:
outputs:
Note that only the last extension is removed.
Two problems with your code:
I'd change your code to "name=`echo $filename | cut -f 1 -d '.' `", as shown below (again, notice the back ticks surrounding the name variable definition):
You should be using the command substitution syntax
$(command)
when you want to execute a command in script/command.So your line would be
Code explanation:
echo
get the value of the variable$filename
and send it to standard outputcut
commandcut
will use the . as delimiter (also known as separator) for cutting the string into segments and by-f
we select which segment we want to have in output$()
command substitution will get the output and return its valuename
Note that this gives the portion of the variable up to the first period
.
:use whichever you want. Here I assume that last
.
(dot) followed by text is extension.You can also use parameter expansion: