Bash variable types and bash Variables

Source: Internet
Author: User

Bash variable types and bash Variables

Local variable: it acts on the Current shell and is invalid for other shell processes other than the current shell and the Current shell Sub-processes.

Assign a value to a local variable

name='value'

Value can be a string or a variable. The referenced variable uses $ {name}, $ name, or ''.

Note: single quotes are strongly referenced, while double quotes are weak references.

[root@izwz9bqngpatc008nw00e7z tmp]# echo '$animal'$animal[root@izwz9bqngpatc008nw00e7z tmp]# echo "$animal"dog[root@izwz9bqngpatc008nw00e7z tmp]# echo "$animals.".[root@izwz9bqngpatc008nw00e7z tmp]# echo "${animal}s."dogs.
[root@izwz9bqngpatc008nw00e7z tmp]# myid = `id -u`-bash: myid: command not found[root@izwz9bqngpatc008nw00e7z tmp]# myid=`id -u`[root@izwz9bqngpatc008nw00e7z tmp]# echo '$myid'$myid[root@izwz9bqngpatc008nw00e7z tmp]# echo "$myid"0
[root@izwz9bqngpatc008nw00e7z tmp]# filename=`date +%F-%H-%M-%S`[root@izwz9bqngpatc008nw00e7z tmp]# echo "$filename"2018-03-19-18-41-21

Use set to display all Defined variables, and unset name to destroy the variables.

Local variable: takes effect in a code segment of the shell process.

Environment variable: takes effect in the current shell process and its sub-process.

The declaration method is

export name=valuedeclare -x name=value
[root@izwz9bqngpatc008nw00e7z tmp]# export TES='animal'[root@izwz9bqngpatc008nw00e7z tmp]# echo $TESanimal[root@izwz9bqngpatc008nw00e7z tmp]# declare -x myname='jeffd'[root@izwz9bqngpatc008nw00e7z tmp]# echo $mynamejeffd

Display All environment variables: export, env, and printenv.

Destroy environment variable: unset name.

Do not use bash built-in environment variables.

Read-Only variables:

readonly namedeclare -r name

Location variable: The parameter used for the script to call the parameter passed to it through the command line in the script code.

[root@izwz9bqngpatc008nw00e7z tmp]# cat test.sh #!/bin/bashlinecount="$(wc -l $1|cut -d' ' -f1)"echo "$1 has $linecount lines."[root@izwz9bqngpatc008nw00e7z tmp]# chmod +x test.sh [root@izwz9bqngpatc008nw00e7z tmp]# ./test.sh /etc/passwd/etc/passwd has 25 lines.

Special variable: $? The Return Value of the previous command execution status.

Program execution may return two types of values:
1. program execution results
2. Is the program status returned (0-255)
0 indicates that the execution is correct.
1-255 execution error (127, system reserved );

$ #: Get the total number of parameters in the Current shell Command Line
$ *: Get all parameters of the current shell "$1 $2 $3 ..., Controlled by IFS
$ @: All parameters of this program "$1" "$2" "$3" "…", Not controlled by IFS
$0 get the name of the currently executed shell script
$ N obtain the nth parameter value of the currently executed shell script, n = 1 .. 9
$ Obtain the current shell process ID (PID)
$! PID used to execute the previous command

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.