Flylib.com

Books Software

 
 
 

Variable Substitution

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Appendix G.  Pdksh Quick Reference


Variable Substitution

Variable values can be accessed and manipulated using variable expansion. Basic expansion is done by preceding the variable name with the $ character. Other types of expansion can be used to return portions or the length of variables , use default or alternate values, assign default or alternate values, and more.

Variable Expansion Format

${ variable }

value of variable

${# variable }

length of variable

${ variable : ?/span> word }

value of variable if set and not null, else print word . If : is omitted, variable is only checked if it is set.

${ variable := word }

value of variable if set and not null, else variable is set to word , then expanded. If : is omitted, variable is only checked if it is set.

${ variable :?}

value of variable if set and not null, else print " variable: parameter null or not set ". If : is omitted, variable is only checked if it is set.

${ variable :? word }

value of variable if set and not null, else print value of word and exit. If : is omitted, variable is only checked if it is set.

${ variable :+ word }

value of word if variable is set and not null, else nothing is substituted. If : is omitted, variable is only checked if it is set.

${ variable # pattern }

value of variable without the smallest beginning portion that matches pattern

${ variable ## pattern }

value of variable without the largest beginning portion that matches pattern

${ variable % pattern }

value of variable without the smallest ending portion that matches pattern

${ variable %% pattern }

value of variable without the largest ending portion that matches pattern


   
Top
 

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Appendix G.  Pdksh Quick Reference


Special Parameters

Some special parameters are automatically set by the Korn shell, and usually cannot be directly set or modified.

$ #

number of positional parameters

$ @

all positional parameters (" $1 ", " $2 ", ..., " $ n ")

$ *

all positional parameters (" $1 $2 ... $ n ")

$ ?

exit status of the last command

$$

process id of the current shell

$ ?/span>

current options in effect

$ !

process id of last background command


   
Top
 

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Appendix G.  Pdksh Quick Reference


Special Variables

There are a number of variables provided by the Korn shell that allow you to customize your working environment. Some are automatically set by the Korn shell, some have a default value if not set, while others have no value unless specifically set.

CDPATH

search path for cd when not given a full pathname (no default)

COLUMNS

window width for in-line edit mode and select command lists (default 80 )

EDITOR

pathname of the editor for in-line editing (default /bin/ ed )

ENV

pathname of the environment file (no default)

FCEDIT

default editor for the fc command

FPATH

search path for auto-loaded functions pathname of the history file

HISTFILE

pathname of the history file (default $HOME/ .sh_history )

HISTSIZE

number of commands to save in history file (default 128 )

HOME

home directory

IFS

internal field separator (default space, tab, newline)

LINES

specifies column length for select lists

MAIL

name of mail file

MAILCHECK

specifies how often to check for mail (default 600 seconds)

MAILPATH

search path for mail files (no default)

OLDPWD

previous working directory

OPTARG

value of the last getopts option argument

OPTIND

index of the last getopts option argument

PATH

search path for commands (default /bin:/usr/bin: )

PPID

process id of the parent shell

PS1

primary prompt string (default $, #)

PS2

secondary prompt string (default >)

PS3

select command prompt (default #?)

PS4

debug prompt string (default +)

RANDOM

contains a random number

REPLY

contains input to read command when no variables given

SECONDS

contains number of seconds since Korn shell invocation

SHELL

pathname of shell

TERM

specifies your terminal type (no default)

TMOUT

Korn shell timeout variable (default )

VISUAL

pathname of the editor for in-line editing


   
Top