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 $0
How to check supported shells?
cat /etc/shells
What is shebang?
It specifies which interpreter should be used to execute the script.
#!/bin/bash
Basic 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.sh
How 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 superadmin
Array:
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 values
How to update an array?
myArray+=(New 7 8)