Linux shell programming study notes 31: alias and unalias operations command aliases

Table of contents

  1. 0 Preface
  2. 1 Define alias
  3. 2 View aliases
    1. 2.1 View all aliases
    2. 2.2 View an alias
      1. 2.2.1 alias alias
      2. 2.2.2 alias | grep alias string
      3. 2.2.3 Use Ctrl+Alt+E key combination
  4. 3 unalias: delete alias
  5. 4 How to execute the command itself instead of the alias
    1. 4.1 Method 1: Use Ctrl+Alt+E key combination && unalias
    2. 4.2 Method 2: Add the absolute path of the command file before the command
    3. 4.3 Method 3: Add relative path before command
    4. 4.4 Method 4: Add a backslash before the command\
  6. 5 How to make an alias permanent
  7. 6 Why is it recommended to use single quotes?
  8. 7 References

0 Preface

Remember that there is a doskey command in DOS. You can use the doskey command to define macros. The execution priority of macros is the highest, higher than the built-in commands of DOS (the command execution priorities in DOS from high to low are: macro commands , DOS internal commands, executable programs with com extension, executable programs with exe extension, batch programs with bat extension).

This means that if the macro you define has the same name as the built-in command of dos, such as dir, when you enter dir on the command line, the system will execute your macro first instead of the built-in command of dos. This provides us with the opportunity to modify the internal Ways to command functions.

So does Linux provide similar functions?

1 Define alias

In Linux, we use the alias command to define command aliases. The format is as follows:

alias [command alias[=value]]

Similar to defining variables, when defining command aliases, pay attention to:

  • There cannot be spaces before and after the equal sign (=), otherwise it will become a comparison and cause syntax errors.
  • If there are spaces or tabs in the value, the value must be enclosed in single quotes or double quotes (single quotes are recommended)

2 View aliases

2.1 View all aliases

To view all aliases, enter the alias command directly without any options or parameters:

purpleEndurer @ bash $alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
purpleEndurer @ bash $

There are currently 6 command aliases defined 

2.2 View an alias

There are several ways to view an alias.

2.2.1 alias alias

Example 2.2.1.1 To view the alias named abc, use the command: alias abc

purpleEndurer @ bash $alias abc
bash: alias: abc: not found

 

Since the alias named abc does not exist in the system, the system feedback is not found.

Example 2.2.1.2 To view the alias named ls, use the command: alias ls

purpleEndurer @ bash $alias ls
alias ls='ls --color=auto'

It can be seen that the ls command we use on the command line is actually the ls command with parameters.

This means that if some command options are frequently used but are not the default options for the command, we can define aliases so that we do not need to enter those options every time.

  • Note: alias view aliases do not support wildcards

For example, to find all aliases starting with l:

purpleEndurer @ bash $alias l*
bash: alias: l*: not found

2.2.2 alias | grep alias string

For example, we want to find all aliases containing ls:

purpleEndurer @ bash $alias | grep ls*
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
purpleEndurer @ bash $

Since grep supports regular expressions, we can also use regular expressions to get qualified command aliases.

Example 2.2.2.1 Get the command alias that starts with ls and can be followed by letters, numbers, underscores or Chinese characters:

purpleEndurer @ bash $ alias | grep "\bls\w*="
alias ls='ls --color=auto'

The string "\bls\w*=" in the command is a regular expression, where:

  1. \b matches the beginning or end of a word
  2. \w letters, numbers, underscores or Chinese characters

This way only one matching command alias is found.

Example 2.2.2.2 Get the command alias that starts with l and can be followed by letters, numbers, underscores or Chinese characters:

purpleEndurer @ bash $ alias | grep "\bl\w*="
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'

This finds two matching command aliases.

Example 2.2.2.3 Get the command alias that starts with l and can be followed by any character except a newline character:

purpleEndurer @ bash $ alias | grep "\bl.*="
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'

in the regular expression "\bl.*=" in the command

  • . represents any character except newline characters

2.2.3 Use Ctrl+Alt+E key combination

After entering a command in the terminal command line, press the Ctrl+Alt+E key combination. If an alias is set, the alias will automatically become the actual command.

