takeaway

Alias is very common in shell. It is mainly used to alias commands to simplify typing. However, it is mainly used in interactive scenarios and is rarely used in scripts. Eval is a very powerful command that parses strings into code for execution, but it also adds a lot of additional complexity, and unnecessary scenarios should be minimized. Alias and eval may seem unrelated, but they are similar in functionality, so they are used together.

alias

The most typical example is to simplify ls -l to ll:

% alias ll='ls -l'
% ll
total 0
drwx------ 0 goreliu goreliu 512 Aug 31 13:55 tmux-1000
drwxr-xr-x 0 goreliu goreliu 512 Aug 31 13:37 yaourt-tmp-goreliuCopy the code

The alias effect is equivalent to simply replacing the string, which is easier to understand.

Running aliases directly will list all aliases
% alias
ll='ls -l'
lla='ls -F --color --time-style=long-iso -lA'.Copy the code

Such aliases are resolved only if they appear at the beginning of the line. But there is a more powerful global alias in ZSH that can be resolved without the start of a line:

% alias -g G='| grep'

% ls G tmux
tmux-1000Copy the code

But I need to be careful about the possible side effects, such as if I wanted to create a file called G:

% touch G
touch: missing file operand
Try 'touch --help' for more information.
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.Copy the code

The result is that the G is replaced, and you can only put quotation marks around the G.

If a global alias isn’t working well, it can have disastrous consequences, such as deleting important files (like passing a global alias to RM that deleted files from the alias string), so you need to weigh it before using it, and use it with care.

eval

The function of eval is to execute strings as code. It seems to be simple, but in fact it involves very complex content, mainly semantic problems caused by symbol escape.

An important use of eval in bash is to take the value of a variable as its name and then take its value, similar to the c pointer to a variable:

% str1=str2
% str2=abc
% eval echo\ $$str1
abcCopy the code

Note that there is a \ and two $, because the second $takes str1 as usual, while the first $needs to be escaped because it takes str2 during eval execution and cannot be expanded now.

This usage is very problematic and very unreadable. Fortunately, ZSH doesn’t have to do this. There’s a better way:

% str1=str2
% str2=abc
% echo ${(P)str1}
abcCopy the code

(P) for this scenario, no need to escape $.

Eval can also be used to execute code dynamically, for example when a script accepts input from a user that is also a piece of script code. But this is extremely dangerous, because the script can have all kinds of dangerous operations, and the shell syntax is so flexible that it is difficult to detect dangerous operations through static scanning. Unreliable code should never be run. Even if you must run it, you can write it to a file before running it, so that the passed code does not affect your own logic.

This is not to say that eval is completely unnecessary in ZSH; it can be useful in special situations, such as syntax modification and syntax sugar. But if you want to use it, you must be aware of the possible side effects, the pros and cons have to be weighed. Bash eval is similar to bash eval. You can search for bash eval on the Web.

conclusion

This article briefly introduces the use of alias and the use of eval scenarios. Alias is simple and is used primarily in.zshrc. Eval is complex, and unnecessary scenarios should be avoided.

Full article address: github.com/goreliu/zsh…

Pay to solve Windows, Linux, Shell, C, C++, AHK, Python, JavaScript, Lua and other fields related problems, flexible pricing, welcome to consult, wechat LY50247.