0% found this document useful (0 votes)
3 views15 pages

Linux Scripting

This document is a comprehensive tutorial on Bash scripting, covering various topics such as creating scripts, using variables, passing arguments, executing commands, and handling user input. It includes practical examples and explanations of Bash constructs like loops, functions, and arrays. The tutorial is designed for beginners with no prior knowledge of Bash scripting, providing a step-by-step guide to learning the basics and advanced features.

Uploaded by

rayzibreezy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Linux Scripting

This document is a comprehensive tutorial on Bash scripting, covering various topics such as creating scripts, using variables, passing arguments, executing commands, and handling user input. It includes practical examples and explanations of Bash constructs like loops, functions, and arrays. The tutorial is designed for beginners with no prior knowledge of Bash scripting, providing a step-by-step guide to learning the basics and advanced features.

Uploaded by

rayzibreezy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

(/index.

php)
Contact us Advertise Here
(/contact-us) (/advertise-here)

Home Linux Tutorials Linux Commands Write For Us


(/) (/linux-tutorials) (/linux-commands) (/write-for-us)

Bash scripting Tutorial

Contents [Hide]
Hello World Bash Shell Script (https://linuxconfig.org/bash-scripting-tutorial#h1-hello-world-bash-shell-script)
Simple Backup bash shell script (https://linuxconfig.org/bash-scripting-tutorial#h2-simple-backup-bash-shell-script)
Variables (https://linuxconfig.org/bash-scripting-tutorial#h3-variables)
Global vs. Local variables (https://linuxconfig.org/bash-scripting-tutorial#h3-1-global-vs-local-variables)

Passing arguments to the bash script (https://linuxconfig.org/bash-scripting-tutorial#h4-passing-arguments-to-the-bash-script)


Executing shell commands with bash (https://linuxconfig.org/bash-scripting-tutorial#h5-executing-shell-commands-with-bash)
Reading User Input (https://linuxconfig.org/bash-scripting-tutorial#h6-reading-user-input)
Bash Trap Command (https://linuxconfig.org/bash-scripting-tutorial#h7-bash-trap-command)
Arrays (https://linuxconfig.org/bash-scripting-tutorial#h8-arrays)
Declare simple bash array (https://linuxconfig.org/bash-scripting-tutorial#h8-1-declare-simple-bash-array)
Read file into bash array (https://linuxconfig.org/bash-scripting-tutorial#h8-2-read-file-into-bash-array)

Bash if / else / fi statements (https://linuxconfig.org/bash-scripting-tutorial#h9-bash-if-else-fi-statements)


Simple Bash if/else statement (https://linuxconfig.org/bash-scripting-tutorial#h9-1-simple-bash-if-else-statement)
Nested if/else (https://linuxconfig.org/bash-scripting-tutorial#h9-2-nested-if-else)

Bash Comparisons (https://linuxconfig.org/bash-scripting-tutorial#h10-bash-comparisons)


Arithmetic Comparisons (https://linuxconfig.org/bash-scripting-tutorial#h10-1-arithmetic-comparisons)
String Comparisons (https://linuxconfig.org/bash-scripting-tutorial#h10-2-string-comparisons)

Bash File Testing (https://linuxconfig.org/bash-scripting-tutorial#h11-bash-file-testing)


Loops (https://linuxconfig.org/bash-scripting-tutorial#h12-loops)
Bash for loop (https://linuxconfig.org/bash-scripting-tutorial#h12-1-bash-for-loop)
Bash while loop (https://linuxconfig.org/bash-scripting-tutorial#h12-2-bash-while-loop)
Bash until loop (https://linuxconfig.org/bash-scripting-tutorial#h12-3-bash-until-loop)
Control bash loop with (https://linuxconfig.org/bash-scripting-tutorial#h12-4-control-bash-loop-with)

Bash Functions (https://linuxconfig.org/bash-scripting-tutorial#h13-bash-functions)


Bash Select (https://linuxconfig.org/bash-scripting-tutorial#h14-bash-select)
Case statement conditional (https://linuxconfig.org/bash-scripting-tutorial#h15-case-statement-conditional) Free Linux eBooks
Bash quotes and quotations (https://linuxconfig.org/bash-scripting-tutorial#h16-bash-quotes-and-quotations)
Escaping Meta characters (https://linuxconfig.org/bash-scripting-tutorial#h16-1-escaping-meta-characters)
Single quotes (https://linuxconfig.org/bash-scripting-tutorial#h16-2-single-quotes)
Double Quotes (https://linuxconfig.org/bash-scripting-tutorial#h16-3-double-quotes)
Bash quoting with ANSI-C style (https://linuxconfig.org/bash-scripting-tutorial#h16-4-bash-quoting-with-ansi-c-style)

Arithmetic Operations (https://linuxconfig.org/bash-scripting-tutorial#h17-arithmetic-operations)


Bash Addition Calculator Example (https://linuxconfig.org/bash-scripting-tutorial#h17-1-bash-addition-calculator-example)
Bash Arithmetics (https://linuxconfig.org/bash-scripting-tutorial#h17-2-bash-arithmetics)
Round floating point number (https://linuxconfig.org/bash-scripting-tutorial#h17-3-round-floating-point-number)
Bash floating point calculations (https://linuxconfig.org/bash-scripting-tutorial#h17-4-bash-floating-point-calculations)

Redirections (https://linuxconfig.org/bash-scripting-tutorial#h18-redirections)
STDOUT from bash script to STDERR (https://linuxconfig.org/bash-scripting-tutorial#h18-1-stdout-from-bash-script-to-stderr)
STDERR from bash script to STDOUT (https://linuxconfig.org/bash-scripting-tutorial#h18-2-stderr-from-bash-script-to-stdout)
stdout to screen (https://linuxconfig.org/bash-scripting-tutorial#h18-3-stdout-to-screen)
stdout to file (https://linuxconfig.org/bash-scripting-tutorial#h18-4-stdout-to-file)
stderr to file (https://linuxconfig.org/bash-scripting-tutorial#h18-5-stderr-to-file) Search Articles
stdout to stderr (https://linuxconfig.org/bash-scripting-tutorial#h18-6-stdout-to-stderr)
stderr to stdout (https://linuxconfig.org/bash-scripting-tutorial#h18-7-stderr-to-stdout)
stderr and stdout to file (https://linuxconfig.org/bash-scripting-tutorial#h18-8-stderr-and-stdout-to-file) search ...

Search

Friendly Sites
This bash script tutorial assumes no previous knowledge of bash scripting.As you will soon
Tuxmachines.org (http://tuxmachines.org/)
discover in this quick comprehensive bash scripting guide, learning the bash shell scripting is
(http://linuxconfig- very easy task. However, if you do not find an answer to your questions by reading this bash
tutorial or you need extra help, feel free to ask us on our new Linux Forum
(http://forum.linuxcareer.com). We will be more than happy to help you with your bash questions Latest Configs
there.

org.tradepub.com/free/w_mach02#linux- Simple Virtualization With Ubuntu 16.04


Lets begin this bash scripting tutorial with a simple "Hello World" script. Let's start with Learning Linux and KVM (/simple-virtualization-with-
offers)
the bash Shell: Unix Shell Programming
>PDF DOWNLOAD< (http://linuxconfig- ubuntu-16-04-and-kvm)
org.tradepub.com/free/w_mach02#linux- How to reset MySQL root password on your
Bash Scripting Beginners Guide
offers)
FREE PDF DOWNLOAD (http://linuxconfig-org.tradepub.com/free/w_mach02#linux-offers)
offers)
FREE PDF DOWNLOAD (http://linuxconfig-org.tradepub.com/free/w_mach02#linux-offers) Linux server (/how-to-reset-mysql-root-
password-on-your-linux-server)
Getting The RX 480 Running With
Bash Scripting Advanced Guide
(http://linuxconfig- AMDGPU on Linux (/getting-the-rx-480-
FREE PDF DOWNLOAD (http://linuxconfig-org.tradepub.com/free/w_advb01/?
running-with-amdgpu-on-linux)
p=w_advb01#linux-offers)
How to run X applications without a desktop
or a WM (/how-to-run-x-applications-
org.tradepub.com/free/w_advb01/? without-a-desktop-or-a-wm)
p=w_advb01#linux-offers) How to automatically execute shell script at
>PDF DOWNLOAD< (http://linuxconfig- startup boot on systemd Linux (/how-to-
org.tradepub.com/free/w_advb01/? automatically-execute-shell-script-at-
p=w_advb01#linux-offers) startup-boot-on-systemd-linux)
Basic PHP 7 and Nginx Configuration on
1. Hello World Bash Shell Script Ubuntu 16.04 Linux (/basic-php-7-and-
nginx-configuration-on-ubuntu-16-04-linux)
Ruby on Rails Development On Ubuntu
First you need to find out where is your bash interpreter located. Enter the following 16.04 Linux (/ruby-on-rails-development-on-
into your command line: ubuntu-16-04)
Fedora 24 Workstation Review: All Work
and Very Little Play (/fedora-24-workstation-
review-all-work-and-very-little-play)
$ which bash How to setup latest version of Django
framework with Python 3 on Debian 8
Jessie Linux (/how-to-setup-latest-version-
of-django-framework-with-python-3-on-
debian-8-jessie-linux)
Open up you favorite text editor and a create file called hello_world.sh. Insert the following lines to a file: How to reset lost root password on Ubuntu
NOTE:Every bash shell script in this tutorial starts with shebang:"#!" which is not read as a comment. First line is also a place where you 16.04 Xenial Xerus Linux (/how-to-reset-
put your interpreter which is in this case: /bin/bash. lost-root-password-on-ubuntu-16-04-xenial-
xerus-linux)
Here is our first bash shell script example:
Basic NFS Installation and Configuration on
Linux (/basic-nfs-installation-and-
#!/bin/bash
configuration-on-linux)
# declare STRING variable
Setting up apt proxy Server Approx on
STRING="Hello World"
Ubuntu Linux (/setting-up-apt-proxy-server-
#print variable on a screen
approx-on-ubuntu-linux)
echo $STRING
Linux command line basics for beginners:
Part 3 (/linux-command-line-basics-for-
beginners-part-3)
Navigate to a directory where your hello_world.sh is located and make the file executable:
Linux command line basics for beginners:

$ chmod +x hello_world.sh Part 2 (/linux-command-line-basics-for-


beginners-part-2)
Create and Restore manual Logical Volume
Snapshots (/create-and-restore-manual-

Now you are ready to execute your first bash script: logical-volume-snapshots)
I2P - Anonymity for the Masses (/i2p-
./hello_world.sh anonymity-for-the-masses)
Learning Linux Commands: man (/learning-
linux-commands-man)
Linux Command Line & Bash Shell
Shortcuts (/linux-command-line-bash-shell-
shortcuts)
2. Simple Backup bash shell script
How to Install Any Linux Distro on a
Chromebook (/how-to-install-any-linux-
#!/bin/bash distro-on-a-chromebook)
tar -czf myhome_directory.tar.gz /home/linuxconfig Configuration of High-Availability Storage
Server Using GlusterFS (/configuration-of-
high-availability-storage-server-using-
glusterfs)

3. Variables

In this example we declare simple bash variable and print it on the screen ( stdout ) with echo command.

#!/bin/bash
STRING="HELLO WORLD!!!"
echo $STRING

Your backup script and variables:

#!/bin/bash
OF=myhome_directory_$(date +%Y%m%d).tar.gz
tar -czf $OF /home/linuxconfig
3.1. Global vs. Local variables

#!/bin/bash
#Define bash global variable
#This variable is global and can be used anywhere in this bash script
VAR="global variable"
function bash {
#Define bash local variable
#This variable is local to bash function only
local VAR="local variable"
echo $VAR
}
echo $VAR
bash
# Note the bash global variable did not change
# "local" is bash reserved word
echo $VAR

4. Passing arguments to the bash script

#!/bin/bash
# use predefined variables to access passed arguments
#echo arguments to the shell
echo $1 $2 $3 ' -> echo $1 $2 $3'

# We can also store arguments from bash command line in special array
args=("$@")
#echo arguments to the shell
echo ${args[0]} ${args[1]} ${args[2]} ' -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}'

#use $@ to print out all arguments at once


echo $@ ' -> echo $@'

# use $# variable to print out


# number of arguments passed to the bash script
echo Number of arguments passed: $# ' -> echo Number of arguments passed: $#'

/arguments.sh Bash Scripting Tutorial

5. Executing shell commands with bash

#!/bin/bash
# use backticks " ` ` " to execute shell command
echo `uname -o`
# executing bash command without backticks
echo uname -o

6. Reading User Input


#!/bin/bash

echo -e "Hi, please type the word: \c "


read word
echo "The word you entered is: $word"
echo -e "Can you please enter two words? "
read word1 word2
echo "Here is your input: \"$word1\" \"$word2\""
echo -e "How do you feel about bash scripting? "
# read command now stores a reply into the default build-in variable $REPLY
read
echo "You said $REPLY, I'm glad to hear that! "
echo -e "What are your favorite colours ? "
# -a makes read command to read into an array
read -a colours
echo "My favorite colours are also ${colours[0]}, ${colours[1]} and ${colours[2]}:-)"

7. Bash Trap Command

#!/bin/bash
# bash trap command
trap bashtrap INT
# bash clear screen command
clear;
# bash trap function is executed when CTRL-C is pressed:
# bash prints message => Executing bash trap subrutine !
bashtrap()
{
echo "CTRL+C Detected !...executing bash trap !"
}
# for loop from 1/10 to 10/10
for a in `seq 1 10`; do
echo "$a/10 to Exit."
sleep 1;
done
echo "Exit Bash Trap Example!!!"

8. Arrays

8.1. Declare simple bash array

#!/bin/bash
#Declare array with 4 elements
ARRAY=( 'Debian Linux' 'Redhat Linux' Ubuntu Linux )
# get number of elements in the array
ELEMENTS=${#ARRAY[@]}

# echo each element in array


# for loop
for (( i=0;i<$ELEMENTS;i++)); do
echo ${ARRAY[${i}]}
done

8.2. Read file into bash array


#!/bin/bash
# Declare array
declare -a ARRAY
# Link filedescriptor 10 with stdin
exec 10<&0
# stdin replaced with a file supplied as a first argument
exec < $1
let count=0

while read LINE; do

ARRAY[$count]=$LINE
((count++))
done

echo Number of elements: ${#ARRAY[@]}


# echo array's content
echo ${ARRAY[@]}
# restore stdin from filedescriptor 10
# and close filedescriptor 10
exec 0<&10 10<&-

Bash script execution with an output:

linuxconfig.org $ cat bash.txt


Bash
Scripting
Tutorial
Guide
linuxconfig.org $ ./bash-script.sh bash.txt
Number of elements: 4
Bash Scripting Tutorial Guide
linuxconfig.org $

9. Bash if / else / fi statements

9.1. Simple Bash if/else statement

Please note the spacing inside the [ and ] brackets! Without the spaces, it won't work!

#!/bin/bash
directory="./BashScripting"

# bash check if directory exists


if [ -d $directory ]; then
echo "Directory exists"
else
echo "Directory does not exists"
fi

9.2. Nested if/else


#!/bin/bash

# Declare variable choice and assign value 4


choice=4
# Print to stdout
echo "1. Bash"
echo "2. Scripting"
echo "3. Tutorial"
echo -n "Please choose a word [1,2 or 3]? "
# Loop while the variable choice is equal 4
# bash while loop
while [ $choice -eq 4 ]; do

# read user input


read choice
# bash nested if/else
if [ $choice -eq 1 ] ; then

echo "You have chosen word: Bash"

else

if [ $choice -eq 2 ] ; then


echo "You have chosen word: Scripting"
else

if [ $choice -eq 3 ] ; then


echo "You have chosen word: Tutorial"
else
echo "Please make a choice between 1-3 !"
echo "1. Bash"
echo "2. Scripting"
echo "3. Tutorial"
echo -n "Please choose a word [1,2 or 3]? "
choice=4
fi
fi
fi
done

10. Bash Comparisons

10.1. Arithmetic Comparisons

-lt <
-gt >
-le <=
-ge >=
-eq ==
-ne !=

#!/bin/bash
# declare integers
NUM1=2
NUM2=2
if [ $NUM1 -eq $NUM2 ]; then
echo "Both Values are equal"
else
echo "Values are NOT equal"
fi
#!/bin/bash
# declare integers
NUM1=2
NUM2=1
if [ $NUM1 -eq $NUM2 ]; then
echo "Both Values are equal"
else
echo "Values are NOT equal"
fi

#!/bin/bash
# declare integers
NUM1=2
NUM2=1
if [ $NUM1 -eq $NUM2 ]; then
echo "Both Values are equal"
elif [ $NUM1 -gt $NUM2 ]; then
echo "NUM1 is greater then NUM2"
else
echo "NUM2 is greater then NUM1"
fi

10.2. String Comparisons

= equal
!= not equal
< less then
> greater then
-n s1 string s1 is not empty
-z s1 string s1 is empty

#!/bin/bash
#Declare string S1
S1="Bash"
#Declare string S2
S2="Scripting"
if [ $S1 = $S2 ]; then
echo "Both Strings are equal"
else
echo "Strings are NOT equal"
fi

#!/bin/bash
#Declare string S1
S1="Bash"
#Declare string S2
S2="Bash"
if [ $S1 = $S2 ]; then
echo "Both Strings are equal"
else
echo "Strings are NOT equal"
fi

11. Bash File Testing

-b filename Block special file


-c filename Special character file
-d directoryname Check for directory existence
-e filename Check for file existence
-f filename Check for regular file existence not a directory
-G filename Check if file exists and is owned by effective group ID.
-g filename true if file exists and is set-group-id.
-k filename Sticky bit
-L filename Symbolic link
-O filename True if file exists and is owned by the effective user id.
-r filename Check if file is a readable
-S filename Check if file is socket
-s filename Check if file is nonzero size
-u filename Check if file set-ser-id bit is set
-w filename Check if file is writable
-x filename Check if file is executable

#!/bin/bash
file="./file"
if [ -e $file ]; then
echo "File exists"
else
echo "File does not exists"
fi

Similarly for example we can use while loop to check if file does not exists. This script will sleep until file does exists. Note bash negator "!"
which negates the -e option.

#!/bin/bash

while [ ! -e myfile ]; do
# Sleep until file does exists/is created
sleep 1
done

12. Loops

12.1. Bash for loop

#!/bin/bash

# bash for loop


for f in $( ls /var/ ); do
echo $f
done

Running for loop from bash shell command line:

$ for f in $( ls /var/ ); do echo $f; done

12.2. Bash while loop

#!/bin/bash
COUNT=6
# bash while loop
while [ $COUNT -gt 0 ]; do
echo Value of count is: $COUNT
let COUNT=COUNT-1
done

12.3. Bash until loop

#!/bin/bash
COUNT=0
# bash until loop
until [ $COUNT -gt 5 ]; do
echo Value of count is: $COUNT
let COUNT=COUNT+1
done
12.4. Control bash loop with

Here is a example of while loop controlled by standard input. Until the redirection chain from STDOUT to STDIN to the read command
exists the while loop continues.

#!/bin/bash
# This bash script will locate and replace spaces
# in the filenames
DIR="."
# Controlling a loop with bash read command by redirecting STDOUT as
# a STDIN to while loop
# find will not truncate filenames containing spaces
find $DIR -type f | while read file; do
# using POSIX class [:space:] to find space in the filename
if [[ "$file" = *[[:space:]]* ]]; then
# substitute space with "_" character and consequently rename the file
mv "$file" `echo $file | tr ' ' '_'`
fi;
# end of while loop
done

13. Bash Functions

!/bin/bash
# BASH FUNCTIONS CAN BE DECLARED IN ANY ORDER
function function_B {
echo Function B.
}
function function_A {
echo $1
}
function function_D {
echo Function D.
}
function function_C {
echo $1
}
# FUNCTION CALLS
# Pass parameter to function A
function_A "Function A."
function_B
# Pass parameter to function C
function_C "Function C."
function_D

14. Bash Select

#!/bin/bash

PS3='Choose one word: '

# bash select
select word in "linux" "bash" "scripting" "tutorial"
do
echo "The word you have selected is: $word"
# Break, otherwise endless loop
break
done

exit 0
15. Case statement conditional

#!/bin/bash
echo "What is your preferred programming / scripting language"
echo "1) bash"
echo "2) perl"
echo "3) phyton"
echo "4) c++"
echo "5) I do not know !"
read case;
#simple case bash structure
# note in this case $case is variable and does not have to
# be named case this is just an example
case $case in
1) echo "You selected bash";;
2) echo "You selected perl";;
3) echo "You selected phyton";;
4) echo "You selected c++";;
5) exit
esac

16. Bash quotes and quotations

Quotations and quotes are important part of bash and bash scripting. Here are some bash quotes and quotations basics.

16.1. Escaping Meta characters

Before we start with quotes and quotations we should know something about escaping meta characters. Escaping will suppress a special
meaning of meta characters and therefore meta characters will be read by bash literally. To do this we need to use backslash "\" character.
Example:

#!/bin/bash

#Declare bash string variable


BASH_VAR="Bash Script"

# echo variable BASH_VAR


echo $BASH_VAR

#when meta character such us "$" is escaped with "\" it will be read literally
echo \$BASH_VAR

# backslash has also special meaning and it can be suppressed with yet another "\"
echo "\\"

16.2. Single quotes

Single quotes in bash will suppress special meaning of every meta characters. Therefore meta characters will be read literally. It is not
possible to use another single quote within two single quotes not even if the single quote is escaped by backslash.

#!/bin/bash

#Declare bash string variable


BASH_VAR="Bash Script"

# echo variable BASH_VAR


echo $BASH_VAR

# meta characters special meaning in bash is suppressed when using single quotes
echo '$BASH_VAR "$BASH_VAR"'
16.3. Double Quotes
Double quotes in bash will suppress special meaning of every meta characters except "$", "\" and "`". Any other meta characters will be
read literally. It is also possible to use single quote within double quotes. If we need to use double quotes within double quotes bash can
read them literally when escaping them with "\". Example:

#!/bin/bash

#Declare bash string variable


BASH_VAR="Bash Script"

# echo variable BASH_VAR


echo $BASH_VAR

# meta characters and its special meaning in bash is


# suppressed when using double quotes except "$", "\" and "`"

echo "It's $BASH_VAR and \"$BASH_VAR\" using backticks: `date`"

16.4. Bash quoting with ANSI-C style


There is also another type of quoting and that is ANSI-C. In this type of quoting characters escaped with "\" will gain special meaning
according to the ANSI-C standard.

\a alert (bell) \b backspace


\e an escape character \f form feed
\n newline \r carriage return
\t horizontal tab \v vertical tab
\\ backslash \` single quote
octal value of characters ( see hexadecimal value of characters ( see
\nnn \xnn
[http://www.asciitable.com/ ASCII table] ) [http://www.asciitable.com/ ASCII table] )

The syntax fo ansi-c bash quoting is: $'' . Here is an example:

#!/bin/bash

# as a example we have used \n as a new line, \x40 is hex value for @


# and \56 is octal value for .
echo $'web: www.linuxconfig.org\nemail: web\x40linuxconfig\56org'

17. Arithmetic Operations

17.1. Bash Addition Calculator Example

#!/bin/bash

let RESULT1=$1+$2
echo $1+$2=$RESULT1 ' -> # let RESULT1=$1+$2'
declare -i RESULT2
RESULT2=$1+$2
echo $1+$2=$RESULT2 ' -> # declare -i RESULT2; RESULT2=$1+$2'
echo $1+$2=$(($1 + $2)) ' -> # $(($1 + $2))'

17.2. Bash Arithmetics


#!/bin/bash

echo '### let ###'


# bash addition
let ADDITION=3+5
echo "3 + 5 =" $ADDITION

# bash subtraction
let SUBTRACTION=7-8
echo "7 - 8 =" $SUBTRACTION

# bash multiplication
let MULTIPLICATION=5*8
echo "5 * 8 =" $MULTIPLICATION

# bash division
let DIVISION=4/2
echo "4 / 2 =" $DIVISION

# bash modulus
let MODULUS=9%4
echo "9 % 4 =" $MODULUS

# bash power of two


let POWEROFTWO=2**2
echo "2 ^ 2 =" $POWEROFTWO

echo '### Bash Arithmetic Expansion ###'


# There are two formats for arithmetic expansion: $[ expression ]
# and $(( expression #)) its your choice which you use

echo 4 + 5 = $((4 + 5))


echo 7 - 7 = $[ 7 - 7 ]
echo 4 x 6 = $((3 * 2))
echo 6 / 3 = $((6 / 3))
echo 8 % 7 = $((8 % 7))
echo 2 ^ 8 = $[ 2 ** 8 ]

echo '### Declare ###'

echo -e "Please enter two numbers \c"


# read user input
read num1 num2
declare -i result
result=$num1+$num2
echo "Result is:$result "

# bash convert binary number 10001


result=2#10001
echo $result

# bash convert octal number 16


result=8#16
echo $result

# bash convert hex number 0xE6A


result=16#E6A
echo $result

17.3. Round floating point number


#!/bin/bash
# get floating point number
floating_point_number=3.3446
echo $floating_point_number
# round floating point number with bash
for bash_rounded_number in $(printf %.0f $floating_point_number); do
echo "Rounded number with bash:" $bash_rounded_number
done

17.4. Bash floating point calculations

#!/bin/bash
# Simple linux bash calculator
echo "Enter input:"
read userinput
echo "Result with 2 digits after decimal point:"
echo "scale=2; ${userinput}" | bc
echo "Result with 10 digits after decimal point:"
echo "scale=10; ${userinput}" | bc
echo "Result as rounded integer:"
echo $userinput | bc

18. Redirections

18.1. STDOUT from bash script to STDERR

#!/bin/bash

echo "Redirect this STDOUT to STDERR" 1>&2

To prove that STDOUT is redirected to STDERR we can redirect script's output to file:

18.2. STDERR from bash script to STDOUT

#!/bin/bash

cat $1 2>&1

To prove that STDERR is redirected to STDOUT we can redirect script's output to file:

18.3. stdout to screen

The simple way to redirect a standard output ( stdout ) is to simply use any command, because by default stdout is automatically
redirected to screen. First create a file "file1":

$ touch file1
$ ls file1
file1

As you can see from the example above execution of ls command produces STDOUT which by default is redirected to screen.

18.4. stdout to file


The override the default behavior of STDOUT we can use ">" to redirect this output to file:

$ ls file1 > STDOUT


$ cat STDOUT
file1

18.5. stderr to file

By default STDERR is displayed on the screen:

$ ls
file1 STDOUT
$ ls file2
ls: cannot access file2: No such file or directory

In the following example we will redirect the standard error ( stderr ) to a file and stdout to a screen as default. Please note that STDOUT
is displayed on the screen, however STDERR is redirected to a file called STDERR:

$ ls
file1 STDOUT
$ ls file1 file2 2> STDERR
file1
$ cat STDERR
ls: cannot access file2: No such file or directory

18.6. stdout to stderr

It is also possible to redirect STDOUT and STDERR to the same file. In the next example we will redirect STDOUT to the same descriptor
as STDERR. Both STDOUT and STDERR will be redirected to file "STDERR_STDOUT".

$ ls
file1 STDERR STDOUT
$ ls file1 file2 2> STDERR_STDOUT 1>&2
$ cat STDERR_STDOUT
ls: cannot access file2: No such file or directory
file1

File STDERR_STDOUT now contains STDOUT and STDERR.

18.7. stderr to stdout

The above example can be reversed by redirecting STDERR to the same descriptor as SDTOUT:

$ ls
file1 STDERR STDOUT
$ ls file1 file2 > STDERR_STDOUT 2>&1
$ cat STDERR_STDOUT
ls: cannot access file2: No such file or directory
file1

18.8. stderr and stdout to file


Previous two examples redirected both STDOUT and STDERR to a file. Another way to achieve the same effect is illustrated below:

$ ls
file1 STDERR STDOUT
$ ls file1 file2 &> STDERR_STDOUT
$ cat STDERR_STDOUT
ls: cannot access file2: No such file or directory
file1

or

ls file1 file2 >& STDERR_STDOUT


$ cat STDERR_STDOUT
ls: cannot access file2: No such file or directory
file1

AdChoices ► Linux Tutorial ► Linux Download ► Shell Script ► Linux Commands


3 Comments Linux Config 1 Login

Recommend 23 Share Sort by Newest

Join the discussion…

Alfonso Hernandez • 6 days ago


Could you please elaborate on this exercise: "2. Simple Backup bash shell script" I don't know what is being
backed up.

Thank you.
• Reply • Share ›

Lubos Mod Alfonso Hernandez • 6 days ago


It creates a backup of the entire /home/linuxconfig directory.
• Reply • Share ›

MyStockAlarm.com • 2 months ago


I got error -bash: ./hello_world.sh: /bin/bash^M: bad interpreter: No such file or directory
• Reply • Share ›

ALSO ON LINUX CONFIG

Deployment of Kippo SSH Honeypot on Ubuntu Rename all file names from uppercase to
Linux lowercase characters
1 comment • 2 months ago• 1 comment • 3 months ago•
Ben Sherkat — I get this when i try to run it can you Red Cricket — awesome! thank you!
please help. kippo@kali:/root/kippo-0.5$ ./start.sh
Starting kippo in background...Unhandled Error …

How to disable User Accounts in Linux Linux DOSBox emulator and DOS games now
1 comment • 2 months ago• free to download
saurabh rathor — I have a local user which is able to 1 comment • 3 months ago•
access directory but not able to list (ls) files in directory Fernando Basso — I'll give it a try. This stuff is
(permission denied). User has all rights. However, if … wonderful. Thanks for posting.

Subscribe Add Disqus to your site Add Disqus Add Privacy

Contact us (http://www.linuxcareer.com/contact-us)
(floss-technical-writer-linuxconfig-org)

Copyright 2007 - 2016 LinuxConfig.org. All rights reserved.

You might also like