Example 2.2.3.1 In the terminal command line, first enter the command alias to view all current command aliases, then enter the command ls, and then press Ctrl+Alt+E to see the actual What is the command?

purpleEnduer @ bash $ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
purpleEnduer @ bash $ ls --color=auto

 

We first enter alias on the command line to view all current command aliases. We can see that the actual command corresponding to the ls command alias is: 'ls --color=auto'

Then enter ls on the command line, and then press Ctrl+Alt+E. At this time, the command on the command line becomes ls --color=auto

Notice:

  • This shortcut key is not available in all terminals. The shortcut keys of some terminals will conflict with this shortcut key. In this case, this method will not work.

3 unalias: delete alias

Too many command aliases are defined, which often brings trouble instead of convenience. So we also need to master the method of deleting aliases.

To delete an alias, you can use the unalias command, whose command format is:

unalias [-a] alias 1 [alias 2 ...]

in:

  • The -a option removes all defined command aliases.

Example 3.1 Delete command alias egrep

purpleEndurer @ bash $ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
purpleEndurer @ bash $ unalias egrep
purpleEndurer @ bash $ alias
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
purpleEndurer @ bash $ 

We first use the alias command to view all current command aliases. You can see the command alias egrep

Then use the command unalias egrep to delete the command alias egrep

Use the alias command again to view all current command aliases. This time, the command alias egrep is no longer visible.

Example 3.2 Delete command aliases egrep, fgrep and grep

purpleEndurer @ bash $ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
purpleEndurer @ bash $ unalias egrep fgrep grep
purpleEndurer @ bash $ alias
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
purpleEndurer @ bash $ 

 

We first use the alias command to view all current command aliases. We can see the command aliases egrep, fgrep and grep.

Then use the command unalias egrep fgrep grep to delete the command aliases egrep, fgrep and grep

Use the alias command again to view all current command aliases. This time, the command aliases egrep, fgrep and grep are no longer visible.

Example 3.3 Delete all command aliases

purpleEndurer @ bash $ alias
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
purpleEndurer @ bash $ unalias -a
purpleEndurer @ bash $ alias
purpleEndurer @ bash $ 

We first use the alias command to view all current command aliases. We can see the command aliases l., ll and ls.

Then use the command unalias -a to delete all command aliases

Use the alias command again to view all current command aliases. This time, you cannot see any command aliases.

4 How to execute the command itself instead of the alias

Although aliases are useful, they also have drawbacks. For example, if the alias defined happens to have the same name as a command, the alias will always be executed in hell.

In this case, what if we want to execute the actual command instead of the alias? There are four ways to solve this problem:

4.1 Method 1: Use Ctrl+Alt+E key combination && unalias

After entering a command in the terminal command line, press the Ctrl+Alt+E key combination. If an alias is set, the alias will automatically become the actual command. If the actual command is inconsistent with what we want to execute, then, we You can use unalias to delete the alias definition, and then what we enter and execute is the actual command.

4.2 Method 2: Add the absolute path of the command file before the command

For example, the function of the pwd command is to display the current working directory.

When we use the command alias pwd='ls' to define the variable alias, enter pwd and press Enter to display is the result of executing the ls command.

If we want to continue executing the original pwd command at this time, we can add the absolute path of the file corresponding to the command before the pwd command, that is: /bin/pwd

purpleEnduer @sh /bin $ pwd
/bin
purpleEnduer @sh /bin $ alias pwd='ls'
purpleEnduer @sh /bin $ pwd
ash        dumpkmap    1inux64        ping6        sync
busybox        echo        ln        printeny    tar
busybox.suid    egrep        login        ps        touch
cat        false        ls        pwd        true
chgrp        fdflush        mkdir        rev        umount
chmod        fgrep        mknod        rm        uname
chown        getopt        mktemp        rmdir        uncompress 
cp        grep        more        rpm        usleep
cpio        gunzip        mount        sed        vi
date        gzip        mv        setarch        watch
dd        hostname    netstat        sh        zcat
df        ipcalc        nice        sleep
dmesg        kill        pidof        stty
dnsdomainame    1inux32        ping        su
purpleEnduer @sh /bin $ /bin/pwd
/bin

 

