Skip to content

Latest commit

 

History

History
451 lines (351 loc) · 10.7 KB

File metadata and controls

451 lines (351 loc) · 10.7 KB

Defining Strings

#!/bin/bash

str1=Hello            # No spaces around =
str2="Hello World"    # Quotes allow spaces
str3='Single quotes'  # Literal string
echo $str1
echo $str2
echo $str3

Printing Strings

#!/bin/bash

str1=Hello            # No spaces around =
str2="Hello World"    # Quotes allow spaces
str3='Single quotes'  # Literal string
num1=1  # Literal string

echo $str1
echo "$str2"
echo "String is: %s\n" "$str3"
echo "num is is: %n\n" "$num1"

Format specifier

  • printf is the preferred method for formatted output in Bash.
  • Bash itself doesn’t interpret %s—it’s just part of the format string for printf.
Specifier Meaning Example
%s String printf "%s\n" "Hello"Hello
%d Signed decimal integer printf "%d\n" 4242
%i Integer (same as %d) printf "%i\n" 4242
%f Floating point decimal printf "%.2f\n" 3.141593.14
%e Scientific notation (lowercase) printf "%e\n" 12341.234000e+03
%E Scientific notation (uppercase) printf "%E\n" 12341.234000E+03
%x Hexadecimal (lowercase) printf "%x\n" 255ff
%X Hexadecimal (uppercase) printf "%X\n" 255FF
%o Octal printf "%o\n" 810
%% Literal % character printf "Progress: 50%%\n"Progress: 50%

Multiple values

first="Ram"
last="Misra"
printf "My name is %s %s\n" "$first" "$last"

printf "Name: %s, Age: %d\n" "Alice" 30
# Output: Name: Alice, Age: 30

printf "%s %s %s\n" "one" "two" "three"
# Output: one two three

Specify width, padding, and alignment:

printf "|%10s|\n" "Hi"
# Output: Right-align in 10 characters → |        Hi|

printf "|%-10s|\n" "Hi"
# Output: Left-align  → |Hi        |

printf "|%5d|\n" 42
# Output: Right-align number → |   42|

printf "|%-5d|\n" 42
# Output: Left-align number  → |42   |

Floating Point Formatting

num=3.14159
printf "%.2f\n" $num   # 2 decimal places → 3.14
printf "%8.3f\n" $num  # Width 8, 3 decimals →    3.142

Escapes and Special Characters

Escape Meaning Example Output
\n New line echo -e "Line1\nLine2" Line1
Line2
\t Tab echo -e "Col1\tCol2" Col1 Col2
\v Vertical tab echo -e "A\vB" A
B
\\ Backslash echo -e "C:\\Path" C:\Path
\" Double quote echo -e "He said \"Hi\"" He said "Hi"
\a Alert / bell echo -e "\a" (system beep)
\r Carriage return echo -ne "Hello\rWorld" World
printf "Line1\nLine2\n"
printf "Column1\tColumn2\n"
printf "Quote: \"%s\"\n" "Hello"

Concatenation

var1="Hello"
var2="World"
var3="${var1} ${var2}!"
echo "$var3"   # Hello World!

String Length

