Did you know? Shell script is interpreted, not compiled.
-
clear terminal texts -
clear
-
current user
hostname
-
current driectory/folder
pwd
-
find the home directory of a user
home
(doesn't work on mac) -
Current shell name
echo $0
-
List avaialble shells on system cat: concatenate -
cat /etc/shells
-
chaneg to other shell
csh
orzsh
orbash
-
get date and time
date
-
download something. This example download astronomy image of the day from NASA API.
wget https://apod.nasa.gov/apod/image/2209/ShastaSky_Rohner_1640.jpg
-
zip everything of a directory.
.
means current directory.zip -r myfiles2.zip .
orzip -r myfiles.zip ./dir_name
-
list of things in current dir
ls
-
change folder/directory
cd /dir
-
change directory (move up - back one dir)
cd ../dir
-
move down one directory
cd -
, there's only one mother, so no dir name req
- run scripts
bash 1.helloworld.sh
, default but not good./1.helloworld.sh
- hello world
#!/bin/bash
# A script example
echo "Hello World!" # print something
- "Shebang” line to instruct which interpreter to use
!/bin/bash
- comments begin with
"#"
- Interactive Shell - one by one
- Non-interactive Shell - a shell that runs scripts
- subshell - shell inside a shell - shell script command contains bash command
- start with a letter or underscore
- reference a variable, prepend
$
, examplemy name is $name
- get current environment variables
printenv/env
(not tested) - local variables
name=value
- global variables
export name=value
echo "2 + 3" |bc
orecho 1 + 2 |bc
(bc means basic calculator)echo "3.8 + 4.2" |bc
- Syntax Strict spaces between condition and the brackets -
if [ condition ]; then
Do something
elif [ condition 2 ] ; then
Do something
else
Do something else
fi
- Example -
A=1
B=2
c=3
if [[ "$A" == "1" || "$B" == "2" || "$c" == "3" ]];
then
echo "cool"
else
echo "not cool"
fi
- to run
bash script_name.sh var1 var2 varN
- access
$1, $2, $3...
- add
{}
when>=10
$0
- shell script name- example
bash init.sh dev
This one I used in an actual AWS project-
cd ./Terraform
ls -a
echo "arg: $1"
if [[ "$1" == "dev" || "$1" == "stage" || "$1" == "prod" ]];
then
echo "environement: $1"
terraform init -backend-config=backend.$1.config
else
echo "Wrong Argument"
echo "Pass 'dev', 'stage' or 'prod' only."
fi
- syntax -
chmod 400 file_name
- For private key file -
chmod 400 my_key.pem
- command -
ssh -i "my_key.pem" uaser_name@ip_address
- syntax
file file_name
- example
file Boring-Website
Thanks to TutorialsPoint and some other sites/docs while googleing.