Echo Command Shell Variables
CTEC1863/2007F Operating Systems – Shell, Part 2 echo Command
Syntax is:
echo [ arg [ arg ... ] ]
echo copies its arguments to the STDOUT, each followed by a space. The last arg is followed by a newline.
The -n switch removes the newline from the end.
Examples:
$ echo PRINT THIS STRING PRINT THIS STRING $ $ echo -n 'enter name' enter name:$
Try this one:
$ echo *
Shell Variables
A shell variable is a name representing a string value.
There are four major categories of variables:
➔ Positional parameters
➔ User-defined variables
➔ Variables maintained by the shell
➔ Special variables
/Users/mboldin/2007F/ctec1863/unixnotes/UNIX-10-Shell_Part2.odt Page 1 of 8 CTEC1863/2007F Operating Systems – Shell, Part 2 Positional Parameters
Positional parameters are implicitly created by the shell when a shell procedure is invoked.
These variables are known as: $0, $1, $2, ..., $9
Example:
The command:
$ MYPROC CRUNCH
is equivalent to:
$ cc -o CRUNCH CRUNCH.c $ CRUNCH ARG1 ARG2
Try this:
$ cat > PARMSIN echo "Example of positional parameters" echo "$5" echo $1 echo $2 echo $3 $4 ^D $ chmod +x PARMSIN $ PARMSIN These parameters are passed
Now try changing the echo "$5" to echo '$5' and see what happens.
/Users/mboldin/2007F/ctec1863/unixnotes/UNIX-10-Shell_Part2.odt Page 2 of 8 CTEC1863/2007F Operating Systems – Shell, Part 2 shift Command
Often used when you wish to be passwd more that 10 ($0-$9) parameters.
After the shift command is executed, the positional parameters from $2 ... are renamed $1 ...
For example, if commandfile is invoked as:
commandfile arg1 arg2 arg3 arg4 arg5 arg6 $0 $1 $2 $3 $4 $5 $6
After a shift command is executed:
commandfile arg1 arg2 arg3 arg4 arg5 arg6 $0 gone $1 $2 $3 $4 $5 shift Example
Try this:
$ vi shiftit (in vi, enter the following text, save, and exit) echo "Example of shift command" echo $1 shift echo $1 shift echo $1 $ chmod +x shiftit $ ./shiftit shows shift command set Command
One can explicitly force values into the positional parameters using the set command.
For example set -- abc def ghi is equivalent to:
$1=abc Note: $2=def Positional parameters are not allowed on the left hand side of an $3=ghi assignment statement.
Try this:
$ vi setex set -- THESE THREE VALUES echo $1 $2 $3 $ sh setex
/Users/mboldin/2007F/ctec1863/unixnotes/UNIX-10-Shell_Part2.odt Page 3 of 8 CTEC1863/2007F Operating Systems – Shell, Part 2 User-Defined Variables
Spring into existence by being mentioned.
May be assigned string values.
Syntax: NAME=STRING
Thereafter "$NAME" will yield "STRING".
No spaces around the = sign.
Double quotation marks around the right-hand side allow STRINGto contain semicolons, spaces, tabs, andnewlines.
Example:
$ echo NAME NAME $ NAME=Howard $ echo $NAME Howard $
Can be used to shorten a pathname:
$ HIS="/usr/user3" $ ls $HIS
will yield a listing of user3's home directory.
Try this:
$ vi varscript echo "Example of setting variables" VAR1=BROWN VAR2=STONE CONCAT=$VAR1$VAR2 echo $VAR1 echo $VAR2 echo $CONCAT $ chmod +x varscript $ ./varscript
/Users/mboldin/2007F/ctec1863/unixnotes/UNIX-10-Shell_Part2.odt Page 4 of 8 CTEC1863/2007F Operating Systems – Shell, Part 2 Variables Maintained by the Shell
HOME Upon login, the shell assigns a user's login directory.
IFS Internal field separators, default is blank, tab, newline.
MAIL Pathname of the file where your mail is deposited.
PATH Search path used to find commands.
PS1 Prompt character(s).
PS2 Secondary prompt, used when the shell expects more input.
Predefined Special Variables
These five variables are: $# $? $$ $! $-
These variables contain information which is very helpful when you're writing a shell procedure, and are automatically set by the shell
$# records the number of arguments passed to a shell procedure.
For example,
$ someproc x y z
sets $# to 3.
One of the primary uses of this variable is to check for a required number of arguments.
if test $# -lt 2 then echo "Two or more arguments are required"; exit fi
$? contains the exit status of the last command executed.
A value of zero indicates successful completion.
A value between 1 and 255 indicates failure.
In a file of commands, it is common to only want a command to be executed if the last command was successfully completed.
/Users/mboldin/2007F/ctec1863/unixnotes/UNIX-10-Shell_Part2.odt Page 5 of 8 CTEC1863/2007F Operating Systems – Shell, Part 2
$$ contains the process number of the currently running process.
Process numbers are unique among all existing processes.
Often used if a shell procedure needs to create temporary files:
. . . temp=$HOME/temp.$$ ls > $temp . . . rm $temp . . .
$! contains the process number of the last process run in the background (using &).
$- is string containing the names of all the flags currently turned on in the shell.
$* is not considered a special variable. It contains $1, $2, ... each separated by a space.
Quoting
Double quotes are used to:
➔ Embed blanks in strings
➔ Neutralize special characters
Double quotes will allow expansion of shell variables.
Double quotes will command substitution.
Quotes cannot be nested.
Try this:
$ echo "DOUBLE QUOTES" $ VAR=STRING2 $ echo $VAR $ echo "$VAR" $ echo "$$"
/Users/mboldin/2007F/ctec1863/unixnotes/UNIX-10-Shell_Part2.odt Page 6 of 8 CTEC1863/2007F Operating Systems – Shell, Part 2 The backslash is a convenient way to quote a special character.
Often used to allow a command to be longer than one line.
Try this:
$ ls \
Precedence
➔ ' ' Undo Quotes " " \
➔ $1 $2 Variable Substitution
➔ ` ` Command Subsitution
➔ I/O Redirection
What will this produce:
$ vi PREC N=`grep $1 /etc/passwd` echo $N $ sh PREC user1
Grouping Shell Commands
Commands which are within parentheses are executed in a subshell.
One common use if if you need to change an environmental parameter, but you don't want the change to last:
Or if you want the output of two commands to go into another:
$ (ls -l /usr; ls -l /usr/user1) | grep "^d"
/Users/mboldin/2007F/ctec1863/unixnotes/UNIX-10-Shell_Part2.odt Page 7 of 8 CTEC1863/2007F Operating Systems – Shell, Part 2 Conditional Substitution
The shell normally replaces $VARIABLE with the appropriate string value.
Expression Meaning
If variable is set, resulting value is that of $VARIABLE, or else the value is that ${VARIABLE-STRING} of STRING.
If variable is set, return $VARIABLE, or else set variable to STRING and return ${VARIABLE=STRING} $VARIABLE.
If variable is set, return $VARIABLE, or else print a line of the form ${VARIABLE?STRING} "$VARAIBLE:STRING" and exit from the shell.
${VARIABLE+STRING} If variable is set, return $STRING, or else return NULL string.
In these cases a variable set to NULL is not the same as a variable not set. If a ':' preceeds the '{' then there is no distinction.
Example 1:
PATH=${PATH-':/bin:/usr/bin'}
If PATH has ever been set, keep its current value; otherwise set it to the string:
':/bin:/usr/bin'
Example 2:
cd ${HOME='/usr/gas'}
If HOME is been set, change directory to it; otherwise set HOME to:
'/usr/gas'
and change directory to it.
/Users/mboldin/2007F/ctec1863/unixnotes/UNIX-10-Shell_Part2.odt Page 8 of 8