1. Shell script comment variable and wildcard

As we probably all know, shell script is badly required, when you thought to automate something in your operating system. We are here using Born shell(sh) and bash in linux ubuntu.

cat /etc/shells #it will give us following shells
/bin/sh    # Bourne Shell
/bin/bash   # Bourne again shell unix and windows
/bin/rbash   # Restricted Shell for unix
/bin/dash  # Debian Almquist shell
/usr/bin/tmux  #Terminal Multiplexing

Shell Comments

We are making comments placing # symbol at the beginning of line which we need to comment out, or write comment should start from #.

#! /bin/bash
  • # ➜ For comments
  • ! ➜ It is called bang
  • /bin/bash ➜ We are using Bourne again shell

Special shell variables

  • $# = No. of parameters given to script
  • $@ = List of parameters given to script
  • $0 = Name of current program (script)
  • $1, $2.. = Parameter 1, 2 and so on..
  • $? = Exit value of last command run
  • $$ , $! = Both are process numbers
  • These variables are shell variables and only valid to the current shell.
echo "I am calling with $# parameters"
echo "My file is $0"
echo "First parameter is $1"
echo "Second parameter is $2"
echo "All parameters are $@"

After execution it will return

bash index.sh
I am calling with 0 parameters
My file is /home/kaushik/index.sh
First parameter is
Second parameter is    
All parameters are 
If we run it like this
bash index.sh pm1 pm2 pm3
I am calling with 3 parameters
My file is index.sh
First parameter is
Second parameter is pm2   
All parameters are pm1 pm2 pm3

Special Shell characters

  • * matches every character, just as in regular expressions.
  • So, ls *txt in a script will list all files whose name ends in txt.
  • \ is an escape character which tells the shell to not interpret the character after it.
  • \ is commonly used to escape the special characters such as *, $ etc.
    echo “Executing script : \”$0\” with $# parameters”

Key Words and Scope

Simple Variable#!/bin/bash
MY_text="Hello World"
echo $MY_text
( External program expr 
only expects numbers)
$x = “String”
expr $x + 1
expr: non-numeric argument
a=2; expr $a + 4
(interactively set variable
names using this)
echo What is your name?
read MY_NAME
echo “Hello $MY_NAME – What’s up.”
(Effect on the
scope of variables)
echo “The wall is : ${abc}”
echo “The wall is : ${abc}”
The wall is :
The wall is : test
export abc=’exported’
The wall is : exported
The wall is : test
$abc // will output ‘exported’
(create any field)
echo “Create File ${abc}_file.sh”
touch “${abc}_file.sh”
Will create exported_file.sh


How to copy all the files from /folder/a into /folder/b. Only the .php files or only the .html files

cp /folder/a/* /folder/b/
cp /folder/a/*.php /folder/b/
cp /folder/a/*.html /folder/b/

Linux list all environment variables

For printing env variables You can use following command with options

printenv| less or more
env| less or more
set| less or more
printenv > env.txtcat env.txt
printenv | grep HOMEFor searching

Few system variables I listed here for reference, this variables can be printed like this echo $HOME

HOMEHome Directory
PWDPresent Working Directory
USERCurrent login user
PATHIt specifies the directories in which executable programs* are located on the machine that can be started without knowing and typing the whole path to the file on the command line.
SSH_CLIENTSSH client info
LOGNAMEUsername of current session
HOSTNAMEPrint the host name you specified in host file
EDITORexport EDITOR=/bin/nano
export VISUAL=nano
Making nano is the default text editor
SHELLShell path
HISTSIZENumber of commands to be remember
BASH_VERSIONHolds the version of this instance of bash.
LANGDefault language
abcThis variable we exported with export $abc command