Watching:  

Introduction to Bash Scripting


Author: Subject Coach
Added on: 23rd Jan 2015

 
Please note: You need to login to view this resource

If you are looking for a beginer's course for Bash scripting, This course is very well for you. I've covered some of the most important topics to get you started. Course contents include

  1. Welcome message [Free]
  2. What is Bash
  3. Command line shortcuts and tricks
  4. Getting started with Bash profiles
  5. Bash Expansions
  6. Command Redirection and Piping 
  7. Using Echo and PrintF and how to preventing command expansion
  8. Understanding local and environment variables
  9. Bash script syntax
  10. A basic Bash script
  11. Working with numbers and operators
  12. Working with arrays
  13. Controlling output with awk, grep and cut commands. How to use basic bash color themes with your output.
  14. Working with here document and files
  15. Flow control statements and loops
  16. Introduction to functions
  17. Interacting with user
  18. Closing note

I hope that you will learn heaps from this course, please leave your feedback and improvement suggestions.

 

 

Author: Subject Coach
Added on: 23rd Jan 2015

Please get in touch with your teacher or tutor in case you have a question related to this lesson

None just yet!

Profiles are good way to put configuration related variables that will be available for login or non login shells. Login shells are created at when someone log into the system, whereas! non login shells are shells which are created by logged in user or automated scripts, examples include, su, graphical terminals, executed scripts or any other bash instances. Mac OS runs login shell by default for each terminal window.

Let's go to the user home directory of the current logged in user and type LS -a command. We are using -a switch to display all files under the folder. files starting with dot are hidden files, which will not be displayed with simple LS command. when directory listing is displayed, you will see files such as

1. .bash_profile
2. .bashrc
3. .bash_logout
4. .bash_history

When a login shell is started, /etc/profile is the first script to be loaded, this means that configuration you've set in this script is activated first for every user who log in to the system. After profile, .bash_profile is called. This file in turns call .bashrc. Each of these scripts can undo changes of previously activated script. For Example, an alias for a command you set in profile, will be modified by bash_profile and then by bashrc.

/etc/profile and .bash_profile is used for setting environment variables, such as PATH. In comparison, .bashrc is used to set local variables and defining aliases, change the default prompt with PS1 variable etc.

Let's try to understand with example.

Open /etc/profile script as super user and create an alias for LS command, alias is short cut to a longer version of a command. We have created an alias for LS command, let's call our alias LA. When you type LA and hit enter, its not immediately available. Let's logout and log back in. Type LA again, and Ta Da! its working for us. remember! /etc/profile configuration is available for login shells, thus we have to logout and log back in to see our changes.

Now let's modify our .bash_profile file and modify our LA alias by removing L switch from our LS command. Save the file and exit. however! LA command will not show any changes just yet, why? because .bash_profile gets activated for login shell. So let's logout and log back in. Ta Da! Its working now and bash_profile has modified our profile alias LA.

Now let's modify .bashrc file and modify our alias again. This time I will add couple of switches to our LS command. just open another terminal and type LA again and press enter. .bashrc has modified .bash_profile changes.

So now you understand the order by which these scripts gets activated. Let's now quickly change our command prompt to something more simpler. Open .bashrc in edit mode and add variable PS1 with value, hash. Save and edit. let's open terminal within current terminal for same user. and you see that our prompt has been updated.

.bash_logout can be used to execute scripts for cleaning things up.
.bash_history holds command history.

So you see that bash profiles offers flexibility for you and you can update these files to be more productive. As an exercise, add more aliases and set PATH or LESS variables, try to output some useful information such as operating system name and hostname on user login.

In next chapter, I will talk about command line expansions.