Reputation: 40182
I would like to see the output of the escaped special characters used in setting up $PS1
. For example, placing \u
in PS1 will output the username of the current user.
So in essence:
omar @ ~ > echo -e '\u'
Expect:
omar
Actual output:
\u
Upvotes: 0
Views: 847
Reputation: 29889
You can do it with parameter expansion with the @P
operator like this:
prompt="\u"; echo ${prompt@P}
As of Bash 4.4, you can use the @P
operator in Parameter expansion:
${parameter@operator}
The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter:
@P
The expansion is a string that is the result of expanding the value of parameter as if it were a prompt
Here's some of the common special characters available to the PS1 command:
The following lists the special characters that can appear in the prompt variables PS0, PS1, PS2, and PS4
\a
A bell character
\d
The date, in "Weekday Month Date" format (e.g., "Tue May 26")
\e
An escape character
\H
The hostname
\u
The username of the current user
\w
The current working directory
Further Reading:
Upvotes: 1
Reputation: 40182
Here's one approach that can be used to show the value of an escaped character.
The functions defined below will change your current PS1 to the given string. You can see the output right after entering the command.
Store your current PS1 in a variable
> save=$PS1
Create a reset function
> function reset { PS1=$save; }
Create the print function
> function omar { PS1="\\$1 "; }
Use it as follows
> omar u
: Your command prompt will be your username
omar >
> omar @
: Your command prompt will be the current time in 12-hour am/pm format
11:19 PM >
> omar h
: Your command prompt will be the hostname up to the first `.'
omar-Laptop >
etc...
You can reset your PS1
by either calling reset
or restarting the terminal
Upvotes: 0