Linux-bash variable, bash script

Source: Internet
Author: User


Bash Variable type:
Environment variables
Local variables (local variables)
Positional variables
Special variables

Local variables:
Set Varname=value: scope for the entire bash process;

Local variables:
Local Varname=value: Scope is the current code snippet;


Environment variable: The scope is the current shell process and its child processes, (the action variable itself uses VARNAME, the value of the action variable is only $varname)
Export Varname=value
Varname=value
Export VARNAME
Export


[Email protected] ~]# export name
[Email protected] ~]# bash
[Email protected] ~]# echo $name
Cxiong
[Email protected] ~]# bash
[Email protected] ~]# echo $name
Cxiong


Reference variable: ${varname}, parentheses can sometimes be omitted

[Email protected] ~]# Name=cxiong
[Email protected] ~]# echo $name
Cxiong
[Email protected] ~]#

[Email protected] ~]# echo "My name is $name"
My name is Cxiong
[Email protected] ~]#



Positional variables:
$, $, ...


Special variables:
$?: The execution status return value of the previous command;

[[email protected] ~]# echo $?
0
[Email protected] ~]# Ll/varr
Ls:cannot access/varr:no such file or directory
[[email protected] ~]# echo $?
2
[Email protected] ~]#


program execution, there may be two types of return values:
Program execution Results
Program Status return codes (0-255)
0: Correct execution
1-255: Error execution, 1,2,127 system reservation;

Output redirection:
>
>>
2>
2>>
&>



Undo Variable:
Unset VARNAME

[Email protected] ~]# unset name
[Email protected] ~]# echo $name


[Email protected] ~]#



To view variables in the shell:
Set

[[Email protected] ~]# set
Bash=/bin/bash
Bashopts=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_ Shell:progcomp:promptvars:sourcepath
Bash_aliases= ()
Bash_argc= ()
bash_argv= ()
Bash_cmds= ()



To view environment variables in the current shell:
Printenv
Env
Export


Script: Command stack, according to the actual needs, combined with the command flow control mechanism to implement the source program


Shebang: Magic number
#!/bin/bash
# comment lines, do not execute

The kernel can only perform elf types

[Email protected] ~]# Nano first.sh
[email protected] ~]# ll first.sh

[Email protected] ~]# chmod +x first.sh
[Email protected] ~]# pwd

[Email protected] ~]# first.sh


#
#/etc/fstab
# Created by Anaconda on Sun June 21 02:15:00 2015
#
# Accessible filesystems, by reference, is maintained under '/dev/disk '
# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info
#



/dev/null: Software device, bit bucket, data black hole

$?

[[email protected] ~]# ID student &>/dev/null
[[email protected] ~]# echo $?
1
[[email protected] ~]# ID rhel &>/dev/null
[[email protected] ~]# echo $?
0
[Email protected] ~]#



The script starts a child shell process when it executes;
Scripts that are started on the command line inherit the current shell environment variable;
Scripts that are automatically executed by the system (non-command-line startup) require a self-defined environment variable;


Linux-bash variable, bash script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.