Linux FAQ's & Manuals
- Linux Scripts
- Debian Install
- Bash For Beginners
- Bugzilla
- Consultants Guide
- GCC Manual
- Linux Command Line Tools
- Gnu Pascal Coding Standards
- Linux Installation Disk
- Labolatorium Linux(PL)
- Budowa systemu Linux(PL)
- Linux Dictionary
- Network Administrators
- Rescue Disk for Linux
- Red Hat Installation
- Red Hat Customization
- Red Hat Getting Started
- Red Hat Security
- Secure & Optimize
- Slackware Manual
- Suse Support
- Suse FAQ
InfoWorld: "Red Hat's Michael Evans, vice president of corporate development at the company, said afterward that liked the idea. But he expressed doubts about the effort since Hovsepian was involved..."
MySQL Ends Distribution of Enterprise Source Tarballs
Linux.com: "MySQL quietly let slip that it would no longer be distributing the MySQL Enterprise Server source as a tarball, not quite a year after the company announced a split between its paid and free versions..." With follow-up from MySQL's Kaj Arnö.
Vista Aiding Linux Desktop, Strategist Says
eWeek: "Microsoft will lose market share to open-source desktops, a Dell strategist says at LinuxWorld..."
Linux: Replacing atime With relatime
KernelTrap: "In a recent lkml thread, Linus Torvalds was involved in a discussion about mounting filesystems with the noatime option for better performance..."
Interview: Chris Mason About Btrfs
/home/liquidat:
10.3. operations on variables
10.3.1. arithmetic on variables
we discussed this already in section 3.4.6.
10.3.2. length of a variable
using the ${#var} syntax will calculate the number of characters in a variable. if var is "*" or "@", this value is substituted with the number of positional parameters or number of elements in an array in general. this is demonstrated in the example below:
[bob in ~] echo $shell /bin/bash [bob in ~] echo ${#shell} 9 [bob in ~] array=(one two three) [bob in ~] echo ${#array} 3 |
10.3.3. transformations of variables
10.3.3.1. substitution
${var:-word}
if var is not defined or null, the expansion of word is substituted; otherwise the value of var is substituted:
[bob in ~] echo ${test:-test} test [bob in ~] echo $test [bob in ~] export test=a_string [bob in ~] echo ${test:-test} a_string [bob in ~] echo ${test2:-$test} a_string |
this form is often used in conditional tests, for instance in this one:
[ -z "${columns:-}" ] && columns=80 |
it is a shorter notation for
if [ -z "${columns:-}" ]; then columns=80 fi |
see section 7.1.2.3 for more information about this type of condition testing.
if the hyphen (-) is replaced with the equal sign (=), the value is assigned to the parameter if it does not exist:
[bob in ~] echo $test2 [bob in ~] echo ${test2:=$test} a_string [bob in ~] echo $test2 a_string |
the following syntax tests the existence of a variable. if it is not set, the expansion of word is printed to standard out and non-interactive shells quit. a demonstration:
[bob in ~] cat vartest.sh #!/bin/bash # this script tests whether a variable is set. if not, # it exits printing a message. echo ${testvar:?"there's so much i still wanted to do..."} echo "testvar is set, we can proceed." [bob in testdir] ./vartest.sh ./vartest.sh: line 6: testvar: there's so much i still wanted to do... [bob in testdir] export testvar=present [bob in testdir] ./vartest.sh present testvar is set, we can proceed. |
using "+" instead of the exclamation mark sets the variable to the expansion of word; if it does not exist, nothing happens.
10.3.3.2. removing substrings
to strip a number of characters, equal to offset, from a variable, use this syntax:
${var:offset:length}
the length parameter defines how many characters to keep, starting from the first character after the offset point. if length is omitted, the remainder of the variable content is taken:
[bob in ~] export string="thisisaverylongname" [bob in ~] echo ${string:4} isaverylongname [bob in ~] echo ${string:6:5} avery |
${var#word}
and
${var##word}
these constructs are used for deleting the pattern matching the expansion of word in var. word is expanded to produce a pattern just as in file name expansion. if the pattern matches the beginning of the expanded value of var, then the result of the expansion is the expanded value of var with the shortest matching pattern ("#") or the longest matching pattern (indicated with "##").
if var is * or @, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list.
if var is an array variable subscribed with "*" or "@", the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list. this is shown in the examples below:
[bob in ~] echo ${array[*]} one two one three one four [bob in ~] echo ${array[*]#one} two three four [bob in ~] echo ${array[*]#t} one wo one hree one four [bob in ~] echo ${array[*]#t*} one wo one hree one four [bob in ~] echo ${array[*]##t*} one one one four |
the opposite effect is obtained using "%" and "%%", as in this example below. word should match a trailing portion of string:
[bob in ~] echo $string thisisaverylongname [bob in ~] echo ${string%name} thisisaverylong |
10.3.3.3. replacing parts of variable names
this is done using the
${var/pattern/string}
or
${var//pattern/string}
syntax. the first form replaces only the first match, the second replaces all matches of pattern with string:
[bob in ~] echo ${string/name/string} thisisaverylongstring |
more information can be found in the bash info pages.