Thursday, April 28, 2005

Shell Parameter Expansion

This one is big so here I go.

${!prefix*}

This one expands the variables whose name starts with the prefix.One thing to note is that '*" is needed as shown below

$ echo ${!H*}
HISTCMD HISTFILE HISTFILESIZE HISTSIZE HOME HOSTNAME HOSTTYPE
$ echo ${!HISTCMD}

echo ${!HISTCMD*}
HISTCMD


${#parameter}
length in characters of the parameter but in case * and @ they refer to number of parameters.

$ cat parameter.sh
#!/bin/bash

echo ${#1}
echo $(#*}
$./parameter.sh abcdefghijklmnopqrstuvwxyz
26
1


${parameter#word}
${parameter##word}
try to match the word(it can be a pattern) against the parameter at the beginning and if it matches delete the shortest match

$ cat parametermatch.sh
#!/bin/bash

echo ${1#ab*}
echo ${1##ab*}
$ ./parametermatch.sh abcdefghi
cdefghi
$

${parameter%word}
${parameter%%word}
Samething as above but matches and deletes at the end
$ ./parametermatch.sh abcdefghi
abcdefg

$ cat parametermatch.sh
#!/bin/bash

echo ${1%*hi}
echo ${1%%*hi}

${parameter/pattern/string}

$ cat parametermatch.sh
#!/bin/bash

echo ${1/pattern/replacepattern}
$ ./parametermatch.sh hipattern
hireplacepattern

0 Comments:

Post a Comment

<< Home