Difference between revisions of "BASH scripting"
(→Bourne-Again Shell (Bash)) |
(→Bourne-Again Shell (Bash)) |
||
Line 25: | Line 25: | ||
./my_first_script.sh | ./my_first_script.sh | ||
+ | |||
+ | Suppose your ''my_first_script.sh'' contains the following lines: | ||
+ | |||
+ | #!/bin/sh | ||
+ | |||
+ | number=6 | ||
+ | for ((i=0;i<number;i++)) | ||
+ | do | ||
+ | echo "Hello world ${i}" | ||
+ | done | ||
+ | |||
+ | If you run <code>./my_first_script.sh</code>, the output will be: | ||
+ | |||
+ | Hello world 0 | ||
+ | Hello world 1 | ||
+ | Hello world 2 | ||
+ | Hello world 3 | ||
+ | Hello world 4 | ||
+ | Hello world 5 | ||
+ | |||
+ | For more on commands, see [[Unix]]. | ||
==Environment Variables== | ==Environment Variables== |
Revision as of 10:20, 24 January 2020
Bourne-Again Shell (Bash)
Bash is an acronym for "Bourne-Again Shell", the name of a code interpreter and a high-level programming language, and it is a must-know tool in Computational Chemistry and Biology.
You can use Bash scripting in Unix/Linux computers through a terminal.
When you initialize the shell, i.e, the interpreter, your computer runs initialization files -- ~/.bash_profile
, ~/.bash_login
, and ~/.profile
(where ~/
points to your home directory) -- but we do not recommend changing these files unless you really know what you are doing.
In most cases, you can change the ~/.bashrc
file, which allows the user to customize the system according to their needs.
A bash script is a text file containing a series of instructions written in the bash language. You can create one by typing the following commands in the terminal:
touch my_first_script.sh
which will generate a modifiable file that you can use to write the instructions to be executed by the shell. You can use the Vi text editor to write your code; just remember to add to the beginning of the file the following line:
#!/bin/sh
This line tells the interpreter that this is a bash script. You can run your script by telling the interpreter:
bash my_first_script.sh
or you can change the permissions of the file to make it an executable by typing:
chmod +x my_first_script.sh
and then running:
./my_first_script.sh
Suppose your my_first_script.sh contains the following lines:
#!/bin/sh number=6 for ((i=0;i<number;i++)) do echo "Hello world ${i}" done
If you run ./my_first_script.sh
, the output will be:
Hello world 0 Hello world 1 Hello world 2 Hello world 3 Hello world 4 Hello world 5
For more on commands, see Unix.
Environment Variables
Bash allows the user to assign values to variables in the command line, but