Книга: Fedora™ Unleashed, 2008 edition
Built-In Variables
Built-In Variables
Built-in variables are special variables provided to the shell by Linux that can be used to make decisions within a shell program. You cannot modify the values of these variables within the shell program.
Some of these variables are the following:
? $#
— Number of positional parameters passed to the shell program
? $?
— Completion code of the last command or shell program executed within the shell program (returned value)
? $0
— The name of the shell program
? $*
— A single string of all arguments passed at the time of invocation of the shell program
To show these built-in variables in use, here is a sample program called mypgm2
:
#!/bin/sh
#my test program
echo "Number of parameters is $#"
echo "Program name is $0"
echo "Parameters as a single string is $*"
If you execute mypgm2
from the command line in bash as follows
$ bash mypgm2 Andrew Hudson
you get the following result:
Number of parameters is 2
Program name is mypgm2
Parameters as a single string is Sanjiv Guha
- Running a Shell Program
- Interpreting Shell Scripts Through Specific Shells
- Using Variables in Shell Scripts
- Using a Simple Script to Automate Tasks
- Built-In Variables
- Special Characters
- Comparison of Expressions in bash
- Special Statements: for, while, and Others
- Using Functions in Shell Scripts
- Reference
- CHAPTER 25 Using Perl
- CHAPTER 33 Writing and Executing a Shell Script
- Using Variables in Shell Scripts
- Using a Simple Script to Automate Tasks
- Comparison of Expressions in bash
- Using Functions in Shell Scripts
- 3.1.12 Volatile Variables
- 5.3.4 Using the mikroICD In-Circuit Debugger
- 6. Variables
- 11.1.2 Defining Functions
- Chapter 9. How a rule is built
- Using Double Quotes to Resolve Variables in Strings with Embedded Spaces