Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

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 
share|improve this question

marked as duplicate by jasonwryan, devnull, Thomas Nyman, terdon, Jenny D Apr 27 '14 at 8:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

You are confusing ${#var} (which returns the length of var) with ${var##substr} which returns the value of var after removing the longest prefix matching substr.

In this case the substring is */ i.e. all the leading path components of the name by which the script was invoked - leaving just the final filename component, script2. Although both expressions use the # character, the position of the # is significant.

There is also ${var#substr} which looks superficially even closer to ${#var} but returns the value of var after removal of the shortest matching prefix.

See for example Removing part of a string

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.