This question already has an answer here:
- How do ${0##*/} and ${0%/*} work? 1 answer
In a uni text provided us to cover bash scripting, the following variable assignment has got be stumped and I've yet to get an answer back from anyone, hence hopefully someone on here can help.
name=${0##*/}
This should expand to provide the name of the command entered. I can see how this is taken from argument $0, as the first character in the braces is 0. However, according to the information on the previous page (of said uni doc) and to sources I've looked at on the web so far, # means the number of arguments the script was called with, and * contains the argument as a string. I just can't understand how these all of these special parameters are expanding only to argument $0.
I'd expect the name to expand to: code/script2 3 3 code/script2 fred george allison.
I'd really appreciate it if someone could explain what each of the special parameters between the braces is doing in order to expand to the name: code/script2
Thanks for your help in advance!
Below is the entire script and output for context:
1 #!/bin/bash
2 # A simple script showing some of the special variables
3
4 echo "The full name of this script is $0"
5
6 name=${0##*/}
7
8 echo "The name of this script is $name"
9
10 echo "The process ID of this script is $$"
11
12 echo "This script was called with $# parameters" 13
14 echo "The parameters are \"$*\""
16 echo "Parameter 1 is - $1"
17 echo "Parameter 2 is - $2"
18 echo "Parameter 3 is - $3"
prompt: code/script2 fred george allison
The full name of this script is code/script2
The name of this script is script2
The process ID of this script is 3501
This script was called with 3 parameters The parameters are "fred george allison"
Parameter 1 is - fred
Parameter 2 is - george
Parameter 3 is - allison