First, the variable name
1, can only contain letters, numbers and underscores, and cannot start with a number,
2. Should not be the same as the existing environment variables in the system
3, the name to have the meaning
Ii. Assigning values to variables
Variable name = value Var_name=value equal to no space on either side
[email protected] ~]# Name=Jerry~]#echo $NAMEjerry
Third, bash: variable type
1. Environment variables
The script starts a child shell process when it executes. The script that is started on the command line inherits the current shell environment variable, which means that the scope of the environment variable is the current shell process and its child shell process. Scripts that are automatically executed by the system (not command-line startup) require self-defined environment variables. You can set environment variables using export.
Export Varname=value or Varname=value export VARNAME
[email protected] ~]#Name=Jerry[[email protected] ~ ]# export NAME[[email protected] ~]# Echo$NAMEjerry[[email protected] ~ ]# bash[[email protected] ~]# Echo$NAMEjerry[[email protected] ~ ]# bash [[email protected]~]#Echo$NAMEjerry
[email protected] ~]# Pstree
Init-+-networkmanager-+-dhclient
...
|-SSHD---sshd---sshd---bash---su---bash ---bash---bash---pstree
|-UDEVD---2*[UDEVD]
'-wpa_supplican
2. Local variables (local variables)
Variables must be variables of the process, so variables declared in one shell are not available in other shells and are not available in their child shells. The reference variable should be added ${varname}, or {} may not be added.
[email protected] ~]# Name=Jerry[[email protected] ~]# Echo$NAMEjerry[[email protected] ~ ]# bash[[email protected] ~]# Echo$NAME[[email protected] ~ ]# exitexit[[email protected] ~]# Echo$NAMEjerry
Local variables: varname=value, scope: The entire bash process.
Locals: local varname=value, scope: The current code snippet.
3. Position variable
$n
4. Special variables (System variables)
To save some special data:
$?: saves the execution status return value of the previous command (0-255). 0 is correct, others are errors. Where the 1,2,127 system is reserved, others can be customized.
[email protected] ~]# ls/Varaccount crash db games lib lock mail opt run tmp ypcache CVS empty GDM local log NIS preserve spool www[[email protected] ~]# Echo$?0 [email protected] ~]# ls/Varrls: Cannot access/varr:no suchfileor directory[[email protected] ~]# Echo$?2
If you want to see only the returned status values and don't want to see the output, simply redirect the output to the data black hole/dev/null
[email protected] ~]# ls /varr &>/dev/null[[email protected] ~]#echo $? 2
Iv. Undoing variables
[email protected] ~]#unset NAME ~]#echo $NAME
v. View current shell variables View all variables of the current shell, including environment variables and local variables
[email protected] ~]#setbash=/bin/bashbashopts=checkwinsize:cmdhist: Expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars: Sourcepathbash_aliases=() ...
View current shell environment variables printenv env or export
[email protected] ~]#printenvhostname=Haoshell=/bin/bashterm =xterm ...
Vi. Append Shell variables
[email protected] ~]# Animal=Pig[[email protected] ~]# Animal=$ANIMAL: Goat[[email protected] ~]# Echo$ANIMALpig: Goat[[email protected] ~]# Echo$PATH/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin[[email protected] ~]# Export path= $PATH:/usr/local/apache/bin[[email protected] ~]# Echo$PATH/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin[[email protected] ~]#Export path=/usr/local/mysql/bin: $PATH[[email protected] ~]# Echo$PATH/usr/local/mysql/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin
Seven, the first script program
First create the script first.sh, the first line of the script to specify the magic number of the program, #! Add the path to the interpreter. The execution permission is then given, followed by execution, and the path is added because, when the path is not given, the default goes to path to find out if the command exists.
[email protected] ~]# CatFirst.SH#!/bin/Bashls/varCat/etc/inittab[[email protected] ~]# chmod+x first.SH [email protected] ~]# ./first.SHAccount crash db Games lib lock mail opt run tmp ypcache CVS empty GDM local lo G NIS Preserve spool www# inittab is only used by upstart forThe default runlevel.## ADDING Other CONFIGURATION here would have NO EFFECT on YOUR system.## SYSTEM initialization i S started by/etc/init/rcs.conf## Individual runlevels is started by/etc/init/rc.conf## Ctrl-alt-delete is handled by/etc/init/control-alt-delete.conf## Terminal Gettys is handled by/etc/init/tty.conf and/etc/init/serial.conf,# with configurationinch/etc/sysconfig/init.## For information aboutWriteupstart event handlers, or how# upstart works, see Init (5), Init (8), and Initctl (8). # # Default RunLevel. The runlevels used are:#0-Halt (do not set Initdefault to this) #1-Single user mode#2-Multiuser, without NFS (the same as3,ifYou DoNot having networking) #3-Full multiuser mode#4-unused#5-x11#6-reboot (do not set Initdefault to this) #ID:5: Initdefault:
"Brother's Linux Private Dishes" study notes (8)--bash script programming variables