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.
I don't understand the meaning of the sentence written in the subject. What I don't understand exactly is the meaning of ## in this case. What does it mean?
How does it combine with a ${variable} construction?
It removes the longest match from the front of the string. For example if the file name is something.tar.gz a single # would be tar.gz. And ## would be gz.
I'm currently reading the advanced bash scripting tutorial from tldp.org, and there are a lot of examples at the beginning using notions that are explained later on. Even so, I'm still trying to understand as much as possible at this stage so as to understand it much better later on when they're actually explained
I still haven't understood it fully at the stage, but both your links are useful, so I guess I'll get it eventually.
@michaelk what do you mean by 'the front of the string'? Do you mean to say the beginning?
So let's say we have:
filename=something.tar.gz
if [ ${filename##.*} != "gz" ]
Would it be a match in this case? If so, I don't understand the .* at the end, 'cause that's where .tar.gz or .gz should be, shouldn't it? It's not that straightfoward
P.S.
I've just read this from turbocapitalist's link. Made things much more comprehensible:
Get name without extension
${FILENAME%.*}
⇒ bash_hackers.txt
Get extension
${FILENAME##*.}
⇒ bash_hackers.txt
Get directory name
${PATHNAME%/*}
⇒ /home/bash/bash_hackers.txt
Get filename
${PATHNAME##*/}
⇒ /home/bash/bash_hackers.txt
One other question. Let's say I'm in /home/user. If I try:
echo ${PWD%/*}
I get:
/home
Is there a way I can get only the "home" string using substring removal without turning to another tool, such as sed, or tr or whatever?
Now I think it's a little bit of a silly question, 'cause you either start from the beginning or the end with subtring removal, so if you start from the end of the string, the first character of the string "/" is obviously going to be there
# replace all slashes (/) with a space in $PWD value
${PWD//\// }
# use the string delimited with spaces as array values
# array=($string_with_spaces)
path=(${PWD//\// })
# echo first element
echo ${path[0]}
Note: this only works with directories names that contain no spaces of course
(A work around is possible with IFS setting)
is not 100% correct because it also (not) matches the file mame "gz".
To only (not) match a ".gz" name I suggest
Code:
if [[ $filename != *.gz ]]
I'm not sure if I follow you. So if the file is called "gz.tar", for instance, you mean to say that the condition will not verify, basically, if the file has the .gz extension? I'm not sure you're right. Can you expand on that?
Ok, now I get it. You mean to say that if there's a file called "gz" (without any extension), then it would take it (or not) to be a .gz archive, for instance, so it would be a completely correct condition. Yes, nice observation. Thank you for that
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.