24.07.2018 Views

Bash-Beginners-Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

Most shell scripts execute in a private environment: variables are not inherited by child processes unless they<br />

are exported by the parent shell. Sourcing a file containing shell commands is a way of applying changes to<br />

your own environment and setting variables in the current shell.<br />

This example also demonstrates the use of different prompt settings by different users. In this case, red means<br />

danger. When you have a green prompt, don't worry too much.<br />

Note that source resourcefile is the same as . resourcefile.<br />

Should you get lost in all these configuration files, and find yourself confronted with settings of which the<br />

origin is not clear, use echo statements, just like for debugging scripts; see Section 2.3.2. You might add lines<br />

like this:<br />

echo "Now executing .bash_profile.."<br />

or like this:<br />

echo "Now setting PS1 in .bashrc:"<br />

export PS1="[some value]"<br />

echo "PS1 is now set to $PS1"<br />

3.2. Variables<br />

3.2.1. Types of variables<br />

As seen in the examples above, shell variables are in uppercase characters by convention. <strong>Bash</strong> keeps a list of<br />

two types of variables:<br />

3.2.1.1. Global variables<br />

Global variables or environment variables are available in all shells. The env or printenv commands can be<br />

used to display environment variables. These programs come with the sh-utils package.<br />

Below is a typical output:<br />

Chapter 3. The <strong>Bash</strong> environment 34

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!