Bash Programming Tutorial - Argument Handling

Posted by Suraj Singh on January 18, 2019 · 3 mins read
Hello readers,



Welcome Again To My Blog. Today, I am going To Share With You A Simple Bash Script. This Script Will Demonstrate How Bash Script Can Access User Pass Parameters Using Simple Variables And How Can Get Environment Variables.

This Bash Script is really very to Understand and By The Way, For Completely New Bies, I had Used Good Comments Lines To Make This Script More Easy To Understand.

To Run This Script

chmod +x path_to_script.sh

./path_to_script.sh


Bash Command Line Parameters Handling Script



#!/bin/bash
#
#
# Author :
# Suraj Singh Bisht
# surajsinghbisht054@gmail.com
# www.bitforestinfo.com
# github.com/surajsinghbisht054
#
#
# ====================================================
# Please Don't Remove Author name
# ====================================================
#
#
# Description:
# Script To Play With Command Line Parameters Inputs
#

# To Print Home Directory Path
printf "This is Our Home Directory : %s\n" $HOME

# To Print Current Working Directory Path
printf "This is Current Working Path : %s\n" $PWD

# Print All Environment Path
printf "Environment Path : %s\n" $PATH

# Numbers of Arguments Pass
printf "Numbers Of Arugments : %d\n" $#

# Iterate All Passed Arguments
printf "All Arguments In One Variable[1]: %s\n" $*
printf "All Arguments In One Variable[2]: %s\n" $@

# Name Of Current Script
printf "Name Of Current Script : %s\n" $0

# Current Process PID Numbers
printf "Process Id Number(PID) : %d\n" $$

# Previous Executed Program Exit Code
printf "Exit Code of Previous Program : %d\n" $?

# Last Argument Passed Through Command Parameters
printf "Last Argument : %s\n" $_

# Previous Execute Process Identity numbers
printf "Previous Command PID Number : %d\n" $!


 For More Updated Example, Check Github Here