This is a new post in a long, long time. A lot has happened since New Year. I'll post about that at a later date. :)
Now to using bash under Linux.
When you are running a bash session in a terminal you are inside of an interactive environment and you can edit .basrc and source it at any given time.
You source .bashrc like this:
source ~/.bashrc or . ~/.bashrc
Now, let's say you have a bash script that you want to run. You can run in in a terminal, but that is not the subject of this post. You can run it via a file manager, like Dolphin, thunar, ... or you can put a shortcut to it on your desktop.
If you want to source .bashrc in that script, it won't work, because running via file manager or desktop is running a bash script in an non-interactive environment. .bashrc notices that and does not do anything. In fact if you look at .bashrc you will find something like this:
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
at the beginning of you .bashrc.
But there is a solution for this. Put PS1='$ ' in your script before sourcing .bashrc.
Examples:
- run a terminal
- edit .bashrc and put
export TEST_TEXT='My test text' in your .bashrc
- make a new script test.sh:
#!/bin/bash
. ~/.bashrc
echo "TEST_TEXT = $TEST_TEXT"
read
- make it executable
chmod +x test.sh
- run it and you should get something like this:
TEST_TEXT =
- now source .bashrc and run the test.sh script
. ~/.bashrc
./test.sh
- and you get
TEST_TEXT = My test text
If you run this script via file manager you will always get TEST_TEXT =
Change the script to
#!/bin/bash
PS1='$ '
. ~/.bashrc
echo "TEST_TEXT = $TEST_TEXT"
read
and .bashrc will get source everytime you run it.
Ni komentarjev:
Objavite komentar