Shell Scripting Learning Summary--variables and environment variables

Source: Internet
Author: User

All variables in bash are stored as strings, and the ENV command can view the environment variables associated with this terminal process.

View environment variables for a process

1. Get the PID of the program, such as Java
# pgrep Java
23492
2. View the corresponding environment variables
# Cat/proc/23492/environ

3. Format the output so that the view will be clearer than
# Cat/proc/23492/environ |tr ' \ n '
"^@" will be transformed into a newline character.


Assigning values to variables
Var=varlue, if value does not contain white space characters, you do not need to add single or double quotation marks
var = value is an equal operation, note the distinction that many people this place is confusing


Output variables
echo $var or Echo ${var}
For example:

var="${var} (s)"Apple (s)


Get the character length of a variable
echo ${#var}



Special variables

$PS 1 system default: ps1= "[\[email protected]\h \w]\$"
$UID for root user 0 o'clock

$TMOUT Terminal Timeout time

$0,$*, [email protected], $#

[Email protected] sbin]#Cat/usr/local/sbin/test.SH #!/bin/BashEcho '$0='$0 #脚本的名称Echo '$1='$1 #运行脚本的第n个参数Echo '$#='$# #运行脚本参数的个数Echo '$*='$* #所有参数变成一个字符Echo '[Email protected]='[email protected] #所有参数basename$0 #获取文件名dirname$0 #获取文件的目录[email protected] sbin]#SH/usr/local/sbin/test.SHAA bb$0=/usr/local/sbin/test.SH$1=aa$#=2$*=AA Bb[email protected]=AA bbtest.SH/usr/local/sbin

Process state variables

$? Gets the return value that executes the last command (0 is successful, others are failed)

$$ getting the PID of the current shell

$_ gets the last parameter of the previous command or script

!$ executing the previous command

Shell Scripting Learning Summary--variables and environment variables

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.