Bash Scripting Basics Part 1

Bash Scripting Basics Part 1

#!/bin/bash
# simple hello world bash script

echo "Hello World!"

#############################

cp $1 $2 # here we have copied argument1 to argument2, in arguments we can copy any file from src                 # to dst
echo
echo Details for $2 # here we can get details of second argument
echo
ls -lh $2 # in output we can have list of second argument directory or path
echo
echo Below is the ls for $1 # here we have list of first argument directory/path
echo
ls -lh $3 # here we have list of third argument directory/path

#############################
# Other special variables   #
echo Name of the script is: $0
echo
echo $# - How many arguments were passed to the Bash script.
echo
echo $@ - All the arguments supplied to the Bash script.
echo
echo $? - The exit status of the most recently run process.
echo
echo $$ - The process ID of the current script.
echo
echo $USER - The username of the user running the script.
echo
echo $HOSTNAME - The hostname of the machine the script is running on.
echo
echo $SECONDS - The number of seconds since the script was started.
echo
echo $RANDOM - Returns a different random number each time is it referred to.
echo
echo $LINENO - Returns the current line number in the Bash script.

Comments