Shell Primer Basics

Source: Internet
Author: User

First, shell variables

1. Variables can be divided into two categories: environment variables (global variables) and local variables.

Environment variables: All environment variables are system global variables that can be used in all child processes, including editors, shell scripts, and various applications.

Environment variables can be set at the command line, but they are also lost when the user exits, so it is best to use the. bash_profile file in the user's home directory or the global configuration

Defined in/ETC/BASHRC,/etc/profile files, or/etc/profile.d/. The environment variables are placed in the profile file and are initialized each time the user logs on.

All environment variables are uppercase.

Before the environment variable is applied to the user process, the Export command or source or "." Must be used Command Export.

See some Linux common system environment variables through Baidu.

Common environment variables:

(1) Modify history command to display historical command record number: Histfilesize=5

Histsize=5

(2) To view the type of system file: $HOSTTYPE

(3) View system hostname: $HOSTNAME

Local variables:

Define variables: (1) Variable name =value

(2) Variable name = ' value ', using the contents of single quotes is what is, inside if there is a variable is not parsed.

(3) Variable name = "value", using double quotation marks in the content, if there are variables will parse the variable into the content to display

Exam: a = 192.168.1.2

b = ' 192.168.1.2 '

c = "192.168.1.2"

A = 192.168.1.2-$a

b = ' 192.168.1.2-$a '

c = "192.168.1.2-$a"

echo "a = $a"

echo "B = $b"

echo "C = ${c}"

Note: $a is the same effect as ${c}

Answer: a = 192.168.1.2-192.168.1.2

b = 192.168.1.2-$a

c = 192.168.1.2-192.168.1.2-192.168.1.2

There are two ways to use variables as content:

(1) cmd = ' date +%F '

(2) cmd = $ (date +%F)

When do I use the ${var} with this curly brace scenario?

such as: TAR-XZVF fcz_$pwd_log.tar.gz, the system does not know whether the PWD as a variable or pwd_log as a variable

At this point, if you use curly braces TAR-XZVF fcz_${pwd}_log.tar.gz explicitly tells the system that PWD is a variable, and pwd_log is not a variable.

      

Shell Primer Basics

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.