Skip to main content
replaced http://superuser.com/ with https://superuser.com/
Source Link

@slhck@slhck provided valuable suggestions and proof-read most of this. Thanks Werner!

Username http://superuser.com/users/flair/22317.pngUsername

In this questionthis question, XenoroseXenorose asked:

Through some bash debugging facilities, it's even possible to add these formatting rules to the commands you entercommands you enter.

Through color highlighting, the prompt is easily visible between program invocations with lots of output. It prints the previous command's return code unless it's 0, and lists all files in the current directory if the file count is reasonably low. I prefer this to functions that combine cd and lsfunctions that combine cd and ls, as I can immediately see the changes to the current directory after program execution.

As I wrote at the beginning, your prompt is not kept across shell sessions when you simply set PS1 (or PS2) in your shell. To keep your changes across sessions and restarts of your machine, add the relevant assignments of PS1, PS2 and PROMPT_COMMAND -- one per line -- to your .bash_profile or .bashrc file in your home directory (more on these filesmore on these files). Save the file, open a new bash session, and enjoy your custom prompt!

@slhck provided valuable suggestions and proof-read most of this. Thanks Werner!

Username http://superuser.com/users/flair/22317.png

In this question, Xenorose asked:

Through some bash debugging facilities, it's even possible to add these formatting rules to the commands you enter.

Through color highlighting, the prompt is easily visible between program invocations with lots of output. It prints the previous command's return code unless it's 0, and lists all files in the current directory if the file count is reasonably low. I prefer this to functions that combine cd and ls, as I can immediately see the changes to the current directory after program execution.

As I wrote at the beginning, your prompt is not kept across shell sessions when you simply set PS1 (or PS2) in your shell. To keep your changes across sessions and restarts of your machine, add the relevant assignments of PS1, PS2 and PROMPT_COMMAND -- one per line -- to your .bash_profile or .bashrc file in your home directory (more on these files). Save the file, open a new bash session, and enjoy your custom prompt!

@slhck provided valuable suggestions and proof-read most of this. Thanks Werner!

Username

In this question, Xenorose asked:

Through some bash debugging facilities, it's even possible to add these formatting rules to the commands you enter.

Through color highlighting, the prompt is easily visible between program invocations with lots of output. It prints the previous command's return code unless it's 0, and lists all files in the current directory if the file count is reasonably low. I prefer this to functions that combine cd and ls, as I can immediately see the changes to the current directory after program execution.

As I wrote at the beginning, your prompt is not kept across shell sessions when you simply set PS1 (or PS2) in your shell. To keep your changes across sessions and restarts of your machine, add the relevant assignments of PS1, PS2 and PROMPT_COMMAND -- one per line -- to your .bash_profile or .bashrc file in your home directory (more on these files). Save the file, open a new bash session, and enjoy your custom prompt!

added formatting to ps1/ps2 introduction and default prompt explanation, added screenshot of color example, detailed explanation of example prompt
Source Link
Daniel Beck Mod
  • 111.4k
  • 1
  • 28
  • 58

I'll start with PROMPT_COMMAND, as its output appears first:

