In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail about Linux: terminal prompt does not take effect as scheduled, the quality of the article content is high, so Xiaobian share for everyone to make a reference, I hope you have a certain understanding of related knowledge after reading this article.
preface
first, let's briefly introduce ourselves. what the hell is prompt? As the name implies is the meaning of the prompt, looks and we are far away, but in fact as long as each contact shell children's shoes, have to see, that is, we input the command in front of that string prompt.
For example:
Of course, this style can be modified, which involves our PS1 and PS2, experienced or previously set children's shoes estimated will not be unfamiliar, wood has been touched children's shoes can refer to the link to learn: linux PS1 prompt definition
problem
Because the above is not the theme of this time, so simply skip. In fact, in addition to displaying this information, our prompt can also display something more 'dynamic'.
Let's look at a requirement (simple):
Want to see the current time every time a 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 on the prompt, so try it out:
As we guessed, the prompt has become our time, but there seems to be a problem, when we execute the next command, the time does not change, it is still 40 seconds, even if we execute it a few times.
Can PS1 only be loaded once? But this reason was quickly rejected, because when we switch users, the prompt $changes to #, and there are other examples (listed below) that show that PS1 executes every time. So the only problem is that we write it.
If you want to see the results of the test, you can do it by placing the date command into the function.
Modify ~/.bash_profile as follows:
echo_time(){ date +"%Y-%m-%d %H:%M:%S" } export PS1="$(echo_time)\$ "
The result is a failure, it seems that such usage is not good! I looked up a lot of documents until I saw an article that said,"You have to have single quotes on both sides," and then I realized something, and I started to understand why my effect failed.
There may be children here who don't know the difference between single quotation marks and double quotation marks in Linux. Simply put, it is:
Double quotes: make most symbols (e. g. *) meaningless and ordinary.
Single quotes: make all symbols meaningless and become ordinary characters.
For example, echo "$a" gives you the value of variable a, but echo '$a' really gives you $a. Specific details can be consulted.
cause analysis
So the reason I failed here is, I need to use single quotes here, not double quotes, because if I use double quotes, PS1 gets the value of the command/function when I assign it, so every time I print PS1, it's already a specific value, not a command/function. It's like:
#Double quotes: PS1="$(date +%F)" results in: PS1="2017-07-24" #Single quotes: PS1='$(date +%F)' results in: PS1='$(date +%F)'
So when it's output at the terminal, it's going to behave differently: single quotes, every time it's output, it's executing that function, and double quotes, because it's fixed, it's always that value!
solutions
Now that we've got the reason, we can fix 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)\$ '
We can see that *** has solved our problem.
PS: With a small bonus.
Requirements:
If long-term development in the terminal, but also gitlab dedicated users of children's shoes, many times will worry about their own development of the code branch will be wrong, or do not remember what branch they are in, often need git branch to see, now we can use this to solve our problem ~
Modify ~/.bash_profile
#Get branch of 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[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\W\[\033[00m\]\[\033[01;32m\]$(get_branch)\[\033[00m\] \$ '
Effect of operation:
As you can see, when I go into a directory of code, my prompt will show me which branch ~ I'm in. This is a small feature, but I think it's quite useful, just like the git bash client.
About Linux: terminal prompt does not take effect as scheduled what is the reason to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.