[SOLVED] getting the filename without the extinsion in bash?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
getting the filename without the extinsion in bash?
Code:
#!/bin/bash
clear
echo "You just heard, $(cat ~/lastauto)"
lastfm="You just heard, $(cat ~/lastauto)"
function play ()
{
for file in ~/Downloads/*.mp3; do
clear
#file2 = echo "$(basename $file)"
echo "You just heard: $(cat ~/lastauto). You are listening to O-W-E-N. Next up, we have: $file . Lets go!"
espeak "You just heard, $(cat ~/lastauto). You are listening to O-W-E-N, Next up, we have, $file . Lets go!" 2>/dev/null
echo $file>~/lastauto
clear
mpg123 ./"$file"
done
play
}
play
I want to get the filename without the extension. ($file2 is marked out to remind me to fix it.)
The script is a mp3 player that tells me what just played and whats next, so i dont have to look.
Right now, it tells me what the song is, but says "dot mp3" after the file.
And, I cant ever get the basename. Any suggestions?
(I just started learning Bash earlier this week. and i made the script. the entire thing, so i didnt steal. >.<)
${string%substring}
Deletes shortest match of $substring from back of $string.
${string%%substring}
Deletes longest match of $substring from back of $string.
${string#substring}
Deletes shortest match of $substring from front of $string
${string##substring}
Deletes longest match of $substring from front of $string.
Basename is an external command While the string functions are internal. Which make the string functions faster. In this case that's not so important. Also I think it's cleaner but that's arguable.
"Easier to remember" is relative to your experience. Use the substitutions long enough and they become pretty much second nature, just like any other skillset. They aren't really that difficult to remember anyway, at least not the ones you'll use most often. There are actually only about 8 main patterns, and logical variations thereof. And the references are always at hand when you need them.
There is one slight advantage that basename has over the built-in, BTW. You can also pass it an optional suffix argument to remove the file ending at the same time.
Although even then you're still probably going experience faster performance doing it in the shell, even though it requires two separate substitutions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.