PS1='\[\033[0;32m\]\u\[\033[00m\]PROMPT_COMMAND='RET=$?; inecho; \[\033[0;32m\]$(if pwd[ )$RET ($(!= OUT=$(0 ls] -A; |then wcecho -l"rc: );$RET"; echofi; $OUTif )[ entries,"$PWD" $((!= "$HOME" ]; then if [ $( ls -A | wc -l ) -lt $(20 ls]; |then wcls -l )mAF; ))fi; hidden)\n\[\033[1;32m\]#\#else !\!ls \$\[\033[;m\]-mF; 'fi'

  • This reads the last command's return value, and if non-zero, prints it on a separate line.
  • If the current working directory is not the home directory, and has fewer than 20 files and folders in it, list them. If the current working directory is the home directory, only list non-hidden files and folders.

The prompt itself:

PROMPT_COMMAND='RET=$?; echo; if [ $RET !=PS1='\[\033[0;32m\]\u\[\033[00m\] 0in ]\[\033[0;32m\]$( ;pwd then) echo($( "rc:OUT=$( $RET";ls fi;-A if| [wc "$PWD"-l !=); "$HOME"echo ];$OUT then) ifentries, [$(( $( ls -A | wc -l ) -lt 20$( ];ls then| lswc -mAF;l fi;) else)) lshidden)\n\[\033[1;32m\]#\# -mF;!\! fi'\$\[\033[;m\] '

  • Print the user name,
  • the full current working directory (i.e. no ~ instead of /Users/danielbeck),
  • the numbers of both all and hidden files and folders in the current working directory,
  • and on the next line, print the command and history numbers, and the root privileges indicator $/#.

Through color highlighting, the prompt is easily visible between program invocations with lots of output. It shows both the command and history numbers, my user name and full current working directory path, and the number of files in the current directory (both non-hidden and all files).

Through color highlighting, the prompt is easily visible between program invocations with lots of output. It prints the previous command's return code unless it's 0, and lists all files in the current directory if the file count is reasonably low. I prefer this to functions that combine cd and ls, as I can immediately see the changes to the current directory after program execution.

PS1='\[\033[0;32m\]\u\[\033[00m\] in \[\033[0;32m\]$( pwd ) ($( OUT=$( ls -A | wc -l ); echo $OUT ) entries, $(( $( ls -A | wc -l ) - $( ls | wc -l ) )) hidden)\n\[\033[1;32m\]#\# !\! \$\[\033[;m\] '

PROMPT_COMMAND='RET=$?; echo; if [ $RET != 0 ] ; then echo "rc: $RET"; fi; if [ "$PWD" != "$HOME" ]; then if [ $( ls -A | wc -l ) -lt 20 ]; then ls -mAF; fi; else ls -mF; fi'

Through color highlighting, the prompt is easily visible between program invocations with lots of output. It shows both the command and history numbers, my user name and full current working directory path, and the number of files in the current directory (both non-hidden and all files).

It prints the previous command's return code unless it's 0, and lists all files in the current directory if the file count is reasonably low. I prefer this to functions that combine cd and ls, as I can immediately see the changes to the current directory after program execution.

I'll start with PROMPT_COMMAND, as its output appears first:

PROMPT_COMMAND='RET=$?; echo; if [ $RET != 0 ] ; then echo "rc: $RET"; fi; if [ "$PWD" != "$HOME" ]; then if [ $( ls -A | wc -l ) -lt 20 ]; then ls -mAF; fi; else ls -mF; fi'

  • This reads the last command's return value, and if non-zero, prints it on a separate line.
  • If the current working directory is not the home directory, and has fewer than 20 files and folders in it, list them. If the current working directory is the home directory, only list non-hidden files and folders.

The prompt itself:

PS1='\[\033[0;32m\]\u\[\033[00m\] in \[\033[0;32m\]$( pwd ) ($( OUT=$( ls -A | wc -l ); echo $OUT ) entries, $(( $( ls -A | wc -l ) - $( ls | wc -l ) )) hidden)\n\[\033[1;32m\]#\# !\! \$\[\033[;m\] '

  • Print the user name,
  • the full current working directory (i.e. no ~ instead of /Users/danielbeck),
  • the numbers of both all and hidden files and folders in the current working directory,
  • and on the next line, print the command and history numbers, and the root privileges indicator $/#.

Through color highlighting, the prompt is easily visible between program invocations with lots of output. It prints the previous command's return code unless it's 0, and lists all files in the current directory if the file count is reasonably low. I prefer this to functions that combine cd and ls, as I can immediately see the changes to the current directory after program execution.

added formatting to ps1/ps2 introduction and default prompt explanation, added screenshot of color example
Source Link
Daniel Beck Mod
  • 111.4k
  • 1
  • 28
  • 58

Bash primarily uses two variables that define how your prompt looks like:

  • PS1 specifies the format of your regular prompt that appears to the left of every new command you type
  • PS2 defines the continuation prompt: It appears when you enter a line ending with a backslash \ to continue input on the next line.

In the following example, I split the ls command over two input lines to show my system's default PS1 specifies the format of your regular prompt, whileand PS2 defines the continuation prompt: It appears when you enter a line ending with a backslash \ to continue input on the next line. In the following example, I split the ls command over two input linesin action:

As you can see, my PS1 prompt contains the host name (Saurus), the current working directory (~ is a synonym to the user's home directory), my username (danielbeck), and an indicator that I am a regular user ($).

  • the host name (Saurus),
  • the current working directory (~ is a synonym to the user's home directory),
  • my username (danielbeck),
  • and an indicator that I am a regular user ($).

This example produces the following output:

enter image description here

We use two suchof these sequences:

Bash primarily uses two variables that define how your prompt looks like: PS1 specifies the format of your regular prompt, while PS2 defines the continuation prompt: It appears when you enter a line ending with a backslash \ to continue input on the next line. In the following example, I split the ls command over two input lines:

As you can see, my PS1 prompt contains the host name (Saurus), the current working directory (~ is a synonym to the user's home directory), my username (danielbeck), and an indicator that I am a regular user ($).

We use two such sequences:

Bash primarily uses two variables that define how your prompt looks like:

  • PS1 specifies the format of your regular prompt that appears to the left of every new command you type
  • PS2 defines the continuation prompt: It appears when you enter a line ending with a backslash \ to continue input on the next line.

In the following example, I split the ls command over two input lines to show my system's default PS1 and PS2 in action:

As you can see, my PS1 prompt contains

  • the host name (Saurus),
  • the current working directory (~ is a synonym to the user's home directory),
  • my username (danielbeck),
  • and an indicator that I am a regular user ($).

This example produces the following output:

enter image description here

We use two of these sequences:

Trying to keep consistency between posts.
Source Link
Tamara Wijsman
  • 57.7k
  • 24
  • 45
Loading
Source Link
Daniel Beck Mod
  • 111.4k
  • 1
  • 28
  • 58
Loading