Bash Shell and bash scripting

When you open up a terminal in linux its very likely what you are looking at is the bash shell prompt. Their are other shells like fish and zsh but bash is the default on most linux distros.

Many of the commands you use like cd and ls are not standalone programs but are internal bash commands. One of the more useful things bash provides is a scripting ability. Bash scripts are somewhat analogous to Dos batch commands. A bash script is just a list of commands to do one after the other. Something that would be tedious to type by hand each time.

It’s a good habit to get into to start your scripts with a line like this. #!/bin/bash That is known as a shebang and tells the system how to run the script. While not necessary it will become so if you turn your script into an executable program so get in the habit of adding it. Lets take a look at an example script


#!/bin/bash
# loads "Yet Another APRS Client"
cd ~/YAAC
java -jar YAAC.jar

this bash script changes directory to YAAC and tells java to load a jar file for YAAC APRS software. Notice the second line begins with a #. Except for the shebang any line beginning with a # is a comment and will not be ran through the bash interpretor.

Also it is a good habit to add a blank line at the bottom of your script.

Command timing

Sometimes when running commands in a bash script the first command needs to finish up before the second can run properly. in that case put a sleep statement between the commands such as this

command1 sleep 5 command2

the number after sleep is the number of seconds to delay after running the first command

bash variables

there will be times when you need to pass a command an option to use

such as specifying a directory each time the script is run. this is where variables are used. bash looks for the $ before a value and knows its a variable. So if you typed a command with 2 variables such as command option1 option2 Then in the script you can signify these as $1 and $2

Heres an example script we will call it create.sh

#!/bin/bash
mkdir $1
cd $1
touch $2

when I type at the command line sh create.sh data file1 it will create a directory called data move into that directory and create an empty file named file1

script execution

when running a script you can simply type sh scriptname.sh and the script will run but you can do

chmod +x scriptname

and it will be executable. In linux to run a program in the current directory you do ./programname

What I do is make a directory in my home folder called bin, put my scripts in there and add that folder to the path then all you have to do is type the script name.

this brings up the .bashrc file

.bashrc is a hidden file ( all files hidden in linux start with a . ) this file tells bash what environment parameters to use.

it can be quite long but to add a folder to your path go to the bottom of the .bashrc file and add a new line like this export PATH=“$PATH:/home/afleak/bin” replace afleak with your user name The $PATH tells bash to append the directory to your current path so that you don’t clobber the Path to important program folders. Once you add the bin folder to your path logout and back in again now scripts residing in your path can be ran by just typing their name.