[Shell] shell variables in the basic syntax

I hope not white piao, the point of a brush praise or comments to walk, as well as prohibit [Reserved]
* This article is to explain the variables in the shell of what (note I do more, made only slowly, If there is a particular need a certain aspect, you can reply in the comments, if I have the relevant information I will give priority to the issue)
@environment: uninx and Linux are differences, but most commands are similar, this article examples of operating at Iterm2 + oyzsh + Mac systems, does not use remote terminal server. But much the same, the work is actually like this.

variable

By convention, Shell variable capital letters underlined by the whole composition, there are two types of Shell variables:

1. Environment Variables

Environment variables can be passed to the child process from the parent process, so the process environment variables Shell fork can be passed from the current Shell process (split) out of the child process. Shell may display environment variables for the current process with the printenv command.
printenv
printenv operating results
We said before that the difference between sh xxxx.sh and source xxx.sh, why not if some variable is empty case you want to get to know this variable. If you do not really understand the difference execute a shell script, I see another article on how to execute a shell script

2. Local variables

Shell currently exists only in the process (which is the difference between the local variables and environment variables), with the set command to display all variables that are currently defined in the Shell process (including local variables and environment variables) and functions.

Set a local variable (script syntax which will be frequently used)

Environment variables are process has any concept, and local variables are unique to Shell concept. In Shell, the definition and use of environment variables and local variables are similar. Defined in the Shell or assignment to a variable:
VARNAME=value
The above is the value of value is assigned to VARNAME, pay attention to both sides of the equal sign can not have spaces , otherwise it will be interpreted as Shell commands and command-line parameters.

Export the local variable is an environment variable

After you define a variable exists only in the current Shell process, it is a local variable with the export command to export the local variable environment variables, define and export an environment variable can usually be completed step:
export VARNAME=value
can be done in two steps:
VARNAME=value
export VARNAME
@experience: scenarios is also very much like you to write a run every day to run the task we generally known as batch job, then the result of which run a script may be called multiple downstream tasks used (downstream after a task is completed script will pull b script, the script is a script this time b downstream tasks), in which case we can choose Wonderland variable to the value programmed to run a batch environment, of course, this environment is relatively independent of other machines, many manufacturers this framework will be designed to run batches, syntax check it out here just fine.

Delete environment variables or local variables defined.

unset VARNAME
If a variable named VARNAME, use \${VARNAME}may represent its value, without ambiguity may be used \$VARNAMErepresent its value. I generally recommend using the first way with {} be said that because of the complexity of doping sql shell script is not easy to see, what you write is to let people understand, it is recommended that individuals first. Comparison of these two different notation by the following example:
echo $SHELL
Note that when you define your variables $, take the time to use $ variable value . And C language is different, Shell variables do not need to explicitly define the type, in fact, values are strings Shell variables, such as we define VAR = 45, in fact, the value of VAR is the string 45 rather than integers. Shell variables do not need to use the first definition, if there is no definition of a variable value, the value is an empty string. If you used the python this place is very easy to understand.

Published 21 original articles · won praise 19 · views 1859

Guess you like

Origin blog.csdn.net/weixin_43071838/article/details/104510764