Share this page : facebooktwitterlinkedinmailfacebooktwitterlinkedinmail
Variables

Set a variable:

variable = value

Eg.

#! /bin/bash

firstvar=hello

sampleDir=/etc

echo $firstvar $sampleDir
hello /etc
Quotation

single quotes ( ‘ ) or double quotes ( ” ).

  • Single quotes will treat every character literally.
  • Double quotes will allow you to do substitution (that is include variables within the setting of the value).

Essentially a process is a running instance of a program.There could be several processes representing the same program running in memory at the same time, so variables with same name in different processes have different values if we have not export them.

When we export a variable, we tell Bash that every time a new process is created (to run another script or such) then make a copy of the variable and hand it over to the new process.

Special variables

 

  • $0 – The name of the Bash script.
  • $1 – $9 – The first 9 arguments to the Bash script. (As mentioned above.)
  • $# – How many arguments were passed to the Bash script.
  • [email protected] – All the arguments supplied to the Bash script.
  • $? – The exit status of the most recently run process.
  • $$ – The process ID of the current script.
  • $USER – The username of the user running the script.
  • $HOSTNAME – The hostname of the machine the script is running on.
  • $SECONDS – The number of seconds since the script was started.
  • $RANDOM – Returns a different random number each time is it referred to.
  • $LINENO – Returns the current line number in the Bash script.

 

#!/bin/bash

# A simple copy script

cp $1 $2

# Let’s verify the copy worked

echo Details for $2

cat $2

 

[email protected]:~/Documents# ./my.sh proxylist.txt ./result.txt
hello world
Details for ” ./result.txt ”
http://14.201.122.140:80
http://139.59.4.213:3128
http://125.253.96.212:8080
http://125.253.101.44:8080
http://139.59.238.215:8080
http://199.48.160.73:8080

Command Substitution

Command substitution allows us to take the output of a command or program (what would normally be printed to the screen) and save it as the value of a variable. To do this we place it within brackets, preceded by a $ sign.

myvar=$( ls /etc | wc -l )

echo There are $myvar entries in the directory /etc

there are 322 entries in the directory /etc

 

Example:

Find all the sub-directories in current directory and create tarball seperately:

find . -type d -maxdepth 1 -mindepth 1 -exec tar zcvf {}.tar.gz {}  \;

 

Input from user

read varname

Run the command read and save the users response into the variable varname.

Options:

Two commonly used options however are -p which allows you to specify a prompt and -s which makes the input silent. This can make it easy to ask for a username and password combination like the example below:

login.sh

#!/bin/bash
# Ask the user for login details

read -p 'Username: ' uservar

read -sp 'Password: ' passvar

echo

echo Thankyou $uservar we now have your login details

Multiple variables

echo What was your first, second and third car brand?

read car1 car2 car3


echo Your first car was: $car1

echo Your second car was: $car2

echo Your third car was: $car3

Your input should be separated by space.

 

 

Reference

http://ryanstutorials.net/bash-scripting-tutorial/bash-script.php

http://frankfu.click/linux/chapter-7-work-with-bash/