Shell scripting consists of set of commands to perform a task.
Different types of shells:
- bash
- sh
- ksh
- tsh
- fish
- zsh (Extended version of bash with more advanced features)
What is my shell type?
echo $0How to check supported shells?
cat /etc/shellsWhat is shebang?
It specifies which interpreter should be used to execute the script.
#!/bin/bashBasic shell script
#!/bin/bash
echo "hello world!"How to execute script?
Make sure script have execute permission rwx.
1- ./file.sh
2- path/file.sh
3- bash file.shHow to add comments?
1- single line comment
#comment
2- Multiline comment
<Variables:
How to use variables?
#!/bin/bash
person="Arbab Anjum"
hostName=$(hostname)
readonly role="superadmin"
echo $person is using $hostName and role is $role
------
> Output: Arbab Anjum is using ubuntu and role is superadminArray:
How to define and get value of an array?
Set Value:
myArray = (1 2 3 Hello "World");
Get Value:
echo "${myArray[0]}"
echo "${myArray[1]}"How to find length of an array?
echo "${#myArray[*]}"How to get specific values?
echo "${myArray[*]:1}"
echo "${myArray[*]:1:2}" // from index 1, get 2 valuesHow to update an array?
myArray+=(New 7 8)