Discussion on variable operation methods in shell programming bash

Source: Internet
Author: User
Tags integer numbers

1. Conditional Variable replacement:

BASH Shell can replace the conditions of a variable. It can be replaced only when a condition occurs.

Put the condition in.

(1) $ {value:-word}

If the variable is undefined or the value is null, the return value is the content of the word; otherwise, the value of the variable is returned.

(2) $ {value: = word}

Similar to the former, if the variable is undefined or the value is null

Word value to value

(3) $ {value :? Message}

If the variable is assigned a value, it will be replaced normally. Otherwise, the message will be sent to the standard error output (if

If this replacement occurs in the shell program, the program stops running)

(4) $ {value: + word}

If a variable is assigned a value, the value is replaced by word. Otherwise, no replacement is performed.

(5) $ {value: Offset}

$ {Value: offset: length}

Extract substrings from variables. Here, offset and length can be arithmetic expressions.

(6) $ {# value}

Number of Characters in the variable (the number of characters in the variable, not the number of variables)

(7) $ {value # pattern}

$ {Value ## pattern}

Remove the part of value that matches pattern. The condition is that the beginning of value matches pattern.

The difference between # And # Is that one is the shortest matching mode and the other is the longest matching mode.

(_Cool.gif src = "/uploadfiles/newsphoto/con_cool.gif" border = 0 >$ {Value % pattern}

$ {Value % pattern}

Similar to (7), except that the value is matched from the end of pattern. The difference between % and % is the same as that between # And #.

(9) $ {value/pattern/string}

$ {Value // pattern/string}

Replace the variable content, and replace the part that matches pattern with the string content.

Do not be the same as above

Note: Except (2) in the replacement of the above conditional variables, other values of the variables themselves are not affected.

2. arithmetic operations on variables

In bash shell, only two integers can be computed, and the result is still an integer.

Operation, you need to use the let Command, Syntax:

Let expr

Expr is an expression that contains items and operators. An item can be a variable or an integer constant,

When an integer constant is used, the default value is a decimal integer. You can use radio # number to specify other

In this example, radio defines the number of hexadecimal integers, and number indicates the value of this integer.

Radio> 10, then number characters can be from 0-9 and A-Z.

Operators supported in expressions and their meanings are:

+,-, *,/, % Plus, subtraction, multiplication, division, modulo

>>,<<,&, ^, | Shifts left, right, bitwise AND, bitwise OR, bitwise OR

? : Ternary operator. Consistent with the definition in C Language

~ CAPTCHA

!,> =, <=,>, <, = ,! =, &, |

=, + =,-=, * =,/=, % =, <=, >>=, & =, ^ =, | =

Brackets can also be used in expressions. brackets or definitions of Operation priority and

Same.

The let Command has a return value. When the calculation result (if there are multiple expressions, the last one prevails) is 0,

The return value is 1, otherwise it is 0.

When the expression contains special shell characters (such as |), you must use a quote ('or ") to reference it.

When using let, the shell calculates the value of X + Y for a formula such as Let X + Y.

However, the result is discarded. If you do not want to do this, you can use let sum = x + y to save the result of X + Y in the sum variable.

In addition, you can use the (and) operator to replace the let Command.

Expression reference. If you want to return the expression value, you must use the format of $.

In the if condition, "=" is used to compare strings; "-EQ" is used to compare integer numbers.

Bash built-in Parameters

==============

Ppid: The process ID of the bash caller.

PWD: current working directory.

Oldpwd: the previous working directory.

Reply: When the READ command does not have a parameter, it is directly set to reply.

UID: User ID.

EUID: valid user ID.

Bash: the complete path of bash.

Bash_version: Bash version.

Shlvl: Add a number to each bash execution.

Random: Every time this parameter is used, a random number is generated on the random.

Seconds: the time after the shell is started.

Lineno: number of lines in the script.

Histcmd: Number of historical records.

Optarg: the last option parameter for getopts processing.

Optind: The next parameter number to be processed by getopts.

Hosttype: machine type.

Ostype: name of the job system.

Ifs: internal field separator.

Path: command search path.

Path = "/usr/GNU/bin:/usr/local/bin:/usr/UCB:/bin:/usr/bin :."

Home: current user's home directory;

Cdpath: The Search Path of the CD command.

Env: If this parameter is set, the file name set by this parameter will be executed every time a shell script is executed as the Environment setting.

Mail: If this parameter is set and mailpath is not set, Bash will notify the user when a letter comes in.

Mailcheck: set the time to check an email.

Mailpath: a string of email check paths.

Mail_warning: If the email is read, a message is displayed.

PS1: Set the prompt message to "bash $ ". (For details, see the prompt message section .)

PS2: the second message is set to "> ".

PS3: the prompt message used by the SELECT command.

PS4: Set the prompt message used for tracking, which is set to "+ ".

Histsize: indicates the number of command history records, which is set to 500.

Histfile: history file. Set it internally ~ /. Bash_history.

Histfilesize: Maximum number of historical records, which is set to 500.

Opterr: if it is set to 1, Bash will display the getopts error.

Prompt_command: If set, this value is displayed before each command is executed.

Ignoreeof: Use the EOF value as the input and set it to 10.

Tmout: if it is set to greater than zero, the value is interpreted as the number of seconds to wait. If there is no input, it is treated as no input.

Fcedit: The final editor of the FC command.

Fignore: For details, see Readline.

Inputrc: Startup File of Readline, set internally ~ /. Inputrc

Notify: If set, bash immediately reports the terminated background program.

History_control, histcontrol: history.

Command_oriented_history: stores multiple-line commands.

Glob_dot_filenames: if it is set, Bash will include "." into the file path.

Allow_null_glob_expansion: If set, bash allows the path to be called Null String.

Histchars: used by history.

Nolinks: If set, the symbolic links will not be followed when the command is executed.

Hostname_completion_file, hostfile: file name in the same format as/etc/hosts.

Noclobber: If set, Bash will not overwrite any files operated by ">", "> &", and "<>.

Auto_resume: see task control.

No_exit_on_failed_exec: if this value exists, non-interactive shell will not jump out because of exec failure.

Cdable_vars: If the directory cannot be found by the CD command, you can switch to the directory specified by the parameter format.

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.