Zsh Development Guide (Chapter 11 advanced content of variables)

Time:2021-11-13

Reading guide

We have talked about the basic usage of the five variables under Zsh (string, array, hash table, integer and floating point number). However, there are some advanced contents in the use of variables, which is very helpful for some special scenarios.

Typeset command

The typeset command is used to set variables in detail. We’ve seen it before in the hash table. Typeset – a can be used to define hash tables.

% typeset -A hashmap=(aa bb cc dd)

However, we all use local in the future, because the function of local is the same as HashMap (except – F and – G, which are not commonly used), and it is shorter and easier to enter. The typeset command is mentioned here because the name well reflects its functionality. But after knowing this, we can continue to use the local command. After all, they are the same.

The typeset command has many options, which can act on variables and have various effects.

Force string content to be lowercase or uppercase

#Force string contents to be lowercase
% local -l str=abcABC && echo $str
abcabc

#Force string content to uppercase
% local -u str=abcABC && echo $str
ABCABC

Set variable as environment variable

% local -x str=abc
#Export is usually used with the same functions
% export str=abc

Environment variables can be read by child processes.

Set variable to read-only

% local -r str1=abc
#Readonly is usually used, and the functions are the same
% readonly str2=abc

% str1=bcd
zsh: read-only variable: str1
% str2=bcd
zsh: read-only variable: str2

The set array does not contain duplicate elements

% local -U array=(aa bb aa cc) && echo $array
aa bb cc

Sets the number of digits of an integer

#If the number of bits is not enough, the output content will be completed with 0
% local -Z 3 i=5 && echo $i
005

#If out of range, it will be truncated
% local -Z 3 i=1234 && echo $i
234

Binary conversion

Set integer to other hexadecimal display:

% local -i 16 i=255
% echo $i
16#FF

You can set any base between 2 and 36. Setting the decimal display does not affect the calculation, but the display format is different.

Use [#n] num to display decimal numbers as n-ary:

% echo $(([#16] 255))
16#FF

You can use n#num to display n-ary integers in decimal:

% echo $((16#ff))
255

We can define a series of functions to convert Radix quickly without using external commands such as BC:

0x() {
    echo $((16#$1))
}

0o() {
    echo $((8#$1))
}

0b() {
    echo $((2#$1))
}

p16() {
    echo $(([#16] $1))
}

p8() {
    echo $(([#8] $1))
}

p2() {
    echo $(([#2] $1))
}


#Other decimal to decimal
% 0x ff
255
% 0b 1101
13

#Decimal to other decimal
% p16 1234
16#4D2

Assign the same value to multiple variables at the same time

% local {i,j,k}=123
% echo $i $j $k
123 123 123

Binding strings and arrays

% local -T DIR dir
% dir=(/a /b/c /b/d /e/f)
% echo $DIR
/a:/b/c:/b/d:/e/f

#When dir is deleted, dir is also deleted (and vice versa)
% unset dir
% echo $+DIR
0

Strings with separators and colons (such as $path) often need to be processed under Linux. It is troublesome to modify only one of the fields. Local – t can bind the string to the array, so that the array can be modified directly, and the string content will change synchronously (and vice versa). In fact, in Zsh, $path string is bound to the $path array. You can modify $path directly by modifying $path, which is much more convenient in some scenarios.

Displays how variables are defined

% array=(aa bb cc)
% local -p array
typeset -a array=(aa bb cc)

% array+=(dd)
% local -p array
typeset -a array=(aa bb cc dd)

Where should I put double quotes

Readers who have used bash will probably be impressed by the double quotation marks inside. Many places will make mistakes without double quotation marks. In order to avoid mistakes, many people add double quotation marks around each variable. If you don’t mind, the code looks messy.

In fact, there are no problems in Zsh. There is no need to add double quotation marks around variables, and there will be no inexplicable errors. But some places still need double quotation marks.

Scenes requiring double quotation marks:

  1. A string like this that contains characters or special symbols"aa bb \t \n *"When it appears in the code, double quotation marks should be added around it. This basically does not need to be explained.
  2. In use$()When calling the command, if you want the result to be processed as a string, you need to add double quotes,"$()"Otherwise, if there are spaces in the command result,$()Will be expanded into multiple strings.
  3. If you want to treat an array as a single string, you need to use double quotes,array=(a b); print -l "$array"
  4. For other things that are not a single string and need to be converted into a single string, double quotation marks should be added.

In other cases, double quotation marks are not required. Typical cases:

  1. In any case, double quotation marks are not required on both sides of string variables. It doesn’t matter how special the contents are or whether the variables exist, such as$str
  2. If you do not convert types (such as an array to a string), you do not need double quotes around any variable.
  3. $1 $2 $*These parameters (in fact, they are all single strings) do not need double quotes, no matter what the content is or whether the parameters exist.

The above seven scenarios cover almost all scenarios. If there is one that is not covered, try it (let the contents include spaces, line breaks and other special characters to see whether the results meet the expectations).

summary

This article briefly introduces the usage of some typeset (or local) commands. The typeset command has many other parameters, but it is rarely used. I will continue to update it in the future.

reference resources

http://www.bash2zsh.com/zsh_r…

http://www.linux-mag.com/id/1…

Update history

20170831: add “where should double quotation marks be added”

This article will not be updated. The full series of articles will be updated and maintained here:github.com/goreliu/zshguide

Pay to solve problems related to windows, Linux, shell, C, C + +, AHK, python, JavaScript, Lua and other fields, flexible pricing, welcome to consult, wechat ly50247.