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.

Looking at the file /etc/rc.d/init.d/functions, I can see the following is declaring a number of variables as local variables. However, why are the variables killlevel and pid_file have an equal sign after them?

killproc() {
        local RC killlevel= base pid pid_file= delay try

I'm also seeing the following syntaxes ... Any idea what ${1##*/} or ${2:-} bits are for?

base=${1##*/}
...
__pids_var_run() {
        local base=${1##*/}
        local pid_file=${2:-/var/run/$base.pid}
share|improve this question

migrated from serverfault.com Jan 26 at 5:36

This question came from our site for professional system and network administrators.

1 Answer 1

up vote 1 down vote accepted

These are known as parameter expansion,which is advance syntax of Shell scripting

  1. ${2:-/var/run/$base.pid} is example of

    ${VAR :-default }
    

    This will result in $VAR unless VAR is unset or null, in which case it will result in default. In given script, if ${2} is not set, then default value /var/run/$base.pid is taken

  2. base=${1##*/} is example of

    ${var#Pattern}

    You can strip $var as per given pattern from front of $var if f=/etc/resolv.conf then, echo ${f#/etc/} will remove /etc/ part and get a filename only

UPDATE:

f=/etc/httpd/httpd.conf
# This will return etc/httpd/httpd.conf
echo ${f#*/}  
# This will return httpd.conf
echo ${f##*/}

Single # is non-greedy whereas, double # is greedy approach of matching expression.

share|improve this answer
    
Thanks. I was testing it out and appears to be coming to the same conclusion but it's good to have someone verifying it. Just to be clear, base=${1##*/} means strip from the character "#" (there is a second #) and any repetition of it (due to the *) until the character "/"? –  user192702 Jan 26 at 6:33
    
${var#pattern} will remove from shortest front pattern and ${var##pattern} will remove from longest front pattern. –  Abhijeet Kasurde Jan 26 at 6:36
    
Okay can you give me an example just to be clear? I'm not following. Sorry. –  user192702 Jan 26 at 6:38
    
Also see following link for Parameter Exapsion -gnu.org/software/bash/manual/… –  Abhijeet Kasurde Jan 26 at 7:21
    
@AbhijeetKasurde I edited your post. One typo, but primarily formatting of the enumeration as I suspected you did not know how to indent the code under de standard enumeration syntax on this site. From reading I was not sure if you would wanted the sentence "You can strip..." to be indented under 2. as well, turn that back if you don't want it like that. –  Anthon Jan 26 at 7:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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