Unix & Linux tutorial to learn _4

## Chapter 12 Using shell: variables and options

  • Interactive shell
  • process
  • Environment Variables
  • shell variables
  • shell option
  • Metacharacters
  • Quote
  • External command
  • Built-in command
  • Search Path
  • History list
  • Autocomplete
  • Command line editing
  • Aliases
  • Initialization file
  • Note

$ SHELL echo
// display the name of the shell used
to pay attention, if we temporarily change the shell used, the above command is not showing up.

12.1 Interactive shell and non-interactive shell

Non-interactive program is run independently with the people, if we run a script, this time a shell is non-interactive shell.
When you run a shell script, it will automatically start a new shell, and explain the task to the script the shell process.

12.2 environment, processes and variables

Unix, each object is represented as a file or process, or to access the data file storage resources, and the process is the program being executed.
While the process is running, it needs access to the so-called environment , that is used to store a set of variable information.
Unix shell there are two different variables, "shell variables" and "Environment Variables"
are only four types of operations on variables:

  • Creating Variables
  • View a variable value
  • Modify the value of the variable
  • Destruction Variables

Global variables and local variables

  • shell variable is to create local variables of their shell
  • Environment variables are global variables

Unlike general programming, the shell boundary between local variables and global variables are fuzzy.
A variable also has the meaning of local and global variables. It is useful, for example, the shell itself (which means that these variables should be a shell variable), and is also useful for processes started by the shell (which means these variables should be an environment variable).
Bourne shell family (Bash, Korn shell) with the C-shell family (C-Shell, Tcsh) is somewhat different.
Bourne shell family: is defined as a local variable, or both for local variables and global variables. Not completely global variable.
Creating local variables are used. Export command shell variables modified to "shell + Environment" variable
Example: create a variable named HARLEY, and cool the value assigned to it:

HARLEY = cool
this time HARLEY just a shell variable, if you start a new shell or run a command, the new process can not access HARLEY, the following command will export to the environment HARLEY:
Export HARLEY

C-Shell family, environment variables from the command setenv create and name in capital letters, shell variable by the set creation command, and name in lowercase letters.

12.4 display environment variables: env, printenv

Most of the time, we use the default variable, you can use the default variable display env or printenv command

env
or
printenv

12.5 display shell variables: set

12.6 display and use the value of the variable: echo, print

To display the value of a variable, the variable name plus $ character after the variable name with curly braces to enclose:

echo $ {TERM}
under // braces can be omitted in most cases, like the following:
echo $ TERM

If there are characters using echo display element, it is necessary enclosed in double quotes.
For example: To display the value of the angle brackets TERM, being given by the following sentence:

echo The terminal type is <$TERM>.

Because the angle brackets are meta-characters, which itself represents a redirection, so here not display properly, then you can use double quotes, so the shell displays it literally.

echo "The terminal type is <$TERM>."
显示为:
The terminal type is <xterm-256color>.

All shell are allowed to use the echo command to display text and variables, Korn shell can also use the print command:

print "The terminal type is $TERM."

12.7 Bouren shell family use variables: export, unset

Bouren shell family, create a variable is very simple. example:

= Cool HARLEY
// make sure the spaces on both sides of the equal sign is not !!!

If you want to use a value whitespace (space or tab) is included, the value needs to be in double quotes "

WEEDLY="a cool cat"

If the variable exists, you can also use the same syntax to modify values ​​of variables. E.g:

Smart = HARLEY
// value HARLEY changed to "smart"

In a Bourne shell family, each variable is automatically set to a shell variable. Use the export command to export a variable to the environment.

HARLEY WEEDLY Export
// HARLEY and WEEDLY variables at the same time by a shell variable into a "shell + environment" variables.

However, there is a better way. export command actually allows simultaneous set variables and export to the environment. The syntax is:

Export NAME [= value ] Example:
Export the PAGER = less
// creates a variable value of less, and export it as "shell + Environment."

export allows you to specify one or more variable names that one can create and export multiple variables.
When you delete a variable, calling it the "reset (unset)" variable. The syntax is:

unset NAME
例:
unset HARLEY WEEDLY

12.8 C-Shell family uses variable: setenv, unsetenv, set unset

In the C-Shell family, and unsetenv use setenv command set (create) or reset (deleted) environment variables.
When the shell variable set or reset, set and unset command needed.
Set the environment variable :

setenv NAME [ value ]
// Note that the command does not use = (equal sign), Example:
setenv Cool HARLEY

Reset the environment variables:

unsetenv NAME
例:
unsetenv HARLEY

Setting shell variables:

SET name [= value ]
// Note that, the difference is that the environment variables, where use = (equal sign), Example:
SET = VT100 Term

Delete shell variables:

unset variable
// where the variable is a variable name, for example:
unset Term

The variable to understand the difference between null and delete variables:

set harley=cool
set harley
unset harley

The first create a shell variable named harley, and the assignment is "cool", the second command will set the value of the variable harley is "null", the last command to delete a variable harley.

12.9 shell options: set -o, set + o

For the C-Shell family is, you can use the shell variable control shell behavior.
The Bourne shell family, you need to use shell option .
Note the difference between shell option with shell variables, shell options such as on / off switch is the same.
When you open an option, he said set this option. This tells the shell to run in some way.
When this option is turned off, said reset this option. It also tells the shell to stop running in this manner.

  • shell option either off or on, they do not need to create.
  • When the shell option is set, it will shell options open.
  • When they re-shell options, will shell option is turned off.

Setting an option:
SET -o Option
to a reset option:
SET + O Option
embodiment, the monitor option settings:
SET -o monitor
to monitor reset option:
SET + O monitor

12.10 display shell option

Bourne shell family use the shell options to control the operation of the shell. Option to display the current value of the shell, can be set -o or set + o command itself:

set -o
set +o

12.11 machine-readable, human-readable

set -o in a way easy to read display output.
set + o to data suitable for use as a shell script.

12.12 Exercises

Guess you like

Origin www.cnblogs.com/cnyxj/p/11263633.html