I need to set some variables in my shell and format the shell prompt before I do a certain task every time. Also I need to record my actions.
At the moment, I’m running a script, collecting all the information it needs (partly by user input) and it sets the new shell prompt and the variables. Then I start the recording with script $var-actionlog-$anothervar-1.log. The variables used are set during the call of my_script.sh. Then, since a new shell spawned by the script command, I need to call source my_script.sh again.
Is there a way that I can include the script-part directly in my_script.sh so that I don’t have to enter everything twice?
You can use
exportin your initial setup script to make the local variables global. For example, your setup script,my_script.shcould be:Then, once the script has started, to confirm you can run:
If you want to set a different shell prompt (
$PS1) in yourmy_script.sh, to be used by the typescript, make sure that you are not setting$PS1in~/.bashrc. The typescript,script $var-actionlog-$anothervar-1.log, calls~/.bashrcevery time and will override your prompt variable,$PS1. To set your prompt for log-in shells only, set this in~/.bash_login.