Quotes are very crucial in Bash Shell Scripting. These are used
to define and delimit strings in shell or Shell scripts. It can
disable the special treatment of a special character. It is
also very helpful to prevent reserved words. You can use them
to run commands in shell, Processing variables in shell, etc.
There are mainly three types of quotes we used.
1. Single Quotes
2. Double Quotes
3. Back Quotes
1. Single Quotes: When you use single quotes in shell. It
treats characters literally means it will print as it is
mentioned in quotes. If you mentioned any special character in
quotes, it will lose its identity.
# echo 'My machine hostname is $HOSTNAME'
My machine hostname is $HOSTNAME
2. Double Quotes: Double quotes allow us to use command and
variable substitutions in shell or shell script. Basically,
It is use to print any messages and it can be use to print
out put of any variables as it performs the meaning of $ and
\ symbol etc.
# echo "My machine hostname is $HOSTNAME"
My machine hostname is kb-git.kb.com
3. Back Quotes: Back quotes is used to print commands output.
Anything enclosed in back quotes will be treated as command.
# echo "My machine hostname is `hostname`"
My machine hostname is kb-git.kb.com
Some Examples of quotes:
# echo `date`
Thu Feb 15 09:08:19 PM IST 2024
# echo `date +%A`
Thursday
# echo $(date +%A)
Thursday
# echo "Today is = $(date +%A)"
Today is = Thursday
# echo "Today is = $HOSTNAME"
Today is = kb-git.kb.com
# echo $(free -h)
total used free shared buff/cache available Mem: 1.7Gi 506Mi 966Mi 7.0Mi 444Mi 1.2Gi Swap: 1.0Gi 0B 1.0Gi
# echo "$(free -h)"
total used free shared buff/cache available
Mem: 1.7Gi 506Mi 966Mi 7.0Mi 444Mi 1.2Gi
Swap: 1.0Gi 0B 1.0Gi
[root@kb-git script]# echo "$(free -m)"
total used free shared buff/cache available
Mem: 1750 506 966 7 444 1244
Swap: 1023 0 1023
# echo `lsblk`
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sr0 11:0 1 1024M 0 rom nvme0n1 259:0 0 20G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot └─nvme0n1p2 259:2 0 19G 0 part ├─rl-root 253:0 0 18G 0 lvm / └─rl-swap 253:1 0 1G 0 lvm [SWAP]
# echo "`lsblk`"
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 1024M 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─rl-root 253:0 0 18G 0 lvm /
└─rl-swap 253:1 0 1G 0 lvm [SWAP]