You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />
rbash script_name.sh<br />
sh script_name.sh<br />
bash -x script_name.sh<br />
The specified shell will start as a subshell of your current shell and execute the script. This is done when you<br />
want the script to start up with specific options or under specific conditions which are not specified in the<br />
script.<br />
If you don't want to start a new shell but execute the script in the current shell, you source it:<br />
source script_name.sh<br />
source = .<br />
The <strong>Bash</strong> source built-in is a synonym for the Bourne shell . (dot) command.<br />
The script does not need execute permission in this case. Commands are executed in the current shell context,<br />
so any changes made to your environment will be visible when the script finishes execution:<br />
willy:~/scripts> source script1.sh<br />
--output ommitted--<br />
willy:~/scripts> echo $VALUE<br />
9<br />
willy:~/scripts><br />
2.2. Script basics<br />
2.2.1. Which shell will run the script?<br />
When running a script in a subshell, you should define which shell should run the script. The shell type in<br />
which you wrote the script might not be the default on your system, so commands you entered might result in<br />
errors when executed by the wrong shell.<br />
The first line of the script determines the shell to start. The first two characters of the first line should be #!,<br />
then follows the path to the shell that should interpret the commands that follow. Blank lines are also<br />
considered to be lines, so don't start your script with an empty line.<br />
For the purpose of this course, all scripts will start with the line<br />
#!/bin/bash<br />
As noted before, this implies that the <strong>Bash</strong> executable can be found in /bin.<br />
2.2.2. Adding comments<br />
You should be aware of the fact that you might not be the only person reading your code. A lot of users and<br />
system administrators run scripts that were written by other people. If they want to see how you did it,<br />
comments are useful to enlighten the reader.<br />
Chapter 2. Writing and debugging scripts 24