ps1

bash: How to evaluate PS1, PS2, …? [duplicate]

人盡茶涼 提交于 2019-11-29 03:20:44
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Echo expanded PS1 Is there any way to 'evaluate' PS1 , PS2 , etc from within a bash script? Although, I can use alternate means to get all elements of my current PS1 , I would really like to be able to reuse its definition instead of using these alternate means. For example, ===================================== PS1 element --> Alternate means ===================================== \u --> $USER \h --> $HOSTNAME

PS1 command substitution fails when containing newlines on msys bash

邮差的信 提交于 2019-11-29 01:34:58
This command succeeds $ PS1='$(date +%s) $ ' 1391380852 $ However if I add a newline it fails $ PS1='$(date +%s)\n$ ' bash: command substitution: line 1: syntax error near unexpected token `)' bash: command substitution: line 1: `date +%s)' If I use backticks it works $ PS1='`date +%s`\n$ ' 1391381008 $ but backticks are discouraged . So what is causing this error? GNU bash, version 4.2.45(6)-release You can disambiguate the parsing easily, to prevent hitting any such bug (though I can't reproduce it myself): PS1='$(date +%s)'$'\n$ ' This $'\n' syntax parses to a literal newline character,

Detect empty command

泪湿孤枕 提交于 2019-11-28 23:06:08
Consider this PS1 PS1='\n${_:+$? }$ ' Here is the result of a few commands $ [ 2 = 2 ] 0 $ [ 2 = 3 ] 1 $ 1 $ The first line shows no status as expected, and the next two lines show the correct exit code. However on line 3 only Enter was pressed, so I would like the status to go away, like line 1. How can I do this? Here's a funny, very simple possibility: it uses the \# escape sequence of PS1 together with parameter expansions (and the way Bash expands its prompt). The escape sequence \# expands to the command number of the command to be executed. This is incremented each time a command has

Changing PS1 prompt in a Bash parent shell

半世苍凉 提交于 2019-11-28 13:54:34
Using a script, I was to change the prompt of the parent Bash shell. I have tried the following: PS1="Hello World > " This changes the prompt of the subshell, which the script is running in, but which command would I use to change the prompt of the parent shell. Any ideas? In all cases the parent shell must cooperate. The child process in a unix environment cannot influence the parent process without its cooperation. Try this in the subshell script changePrompt.sh : echo 'PS1="Hello World > "' And then call the script from the parent shell like this: eval "$(changePrompt.sh)" Or, a different

Mac终端中主机名与用户名的修改

ぐ巨炮叔叔 提交于 2019-11-28 00:09:34
参照: https://www.cnblogs.com/tdcqma/p/6714492.html Mac终端的主机名称+用户名全部显示出来的话会可能显得特别长,影响整体美观,可以切换至root用户后 修改.bashrc文件进行格式化。 1. 终端中切换root用户 su - root 2. 修改/etc/bashrc文件中的PS1='\h:\W \u\$ ',其中\h代表主机名,\u代表用户名 # System-wide .bashrc file for interactive bash(1) shells. if [ -z "$PS1" ]; then return fi PS1='\h:\W \u\$ ' # Make bash check its window size after a process completes shopt -s checkwinsize [ -r "/etc/bashrc_$TERM_PROGRAM" ] && . "/etc/bashrc_$TERM_PROGRAM" alias ll='ls -al' 3. 比如修改成:PS1='abc:\W 123\$ ' , 终端显示如下: ps:注意,如果担心修改错误在修改前最好先备份一下/etc/bashrc文件 cp /etc/bashrc /tmp 来源: https://www.cnblogs

How to print current bash prompt?

♀尐吖头ヾ 提交于 2019-11-27 17:55:18
问题 The question is simple. I want to evaluate current value of PS1 in my bash script. All materials on google point to tutorials on pimping it up, but I want to evaluate to see how would it be rendered by my current terminal, or at least by some terminal. Is there any soft/function that would help me achieve that? Of course I'd like to have all escaped characters evaluated, so echo $PS1 is not that useful in my case. 回答1: Bash 4.4+ solution using parameter transformation for a prompt string:

Why doesn't my bash prompt update?

瘦欲@ 提交于 2019-11-27 17:09:23
I'm new to git and I'm trying to add the current git branch to my already existing prompt, which is defined as follows : RESET="\[\017\]" NORMAL="\[\033[0m\]" RED="\[\033[31;1m\]" YELLOW="\[\033[33;1m\]" WHITE="\[\033[37;1m\]" SMILEY="${WHITE}:)${NORMAL}" FROWNY="${RED}:(${NORMAL}" SELECT="if [ \$? = 0 ]; then echo \"${SMILEY}\"; else echo \"${FROWNY}\"; fi" export PS1="${RESET}${YELLOW}\u@\h${NORMAL} \`${SELECT}\` ${YELLOW}\w $(__git_ps1) >${NORMAL} " I tried it (by sourcing my .bashrc file again) and it seemed to work, but then I went on another branch and it did not update. How can I make

PS1 command substitution fails when containing newlines on msys bash

帅比萌擦擦* 提交于 2019-11-27 16:03:38
问题 This command succeeds $ PS1='$(date +%s) $ ' 1391380852 $ However if I add a newline it fails $ PS1='$(date +%s)\n$ ' bash: command substitution: line 1: syntax error near unexpected token `)' bash: command substitution: line 1: `date +%s)' If I use backticks it works $ PS1='`date +%s`\n$ ' 1391381008 $ but backticks are discouraged. So what is causing this error? GNU bash, version 4.2.45(6)-release 回答1: You can disambiguate the parsing easily, to prevent hitting any such bug (though I can't

Detect empty command

这一生的挚爱 提交于 2019-11-27 14:43:19
问题 Consider this PS1 PS1='\n${_:+$? }$ ' Here is the result of a few commands $ [ 2 = 2 ] 0 $ [ 2 = 3 ] 1 $ 1 $ The first line shows no status as expected, and the next two lines show the correct exit code. However on line 3 only Enter was pressed, so I would like the status to go away, like line 1. How can I do this? 回答1: Here's a funny, very simple possibility: it uses the \# escape sequence of PS1 together with parameter expansions (and the way Bash expands its prompt). The escape sequence \#

How can I shortern my command line prompt's current directory?

微笑、不失礼 提交于 2019-11-27 10:17:53
I am using Ubuntu and I am tired of this long prompts in bash when I am working with some deep directory hierarchy. So, I would like to tweak my PS1 to shorten the working directory part the following way: Currently I have: pajton@dragon:~/workspace/projects/project1/folder1/test$ and would like to have: pajton@dragon:~/workspace/.../folder1/test$ The truncating would occur if len($PWD) passes given threshold. I want to always keep the first path component and at least one last path component. Then as space permits, add more components taking from the right. This is what I have currently. It