Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove the extension of a filename in a shell script?

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.

like image 650
mimicocotopus Avatar asked Aug 28 '12 04:08

mimicocotopus


People also ask

How do I remove a filename extension in bash?

Remove File Extension Using the basename Command in Bash If you know the name of the extension, then you can use the basename command to remove the extension from the filename. The first command-Line argument of the basename command is the variable's name, and the extension name is the second argument.

How do you remove a filename extension in Unix?

You should be using the command substitution syntax $(command) when you want to execute a command in script/command. name=$(echo "$filename" | cut -f 1 -d '. ')


2 Answers

You can also use parameter expansion:

$ filename=foo.txt $ echo "${filename%.*}" foo 

Just be aware that if there is no file extension, it will look further back for dots, e.g.

  • If the filename only starts with a dot (e.g. .bashrc) it will remove the whole filename.
  • If there's a dot only in the path (e.g. path.to/myfile or ./myfile), then it will trim inside the path.
like image 155
chepner Avatar answered Oct 13 '22 00:10

chepner


You should be using the command substitution syntax $(command) when you want to execute a command in script/command.

So your line would be

name=$(echo "$filename" | cut -f 1 -d '.') 

Code explanation:

  1. echo get the value of the variable $filename and send it to standard output
  2. We then grab the output and pipe it to the cut command
  3. The cut 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
  4. Then the $() command substitution will get the output and return its value
  5. The returned value will be assigned to the variable named name

Note that this gives the portion of the variable up to the first period .:

$ filename=hello.world $ echo "$filename" | cut -f 1 -d '.' hello $ filename=hello.hello.hello $ echo "$filename" | cut -f 1 -d '.' hello $ filename=hello $ echo "$filename" | cut -f 1 -d '.' hello 
like image 29
Rohan Avatar answered Oct 13 '22 01:10

Rohan