I found this easy filename printing on the internet. But I cant find explanation what does these ##*/
mean? It doesnt look like regex. More over, could it be used with result of readlink
in one line?
1 Answers
From Manipulating String, Advanced Bash-Scripting Guide
${string##substring}
Deletes longest match of
substring
from front of$string
.
So in your case, the *
in the substring
indicates: match everything.
The command echo ${full_path##/*}
will:
Print $full_path
unless it starts with a forward slash (/
), in that case an empty string will be shown
Example cases;
$ test_1='/foo/bar'
$ test_2='foo/bar'
$
$ echo "${test_1##/*}"
$ echo "${test_2##/*}"
foo/bar
$
Regarding your second question:
More over, could it be used with result of readlink in one line?
Please take a look at Can command substitution be nested in variable substitution?.
If you're using bash I'd recommend keeping it simple, by assigning the result of readlink
to a variable, then using the regular variable substitution to get the desired output. Linking both actions could be done using the &&
syntax.
An one-liner could look something like:
tmp="$(readlink -f file_a)" && echo "${tmp##/*}"

- 34,288
- 4
- 39
- 64
-
1thanks for the link to this man. Now I see how it is done and it makes sense now. And I see that it supposed to be ```##*/``` then it works better – anteastra Sep 14 '21 at 15:30
-
1Glad you've got it sorted out! If somebody [answers your question](https://stackoverflow.com/help/someone-answers), please consider [upvoting](https://meta.stackexchange.com/questions/173399/how-can-i-upvote-answers-and-comments) or [accepting](https://meta.stackexchange.com/q/5234/179419) the answer. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself.| – 0stone0 Sep 14 '21 at 15:31
-
you answered pretty clear for first part of question, thanks a lot. I still want to include into string manipulation the result of ```readlink``` command. If I mark now I will not get answer, but no worries, I will recognize your input – anteastra Sep 14 '21 at 15:47
-
1I understand. Would recommend using those together, using bash it's not possible. Please see my edit. The linked post has a very nice explanation. – 0stone0 Sep 14 '21 at 16:34