Quick Links:
- Docs Bash - TLDP: https://tldp.org/LDP/abs/html
Running these scripts:
- Use Code Runner (VsCode Extension) - Google Doc
- Using
bmon
from themy_bin
repository (or usingbm
)
Just install this extension: https://marketplace.visualstudio.com/items?itemName=foxundermoon.shell-format and turn the default format in vsocde setting as 'null', so that appropriate formatters will be chosen for appropriate files.
Simple echo example.
#!/bin/bash
# set -x
echo Hello, from example.
#!/bin/bash
echo Hello i am sahil.
#!/bin/bash
var=~/Desktop
cd $var
ls
#/usr/bin/env bash
echo $UID
echo "If you run the script via 'sudo' prefixed you would see '0' value because
# that denotes if the program is initiaed as root access or not."
echo
echo "If the user executing the script has root permissions by default then wihout
sudo the output would be '0'"
#!/bin/bash
ROOT_UID=0 # Only users with $UID 0 have root privileges.
if [ "$UID" -ne "$ROOT_UID" ]; then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
echo Congrats you are root user.
#!/bin/bash
ROOT_UID=0
if [ "$UID" = "$ROOT_UID" ]; then
echo "Yes, you have root permissions!"
fi
if [ "$UID" != "$ROOT_UID" ]; then
echo "No, you dont have root permissions!"
fi
if [ "$UID" -ne "$ROOT_UID" ]; then # -ne is same as !=
echo "No, you dont have root permissions! (WITH '-ne')"
fi
if [ "$UID" = 1000 ]; then # POSIX sh
echo "You are a simple user with UID as '1000'."
fi
if [ "$UID" == 1000 ]; then # bash specific
echo "You are a simple user with UID as '1000'."
fi
# A phenomenal usage:
# https://stackoverflow.com/a/40431336/10012446
#!/bin/bash
echo Read this amazing answer on stackoverflow: https://stackoverflow.com/a/40431336/10012446
echo "There is lots of way to compare value like string and numbers differenctly and using the C way i.e., == operator as well."
#!/bin/bash
#This file demonstrates simple string manipulation with variables.
echo -----
A="Sahil" #Note you should ALWAYS use quotes around text when assigning it to variable.
B="${A} Rajput" #This is how we add text to string in bash.
echo $A
echo $B