str="GeeksForGeeks"
echo ${#str}    # 13

Substring Extraction

str="GeeksForGeeks"
echo ${str:5}     # ForGeeks (from index 5)
echo ${str:5:3}   # For (from index 5, length 3)

Substring Matching (Trimming)

string="Welcome.to.GeeksForGeeks"

echo ${string#*.}   # Remove shortest match from front → to.GeeksForGeeks
echo ${string##*.}  # Remove longest match from front  → GeeksForGeeks

echo ${string%.*}   # Remove shortest match from back  → Welcome.to
echo ${string%%.*}  # Remove longest match from back   → Welcome

Substring Replacement

str="I like apples"

echo ${str/apples/oranges}     # Replace first → I like oranges
echo ${str//apples/oranges}    # Replace all   → I like oranges
echo ${str/ap*/bananas}        # Pattern match → I like bananas
echo ${string/a} # Remove the first occurrence of 'a'
echo ${string//a} # Remove all occurrences of 'a'

Searching in Strings

str="Hello World"
[[ $str == *World* ]] && echo "Found"
[[ $str == Hello*  ]] && echo "Starts with Hello"
[[ $str == *Hello ]] && echo "Ends with Hello"
[[ $str == *World* ]] || echo "Not Found"
[[ $str == Hello*  ]] || echo "Not Starts with Hello"
[[ $str == *Hello ]] || echo "Not Ends with Hello"

String Comparison

a="abc"
b="xyz"

if [[ "$a" == "$b" ]]; then
  echo "Equal"
elif [[ "$a" < "$b" ]]; then
  echo "a is less"
else
  echo "a is greater"
fi

Changing Case

str="hello world"
echo ${str^^}   # HELLO WORLD (uppercase)
echo ${str,,}   # hello world (lowercase)

echo ${str^}    # Hello world (capitalize first letter)
echo ${str,}    # hello world (lowercase first letter)

Trimming Whitespace

str="   hello   "
echo "[$str]"             # [   hello   ]

echo "[$(echo $str)]"     # Trim in subshell
echo "[$(echo $str | xargs)]" # [hello]

Splitting Strings

str="apple,banana,cherry"
IFS=',' read -r -a arr <<< "$str"
echo "${arr[0]}"   # apple
echo "${arr[1]}"   # banana
echo "${arr[2]}"   # cherry

for item in "${arr[@]}"; do
    echo "$item"
done

Explanation:

IFS=','

  • Internal Field Separator: tells Bash to split the string on commas. read -r -a arr <<< "$str"
  • read reads a line from input.
  • -r prevents backslash escapes from being interpreted.
  • -a arr stores the split values into an array called arr.
  • <<< "$str" uses a here-string, sending the content of $str as input to read.

Arrays from Strings

str="Hello there world"
arr=($str)
echo ${arr[@]}     # Hello there world
echo ${#arr[@]}    # 3 (number of words)

Reversing Strings

str="Hello"
echo "$str" | rev    # olleH

Check Empty String

str=""
if [[ -z "$str" ]]; then
  echo "Empty"
fi

if [[ -n "$str" ]]; then
  echo "Not empty"
fi

Extract Filename / Path

path="/home/user/file.txt"
echo ${path##*/}   # file.txt (remove longest prefix)
echo ${path%%/*}   # home (remove longest suffix)
echo ${path%/*}    # /home/user (directory only)
echo ${path##*.}   # txt (extension)

Multiple String Substitutions

str="banana apple cherry"
echo ${str//a/A}   # bAnAnA Apple cherry

Using Regex with [[ ]]

str="abc123"
if [[ $str =~ [0-9]+ ]]; then
  echo "Contains digits"
fi

Advanced with awk / sed

str="Hello World"
echo "$str" | awk '{print toupper($0)}'   # HELLO WORLD
echo "$str" | sed 's/World/Bash/'         # Hello Bash

Parameter Expansion Tricks

var="abcdef"
echo ${var:0:3}   # abc
echo ${var:(-3)}  # def (last 3 chars)
echo ${var: -3}   # def (space before - required)

Default Values in Bash

Form Meaning
${var:-default} Use default if $var is unset or empty, but do not change $var.
${var:=default} Use default if $var is unset or empty, and assign it to $var.
${var:+value} Use value if $var is set and not empty.
${var:?message} Throw an error with message if $var is unset or empty.
#!/bin/bash

# Variable not set
echo "${name:-Guest}"     # Guest (default, does NOT assign)

# Assign default if unset
echo "${name:=Guest}"     # Guest (assigns to $name)
echo "$name"              # Guest

# Value only if set
city="Delhi"
echo "${city:+India}"     # India
unset city
echo "${city:+India}"     # (empty)

# Error if unset
unset file
echo "${file:?File not defined}"  # Exits with message: File not defined

Perfect 👌 — this screenshot is a reference sheet for Bash scripting concepts. I’ll break it down for you with explanations and examples (so you don’t just see the code but also understand why it’s used).


🔹 Quoting

  • '' (single quotes) → no variable expansion

    echo '$HOME'   # prints literally: $HOME
  • "" (double quotes) → variable expansion

    echo "$HOME"   # prints: /home/username

🔹 Backquotes vs $( )

  • Backquotes: `command` → run command substitution.

  • Modern preferred way: $(command) (clearer, allows nesting).

    echo "Today is $(date)"

🔹 Shell Expansion

  • Pathname expansion: *, ?, []

    ls *.txt     # all files ending with .txt  
    ls a*        # all files starting with 'a'  
    ls ???       # all files with exactly 3 characters
    ls [A-Z]*    # files starting with uppercase letter
  • Brace expansion {}

    touch file{1..5}.txt   # creates file1.txt file2.txt … file5.txt

🔹 Command Substitution

  • Replaces command output with its value.

    echo "Today is $(date)"
    FILESIZE=$(wc -l < /etc/passwd)
    echo "Lines in passwd: $FILESIZE"

🔹 Arithmetic Expansion

  • Use $(( expression ))

    a=5
    b=10
    echo $((a+b))   # 15
    echo $((a*2))   # 10

🔹 Parameter Expansion

  • $var → get variable value

  • ${var} → safer way (especially when followed by characters)

    NAME="Mahin"
    echo "My name is $NAME"
    echo "My name is ${NAME}123"   # avoids confusion

🔹 Run Command as a Group

  • { } → group multiple commands in one block.

    { date; echo "This is grouped"; }

🔹 Exit Status

  • $? → holds status code of last command (0 = success, non-zero = error).

    ls /not/here
    echo $?   # non-zero (error)

🔹 Square Brackets (Conditions)

  • [ ] or [[ ]] → test conditions.

  • Examples:

    n=50
    if [ $n -le 100 ]; then
        echo "This is small value compared to 100."
    fi

🔹 Advanced Conditions

  • [[ $var == [a-z] ]] → pattern matching.

  • Example:

    read -p "Enter your name: " name
    if [[ $name == [a-z]* ]]; then
        echo "This is alphabetic"
    else
        echo "This is not alphabetic"
    fi

✅ So this screenshot covers:

  • Quoting rules
  • Variable/parameter expansion
  • Shell expansion (*, ?, [ ], { })
  • Command substitution
  • Arithmetic expansion
  • Pathname expansion (pattern matching)
  • Grouping commands { }
  • Conditions [ ] and [[ ]]