4.3 Method 3: Add relative path before command

In addition to adding absolute paths, you can also use relative paths.

For example:

Executing the pwd command shows that the current working directory is /home/tc

When we use the command alias pwd='ls' to define the variable alias, enter pwd and press Enter to display is the result of executing the ls command. Since /home/tc has no files and directories, the command will not display any results.

The file corresponding to the pwd command is located in /bin.

If you want to continue executing the original pwd command at this time, in addition to adding the absolute path:/bin/pwd

You can also add a relative path, that is: ../../bin/pwd 

purpleEnduer @sh ~ $ pwd
/home/tc
purpleEnduer @sh ~ $ alias pwd='ls'
purp leEnduer @sh  $ pwd
purp leEnduer @sh $ ../../bin/pwd 
/home/tc
purpleEnduer @sh $ /bin/pwd 
/home/tc

purple by Enduer @sh 

 Whether to use an absolute path or a relative path mainly depends on which one is shorter, so that you can lose some characters^_^

4.4 Method 4: Add a backslash before the command\

For example:

When the pwd command alias exists, if we want to execute the actual pwd command, we can use the command:\pwd

purpleEnduer @sh ~ $ pwd
/home/tc
purpleEnduer @sh ~ $ alias pwd='ls'
purpleEnduer @sh $ pwd
purpleEnduer @sh~$ /pwd
-sh: /pwd: not found
purpleEnduer @sh ~ $ \pwd
/home/tc
purpleEnduer @sh $

 

5 How to make an alias permanent

The alias we set through the alias command can only be used in the current Shell. If the system is restarted, the newly set alias will be invalid.

If you want the aliases to be permanently valid, you need to add all aliases to the .alias file in the ($HOME) directory.

If this file does not exist on your system, you can create one.

Then add this code to the .bashrc file:

# Add my aliases
if [ -f ~/.alias ]; then
  . ~/.alias
be

After this setting, the aliases in .alias will be valid no matter how the system is restarted.

6 Why is it recommended to use single quotes?

The environment variable PWD saves the absolute path of the current working directory, and we can use it to understand the current working directory.

Let’s first look at the following command sequence:

purpleEndurer @ bash ~ $ echo $PWD
/home/csdn

It can be seen that the current working directory is /home/csdn

Then we define the alias mypwd="echo $PWD" and execute:

purpleEndurer @ bash ~ $ alias mypwd="echo $PWD"
purpleEndurer @ bash ~ $ mypwd
/home/csdn

The result of executing the alias mypwd is the same as the echo $PWD command.

Next, we change the current working directory to /home/csdn/a

purpleEndurer @ bash ~ $ cd a

Execute the command echo $PWD:


purpleEndurer @ bash ~/a $ echo $PWD
/home/csdn/a

The command execution result is correct.

Try the alias mypwd again:


purpleEndurer @ bash ~/a $ mypwd
/home/csdn

The result is wrong.

Why is something wrong? Let me first look at the commands actually executed by the alias mypwd:


purpleEndurer @ bash ~/a $ alias mypwd
alias mypwd='echo /home/csdn'

see it? Since we used double quotes when defining the alias mypwd, the system did not retain the p string $PWD when creating this alias, but replaced it with the value of PWD /home/csdn.

In this way, no matter how the current working directory changes, the alias mypwd always displays /home/csdn.

After finding the reason, we switch to using single quotes to define, and then check the actual command executed by mypwd:

purpleEndurer @ bash ~/a $ alias mypwd='echo $PWD'

purpleEndurer @ bash ~/a $ alias mypwd
alias mypwd='echo $PWD'

After being defined using single quotes, $PWD is saved successfully and is not replaced with its value.

Let’s execute this alias again:

purpleEndurer @ bash ~/a $ mypwd
/home/csdn/a 

This time the execution result is correct.

7 References

doskey | Microsoft Learnicon-default.png?t=N7T8https://learn.microsoft.com/zh-cn/windows-server/administration/windows-commands/doskey

Guess you like

Origin blog.csdn.net/Purpleendurer/article/details/134642886