Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo $PS1 in script

Tags:

bash

shell

ps1

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.

like image 428
Rafael Avatar asked Dec 11 '22 08:12

Rafael


2 Answers

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"
like image 76
glenn jackman Avatar answered Dec 28 '22 01:12

glenn jackman


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.

like image 35
glglgl Avatar answered Dec 27 '22 23:12

glglgl