Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that the Linux terminal prompt prompt does not take effect as scheduled

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to solve the problem that the Linux terminal prompt prompt does not take effect as scheduled. I hope you will get something after reading this article. Let's discuss it together.

Preface

Before we start this article, let's give a brief introduction to what the prompt is.

As the name implies, it means the prompt, which seems to be far away from us, but in fact, every child's shoe that comes into contact with shell can see it, and that is the prompt in front of us when we enter the command.

For example:

Of course, this style can be modified, which involves our PS1 and PS2. Children's shoes that have experience or have been set up before are probably no stranger. Children's shoes with no contact can refer to the link to learn: https://www.jb51.net/article/72004.htm

problem

Because the above is not the theme of this time, so simply skip it. In fact, our prompt can not only show these information, but also show some more dynamic things.

Let's first look at a requirement (simple):

We want to see the current time each time the command is executed, so we can add the following to ~ / .bash_profile:

Export PS1= "$(date +"% Y-%m-%d% H:%M:%S ")\ $"

As the code shows, you should be able to see our time at the prompt, so try it now:

As we guessed, the prompt has become our time, but there seems to be a problem. When we execute the next command, the time remains the same, 40 seconds, even if we execute it a few more times.

Can PS1 only be loaded once? But this reason is quickly rejected, because when we switch users, the $of the prompt changes to #, and there are other examples (shown below) that PS1 is executed every time. So the problem can only lie in the way we write it.

Considering whether or not to write a dead command, I put the date command in the function to see if it works:

Modify the ~ / .bash_profile file as follows:

Echo_time () {date + "% Y-%m-%d% H:%M:%S"} export PS1= "$(echo_time)\ $"

The result is a failure, it seems that this kind of usage is not good! After consulting a lot of documents, I saw an article saying that there must be single quotation marks on both sides, and then I suddenly realized that I began to understand why my effect failed.

There may be children's shoes here who don't know the difference between linux's single quotation marks and double quotation marks. To put it simply:

Double quotation marks: make most symbols (such as *) lose their meaning and become ordinary characters.

Single quotation marks: make all symbols lose their meaning and become ordinary characters.

For a simple example, echo "$a" can get the value of the variable a, but echo'$a 'can really only output $a. Specific details can be found.

Cause analysis

So the reason for my failure here is that I need to use single quotation marks instead of double quotation marks, because if you use double quotation marks, PS1 will already get the value of the command / function when assigning values, so every time you print PS1, it is already a specific value, not a command / function. It's like:

# double quotation marks: PS1= "$(date +% F)" is the result: PS1= "2017-07-24" # single quotes: PS1='$ (date +% F) 'the result is: PS1='$ (date +% F)'

So when the terminal output, there will be a different performance: single quotation marks, each output, is the execution of that function, while double quotation marks, because it is already a fixed character, so every time it is that value!

Solution

When we find the reason, it is easy to modify it. We just need to change the syntax from double quotes to single quotes.

Modify ~ / .bash_profile

Echo_time () {date + "% Y-%m-%d% H:%M:%S"} export PS1='$ (echo_time)\ $'

You can see that this is the perfect solution to our problem.

PS: with a small benefit

Demand:

If you have been developing under the terminal for a long time and are dedicated users of gitlab, you will often worry that the branch of code you develop will make a mistake, or you may not remember what the branch you are in. You will often need git branch to look at it. Now we can use this to solve our problem.

Modify ~ / .bash_profile

# get the branch of the code get_branch () {BRANCH= `git branch 2 > / dev/null | sed-n'/ ^\ * / s / ^\ * / / p'`if [[- n $BRANCH]] then echo "[* $BRANCH]" else echo''fi} export PS1='\ [\ 033 [01f 32m\]\ u@\ h\ [\ 033 [00m\]:\ [\ 033 [01trans36m\]\ W\ [\ 033 [00m\]\ [\ 033 [01] 32m\] $(get_branch)\ [\ 033 [00m\]\ $'

The effect of running:

After reading this article, I believe you have a certain understanding of "how to solve the problem that the Linux terminal prompt prompt does not take effect as scheduled". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report