Epoxsea Software Document Help

Shell Script

\#linux

To create a shell script, use the following steps:

  1. Create a file with a text editor containing the commands to execute

  2. Place on the first line: #!/bin/bash

  3. Use the chmod command to make the file executable: chmod u+x <file>

  4. Lets you run the script with ./script

Shebang

  • Tells the kernel what shell interpreter to use to run the script

  • Must be on the first line

    • #!/bin/bash

    • #!/usr/bin/ruby

#!/bin/bash hi="Hello World" echo $hi read -p "Please enter a number" var evho $var

Variable

Defining and accessing local variable

a="hi" echo $a #or echo ${a}

Accessing environment variable

echo $PATH #or set #or env #or export -p #or declare -x #or typeset -x

Defining environment variable

EW=1 export EW #or export EX=2 #or declare -x EY=3 #or typeset -x EZ=4

Deleting environment variable

unset EW

Special Variables

  • $0, $1, ..., $9, ${10}, ${11}, ... are the arguments to the script

  • $# means number of command line variables

  • $? is the exit code of the last command to be run

    • 0 means command executed successfully

    • other number means false

Conditional Statement

if, elif, else

if expression1 then statement elif expression2 then statement else statement fi

We can use test command to create the boolean expression The test command can also be written using two square brackets: []

String comparisons

Operator

Description

Example

-z

True iff length of string is zero

[ -z "$string" ]

-n

True iff length of string is not zero

[ -n "$string" ]

==

True iff strings are equal

[ "$string1" == "$string2" ]

!=

True iff strings are not equal

[ "$string1" != "$string2" ]

\>

True iff $string1 is sorted after $string2

[ "$string1" \> "$string2" ]

\<

True iff $string1 is sorted before $string2

[ "$string1" \< "$string2" ]

Notice that string are enclosed with double quote so that comparison can work even if there are space inside string2

File / Directory checking

Operator

Description

Example

-e

True iff file exists

[ -e $file ]

-s

True iff file size > 0

[ -s $file ]

-d

True iff file is a directory

[ -d $file ]

-f

True iff file is a plain file

[ -f $file ]

-r

True iff file has read permission for current user

[ -r $file ]

-w

True iff file has write permission for current user

[ -w $file ]

-x

True iff file has execute permission for current user

[ -x $file ]

Number comparison

Operator

Description

Example

-eq

True iff integers are equal

[ $int1 -eq $int2 ]

-ne

True iff integers are not equal

[ $int1 -ne $int2 ]

-gt

True iff first integer is greater than second integer

[ $int1 -gt $int2 ]

-ge

True iff first integer is greater than or equal to second integer

[ $int1 -ge $int2 ]

-lt

True iff first integer is less than second integer

[ $int1 -lt $int2 ]

-le

True iff first integer is less than or equal to second integer

[ $int1 -le $int2 ]

case (c++ switch)

case var in reg-exp) statement ;; reg-exp) statement ;; *) statement esac

While Statement

while expression do statement done

For Statement

for var in list_of_values do statement done
for (( <for-initialization> ; <bool-exp> ; <post-processing> )) do statement done
  • We can use seq statement to create the iterable list

    • seq <start_value> <increment_value> <end_value>

  • We can use { .. .. } statement to create the iterable list

    • {<start_value>..<end_value>..<increment_value>}

e.g.

for var in `seq 0 10 100` # from 0 to 100 increase 10 each time do statement done

e.g.

for var in {1..100..10} # from 0 to 100 increase 10 each time do statement done

e.g.

for (( i=0 ; i<5 ; i++ )) # from 0 to 5 increase 1 each time do statement done

Function

create function

function function_name() { # accessing arugment by $1, ..., $9, ${10}, ${11}, ... statement }

calling function

function_name <arugment1> <arugment2> ...
Last modified: 12 June 2025