🤖
Guides
  • Introduction
  • Beginners
    • Getting Started
  • Guides
    • SQLi Walkthrough
    • My First BoF
    • OSCP Buffer Overflow Guide (Windows)
    • Parrot OS customisation
    • Terminal Customisation
    • Video Guides
  • Cheat Sheets
    • Reverse Shells
    • Tunnelling, Pivoting and Proxies
    • SQL Injection
      • WAF Bypass
      • SQLMap
      • DBMS Cheatsheets
        • MSSQL
        • MySQL
        • Oracle
        • SQLite
        • PostgreSQL
      • References
    • Bash Cheat Sheets
      • Terminal
      • Find
      • Grep
      • Sed
      • Awk
      • Xargs
      • System
      • Download
      • Networking
      • Hardware
      • Variable
      • Math
      • Data Manipulation
      • Random
      • Time
      • Condition and Loop
      • Other
    • OSINT
    • Ping Sweeps
  • Methodologies
    • VOIP Checklist
    • OWASP v4 Checklist
    • External Inf
    • Internal Infrastructure
  • Linux
    • Checklist - Linux Priv Esc
  • Windows
    • Checklist - Windows Priv Esc
  • Things to do/look at
Powered by GitBook
On this page
  • Variable
  • Variable substitution within quotes
  • Get the length of variable
  • Get the first character of the variable
  • Remove the first or last string from variable
  • Replacement (e.g. remove the first leading 0 )
  • Replacement (e.g. replace 'a' with ',')
  • Replace all (e.g. replace all 'a' with ',')
  • To change the case of the string stored in the variable to lowercase (Parameter Expansion)
  • Expand and then execute variable/argument

Was this helpful?

  1. Cheat Sheets
  2. Bash Cheat Sheets

Variable

Variable

Variable substitution within quotes

# foo=bar
 echo "'$foo'"
#'bar'
# double/single quotes around single quotes make the inner single quotes expand variables

Get the length of variable

var="some string"
echo ${#var}
# 11

Get the first character of the variable

var=string
echo "${var:0:1}"
#s

# or
echo ${var%%"${var#?}"}

Remove the first or last string from variable

var="some string"
echo ${var:2}
#me string

Replacement (e.g. remove the first leading 0 )

var="0050"
echo ${var[@]#0}
#050

Replacement (e.g. replace 'a' with ',')

{var/a/,}

Replace all (e.g. replace all 'a' with ',')

{var//a/,}
#with grep
 test="god the father"
 grep ${test// /\\\|} file.txt
 # turning the space into 'or' (\|) in grep

To change the case of the string stored in the variable to lowercase (Parameter Expansion)

var=HelloWorld
echo ${var,,}
helloworld

Expand and then execute variable/argument

cmd="bar=foo"
eval "$cmd"
echo "$bar" # foo
PreviousHardwareNextMath

Last updated 4 years ago

Was this helpful?