Reputation: 133
Silly question, but since I'm such a newbie at Linux OS, I'm having trouble to make this simple script to echo the result of PS1
.
If I type echo $PS1
in bash, it shows me the result, but not in the script. It returns blank.
Example:
#!/bin/bash
ps1=$(echo $PS1)
echo $ps1
Any tips for a begginer?
Thanks in advance.
Upvotes: 8
Views: 8754
Reputation: 4341
This one doesn't apply -i
to the whole script file. Based on @glennjackman's answer:
> ps1=$(bash -ic 'echo $PS1')
> echo $ps1
\n\[\033[92;5;10m\]\w$...
Upvotes: 0
Reputation: 13
try this script from the documentation ..this might help you :
PS1 – The value of this parameter is used as the primary prompt string. The default value is \u@\h \W\$ .
$ echo $PS1 Sample output: [\u@\h \W]$
\a : an ASCII bell character (07)
\d : the date in “Weekday Month Date” format (e.g., “Tue May 26”)
\e : an ASCII escape character (033)
\h : the hostname up to the first ‘.’
\H : the hostname
\j : the number of jobs currently managed by the shell
\l : the basename of the shell’s terminal device name
\n : newline
\r : carriage return
\u : the username of the current user
\v : the version of bash (e.g., 2.00)
\V : the release of bash, version + patch level (e.g., 2.00.0)
\w : the current working directory, with $HOME abbreviated with a tilde
\W : the basename of the current working directory, with $HOME abbreviated with a tilde
Examples:
PS1="\d \h $ " Sample output: Sat Jun 02 server $
PS1="[\d \t \u@\h:\w ] $ " Sample output: [Sat Jun 02 14:24:12 vivek@server:~ ] $
Upvotes: -1
Reputation: 247142
Other answers are correct. If you add the -i
flag to your shebang, that signals bash that it's supposed to be an interactive shell, so it will read your ~/.bashrc -- see https://www.gnu.org/software/bash/manual/bashref.html#Invoking-Bash
#!/bin/bash -i
ps1="$PS1"
echo "$ps1"
Upvotes: 9
Reputation: 532208
PS1
is typically not exported, since it is usually defined in .bashrc
, which is sourced by every shell that would need PS1
. As such, it isn't inherited by the non-interactive shell that executes your script.
Upvotes: 3
Reputation: 91139
PS1
is a variable which is not "exported", so it is only visible within the shell, but not from any subprocess such as the script's.
Upvotes: 4