Land the job you want — prepare
with Real interviews Q&A
Curated interview questions, company-wise guides and coding rounds. Practice mock interviews, improve with feedback, and track your progress.
Bash stands for Bourne Again Shell. It is a command-line shell and scripting language used mainly in Linux and Unix systems.
Bash allows users to:
- Run commands
- Automate tasks
- Create shell scripts
- Manage files and processes
#!/bin/bash
echo "Hello World"A shell script is a file containing a sequence of Bash commands that are executed automatically.
Shell scripts help automate repetitive tasks.
#!/bin/bash
name="AK"
echo $nameVariables in Bash are declared without data types.
No spaces should be used around =.
#!/bin/bash
name="AK"
echo $nameArithmetic operations in Bash are usually done using $(( )).
#!/bin/bash
num1=10
num2=20
sum=$((num1 + num2))
echo $sumAn if statement is used to execute commands conditionally.
#!/bin/bash
if [ 10 -gt 5 ]
then
echo "10 is greater"
fiA for loop is used to repeat commands multiple times.
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
doneA while loop repeatedly executes commands while a condition remains true.
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo $count
count=$((count + 1))
doneFunctions are reusable blocks of code used to perform specific tasks.
#!/bin/bash
function greet() {
echo "Hello AK"
}
greetPositional parameters are command-line arguments passed to a script.
- $1 → first argument
- $2 → second argument
- $# → total arguments
#!/bin/bash
echo $1
echo $2The read command is used to take input from the user.
#!/bin/bash
read -p "Enter your name: " name
echo "Welcome $name"Bash provides test operators to check files and directories.
#!/bin/bash
touch file.txt
if [ -f file.txt ]
then
echo "File exists"
fiA case statement is used for multiple condition checks.
#!/bin/bash
case $1 in
start)
echo "Starting"
;;
stop)
echo "Stopping"
;;
*)
echo "Invalid option"
;;
esacArrays store multiple values in a single variable.
#!/bin/bash
arr=("apple" "banana" "mango")
echo ${arr[0]}
echo ${arr[1]}Command substitution allows the output of a command to be stored in a variable.
#!/bin/bash
today=$(date)
echo $todaygrep is a command used to search text patterns inside files.
#!/bin/bash
grep "hello" file.txtThe ps command shows running processes.
#!/bin/bash
ps aux | grep nginxThe find command searches for files and directories.
#!/bin/bash
find . -name "*.txt"chmod changes file permissions in Linux.
#!/bin/bash
chmod +x script.shtar is used to archive and compress files.
#!/bin/bash
tar -czvf backup.tar.gz folder/Environment variables store system-wide values used by the shell and applications.
#!/bin/bash
#!/bin/bash
echo "Current User: $USER"
echo "Home Directory: $HOME"Every command in Bash returns an exit status.
- 0 → Success
- Non-zero → Error
#!/bin/bash
echo "Exit Status: $?"