SHELL programming basics (b)

  

SHELL programming basics (b)

Write a simple program

#this is sample // comment

echo "hello,welcome to Fedora 6"  

echo "Logged in $USER."  

env|grep PATH

echo $PATH

 

1 , variable

 

1 ) user variables

 

VAL=val

Such as

St?nt=tom

echo St?nt

echo $St?nt

Variable reference symbol "$"

Note: Single quotes and backslashes can prevent the value of the variables used. Double quotes does not prevent variable substitution, but you can turn off the special meaning of most of the other characters.

When using the variable as a parameter when executing a command, shell will use the value of the variable instead of the variable name, if the value of the variable contain special characters such as *,?, Etc., will be variable expansion. If "" quotes, you can prevent bash to expand variables were pathname expansion.

Delete variables unset $ St? Nt.

2 ) Variable Attributes

 

A , read-only, Readonly

 

bdeclare,typest

 

-a  array variable

 

-f  function name variable

 

-i  integer variable

 

-r  Read-Only

 

-x  output variables

 

By default, the value of the variable is stored as a string.

 

3 ) global (environment) variable

 

Global variables in all capital letters.

System variables can be declared and initialized by inheritance, it can be declared and initialized at shell startup. Values ​​for these variables can be specified by command line or in the initialization file. For those who could not help shell variable automatic output, the user must use the export, setenv so that these variables can also be quilt shell access.

HOME / ~ home directory

PATH shell command search directory

About how to set the PATH, you can see:

Fedora environment variables set

http://blog.163.com/zhoumhan_0351/blog/static/399542272010521114234850/

 

MAIL user mail

PS1 first prompt, the symbol may be used as follows:

\: If a line can not finish the command input \ input command to continue not finish called continuation lines 
\ !: display change command history number 
\ #: SHELL display after activation of the current command history number 
\ $: Displays the current user variable value, if the current user is root # is displayed to other users on the show $ 
\\: display a backslash 
\ d: display a current date 
\ h: the host name of the computer running the display of SHELL 
\ n: print a newline Fu led to prompt new line with C language syntax is similar to 
\ s: displays the current SHELL name 
\ t: displays the current time 
\ u: displays the current user name 
\ W: display the current working directory reference name 
\ w: displays the current working directory 

Current time \ @ according to 12-hour, AM / PM display format

\! No current events

Commonly used system variables:

BASH_ENV

CDPATH

COLUMNS

fcede

HOME

IFS

PS1

PS2

PWD

UID

4 ) the position variables and special variables

 

A set to set

 

#this is sample  

if [$# -eq 0 ]  

then  

echo "not provide name"  

else  

echo "your name is "$1  

be  

5 ) array variable

 

definition:

name=(ele1 ele2 ele3 ...)

$aYear=(Spring Summer Autumn Winter)

declare -a aYear='([0]="Spring" [1]="summer")'  

A third reference value in the array elements:

echo $(aYear[2])

Use subscript [*], [@] can extract the entire array elements, but with a different meaning than when the quotes, @ copy the contents of the original array to a new array, the array generated same as the original, and the symbol * is the original array containing all of the elements copied as a new element to the array, the array generated only one element.

reference

[1] PS1 provided Related http://dong926.blog.51cto.com/386907/179816/

[2] LINUX some common commands and BASH basic variables

Published 81 original articles · won praise 17 · views 6038

Guess you like

Origin blog.csdn.net/hopegrace/article/details/103758108