Linux Learning Diary--Basic commands (7) Variables in--bash, configuration files

Source: Internet
Author: User

Definition and use of variables

Variable type

Strongly typed: You must specify a type when defining a variable, the participating operation must conform to the type requirement, and calling an undeclared variable produces an error;

Weak type: No need to specify type, default is character type, participate in operation will automatically do implicit type conversion, the variable can be called directly without prior definition;

Variable naming laws:
1. The reserved words in the program cannot be made: for example if, for;
2, can only use numbers, letters and underscores, and can not start with a number;
3. See the meaning of the name,


Types of variables in bash:
Based on criteria such as the scope of the variable's entry:
Local variable: The active scope is the current shell process and is not valid for Shell processes other than the current shell, including the current Shell's child shell process;
Environment variables: The effective scope is the current shell process and its child processes;
Local variables: The effective range is the current shell process in a code fragment (usually refers to a function);
Positional variables: $, $, ... To indicate that the script is used to invoke parameters passed to it through the command line in the script code;

Special variables: $?, $, $*, [email protected], $#


Local Variables:
Variable assignment: name= ' value '
You can use references:
Value
(1) can be a direct string; Name= "username"
(2) Variable reference: name= "$username"
(3) Order reference: Name= ' Command ', name=$ (command)
Variable reference: ${name}, $name
"": a weak reference in which the variable reference is replaced with the value of the variable;
": Strong reference, where the variable reference is not replaced with the value of the variable, and the original string is persisted;
Show all variables that have been defined
Set
Destroying variables:

unset name

Environment variables:
Variable declaration, assignment:
Export Name=value
Declare-x Name=value
Variable reference: $name, ${name}
Show all environment variables:
Export
Env
Printenv
Destroyed:
unset name

Bash has many built-in environment variables: PATH, SHELL, UID, Histsize, HOME, PWD, old, Histfile, PS1

Read-only variables:
ReadOnly Name
Declare-r Name

Positional variables:
Calling parameters passed to the script through the command line in the script code;
$, $, ... : Corresponding call 1th, 2nd and other parameters;

Shift [N]

$: the order itself;

$*: All parameters passed to the script;
[Email protected]: All parameters passed to the script;
$#: The number of arguments passed to the script;

Example: Determining the number of rows for a given file
#!/bin/bash
Linecount= "$ (wc-l $1| cut-d"-f1) "
echo "has $linecount lines."

Bash configuration file:

In terms of effective scope, there are two categories:
Global configuration:
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
Personal configuration:
~/.bash_profile
~/.bashrc

by function, there are two categories:
Profile class: Provides configuration for the interactive logon shell
Global:/etc/profile,/etc/profile.d/*.sh
Personal: ~/.bash_profile
Function:
(1) for defining environment variables;
(2) run the command or script;

BASHRC class: Provides configuration for a non-interactive logon shell
Global:/ETC/BASHRC
Personal: ~/.BASHRC

Function:
(1) Define command aliases;
(2) define local variables;

Shell Login:
Interactive login:
Enter the account password directly via the terminal;
Users switching with "Su-username" or "Su-l UserName"

Loading process
/etc/profile---/etc/profile.d/*.sh------~/.BASHRC----and/ETC/BASHRC//late loading will overwrite the same variables that were loaded first

Non-interactive logon:
Su UserName
The terminal opened under the graphical interface
Execute script

Loading process

~/.BASHRC--/ETC/BASHRC-/etc/profile.d/*.sh


Edit the new configuration defined by the profile to take effect:
(1) Restart the shell process;
(2) using the source or. Command process


Linux Learning Diary--Basic commands (7) Variables in--bash, configuration files

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.