如何设置在PS1条件换行符? [英] How to set a conditional newline in PS1?

查看:498
本文介绍了如何设置在PS1条件换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置 PS1 ,以便它打印出的东西登录后恰到好处,但preceded以换行符更高版本。

假设出口PS1 =\\ H:\\ W \\ U \\ $,所以第一次(即登录后右),您可以:

 主机名:用户名〜$

我一直在试图像我的〜/ .bashrc中

 功能__ps1_newline_login {
  如果[[-n$ {PS1_NEWLINE_LOGIN-}]];然后
    PS1_NEWLINE_LOGIN =真
  其他
    printf的'\\ n'
  科幻
}出口PS1 =\\ $(__ ps1_newline_login)\\ H:\\ W \\ U \\ $

期望得到:

 #<空行>
主机名:用户名〜$

从一开始一个完整的例子是:

 主机名:用户名〜$`LS#声明:没有空线以上所需的`!
桌面文档主机名:用户名〜$


解决方案

请尝试以下操作:

 功能__ps1_newline_login {
  如果[[-z$ {PS1_NEWLINE_LOGIN}]];然后
    PS1_NEWLINE_LOGIN =真
  其他
    printf的'\\ n'
  科幻
}PROMPT_COMMAND ='__ ps1_newline_login
出口PS1 =\\ H:\\ W \\ U \\ $

说明:


  • PROMPT_COMMAND 是被设置提示之前,这是每次执行一个特殊的bash变量。

  • 您需要使用 -z 标志,检查是否字符串的长度为0。

I am trying to set PS1 so that it prints out something just right after login, but preceded with a newline later.

Suppose export PS1="\h:\W \u\$ ", so first time (i.e., right after login) you get:

hostname:~ username$ 

I’ve been trying something like in my ~/.bashrc:

function __ps1_newline_login {
  if [[ -n "${PS1_NEWLINE_LOGIN-}" ]]; then
    PS1_NEWLINE_LOGIN=true
  else
    printf '\n'
  fi
}

export PS1="\$(__ps1_newline_login)\h:\W \u\$ "

expecting to get:

# <empty line>
hostname:~ username$ 

A complete example from the the beginning would be:

hostname:~ username$ ls `# notice: no empty line desired above!`
Desktop      Documents

hostname:~ username$ 

解决方案

Try the following:

function __ps1_newline_login {
  if [[ -z "${PS1_NEWLINE_LOGIN}" ]]; then
    PS1_NEWLINE_LOGIN=true
  else
    printf '\n'
  fi
}

PROMPT_COMMAND='__ps1_newline_login'
export PS1="\h:\W \u\$ "

Explanation:

  • PROMPT_COMMAND is a special bash variable which is executed every time before the prompt is set.
  • You need to use the -z flag to check if the length of a string is 0.

这篇关于如何设置在PS1条件换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