cataloguecd ../../../And so on and so on.

When the input is relatively fast, one more point number “or one less point number” will be input in the middle, which needs to be deleted and reentered.



In order to simplify input and reduce input errors, this article introduces acdup.shShell script for.


For example, executionsource 5Command is equivalent to executioncd ../../../../../Orders.


How to set command aliases to avoid typing will be described latersource cdup.shtheseup 5Such input is enough.

Because shell script runs in subshell by default, andcdcatalogue

So it’s executed in a shell scriptcdThe command cannot change the working directory of the parent shell.

In order to keep the script in thecdAfter the directory, need to usesourceCommand to execute the shell script.

usesourceCommand execution shell script, will run under the current shell, rather than running in the subshell.

Script code

listcdup.shThe specific code of the script is as follows.

In this code, detailed comments are provided for most of the key codes to facilitate reading.

Some key points will be explained later in this article to help understand.

#In order to simplify input, the current script can process an integer parameter, specifying how many levels to return to
#For example, source 3 is equivalent to CD.. //
#In order to keep the script in the directory after CD after execution, you need to use the source command
#The following aliases can be added to the ~ /. Bashrc file to facilitate execution:
#   alias up='source'
#The subsequent up 3 command is equivalent to CD.. //

printf "USAGE
    source number
    For example, source 3 is equivalent to CD.. //
    You can set up the up alias with alias up = - source cdup. Sh 'to facilitate execution

if [ $# -ne 1 ]; then
    return 1


    #The first parameter given specifies how many levels to return to
    local count=$1

    local updirs=""
    while ((--count >= 0)); do
        #When arithmetic extension is not used, bash's + = operator will splice strings by default
    echo ${updirs}

target_dir="$(count_updirs $1)"
#Use CD to specify that the original CD command is executed without alias
\cd "${target_dir}"


Code key point description

It is recommended to set the command alias to execute the current script

As explained earlier, you need to use thesourceCommand to executecdup.shScript so that after the script is executed, it can remain in thecdAfter the directory.

That is, when executing, it needs to be written assource

In this way, you need to input more characters, and it’s easy to forget to provide themsourceOrders.

To facilitate input, it is recommended to set command aliases in script comments to execute the current script.

For example, in~/.bashrcAdd the following statement to the file to set the command alias:

alias up='source'

After adding this statement, in the current terminal, you need to executesource ~/.bashrcCommand, the alias will take effect.

You can also reopen the terminal. In the newly opened terminal, the alias will take effect by default.

After the alias takes effect, it can be usedupCommand to executecdup.shscript.

For example,up 3Command equivalent tosource 3Orders.

Here’s the assumptioncdup.shThe script is placed in the addressing directory specified by the path global variable, and can be executed through the file name without specifying the path of the file.


A backslash before the CD command specifies that no alias is used

In Bash, setting command alias is supported.

If a command alias is set tocdString, then executecdCommand to execute the command specified by the alias.

In other words, the original task of switching working directory will not be executedcdOrders.

To avoid this problem, you can use the\cdTo specify that the alias is not usedcdCommand itself.

staycdup.shIt is used in the script\cdcatalogue

Check man bash’s description of alias. It is not explicitly mentioned that adding a backslash in front of the command can avoid using alias.

This is a special usage based on the function of backslash escape character and the processing logic of alias.

yes\cdThis writing method does not need to use the related instructions of command alias. The specific interpretation is as follows.

See man bash vs. backslash\The explanation is as follows:

A non-quoted backslash (\) is the escape character.

It preserves the literal value of the next character that follows, with the exception of <newline>.

That is, when the backslash\When it is not enclosed in any quotation marks, it can keep the following character unchanged.

In addition, after bash processing, it will be removed\Character, only the next character itself is retained.

For example, in Bash, without any quotation marks,\cYou get the character C.

Specific examples are as follows:

$ echo \c
$ echo \cd

As you can see,echo \cThe command prints the character c, not the “C” string.

echo \cdThe command prints the “CD” string.

In fact, it is\cAfter getting character c, character c is combined with character d to form a “CD” string.

Check man bash’s description of alias as follows:

The first word of each simple command, if unquoted, is checked to see if it has an alias.

If so, that word is replaced by the text of the alias.

That is, bash will get the first character of the command that is not enclosed in quotation marks, and check whether it is an alias based on this character.

If so, replace the command alias with the specified command.

In Bash, you cannot set the first character of a command alias to backslash.

Specific examples are as follows:

$ alias \testcd='cd ../../'
$ alias | grep cd
alias testcd='cd ../../'

Here we go firstalias \testcd='cd ../../'Command to set the alias.

From the input parameters, the set alias looks like\testcd

But withaliasCommand prints out all command aliases and uses thegrepFilter out thecdYes.

As you can see, the alias actually set istestcd

The first character of the alias is not a backslash.

As explained earlier, bash removes backslashes that are not enclosed in quotation marks.

Check out the online help link for GNU bash…It is explained as follows:

3.1.1 Shell Operation

2.Breaks the input into words and operators, obeying the quoting rules described in Quoting. These tokens are separated by metacharacters. Alias expansion is performed by this step.

4.Performs the various shell expansions, breaking the expanded tokens into lists of filenames and commands and arguments.

3.5.9 Quote Removal

After the preceding expansions, all unquoted occurrences of the characters ‘’, ‘’’, and ‘”’ that did not result from one of the above expansions are removed.

That is, BashQuote RemovalPhase removes backslashes\Character.

andQuote RemovalThis is done after the shell extension.

The alias is determined before shell extension.

Therefore, bash will also see backslashes when judging aliases\Character.

That is to say, execution\cdCommand, in the judge alias phase, you will see that the first character of the command is\

As explained earlier, you cannot set the first character of a command alias to backslash.

Then I can’t find it\cdThe corresponding alias will not be replaced.

afterQuote RemovalAfter the stage, the backslash is removed, leaving only onecdOrders.

At this time, we have passed the stage of judging alias, so we are executingcdThe command itself does not execute thecdCommand alias for.

That is, strictly speaking, adding a backslash in front of a command is not without an alias.

Instead, an alias starting with a backslash cannot be found, so no alias extension is performed.

Generally speaking, it can be simply understood as adding a backslash before the command to specify that no alias is used.

Example of executing the current script

Use theupCommand aliassource cdup.shThe specific execution results are as follows:

[frameworks/base/services/core/java/com/android/server]$ cd ../../../../../
[frameworks/base/services]$ cd -
[frameworks/base/services/core/java/com/android/server]$ up 5

In this example, the directory path shown in the middle of the square brackets is the working directory of the current shell.

As you can see,cd ../../../../../The command returns to the level 5 superior directory.

Then executecd -catalogue

Reexecutionup 5catalogue

up 5The effect of the command is similar to that of the commandcd ../../../../../The command is the same, but the input is very simple and not easy to make mistakes.