Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter Expansion (Mutli-issue thread) #85

Open
jeff-hykin opened this issue Apr 16, 2024 · 0 comments
Open

Parameter Expansion (Mutli-issue thread) #85

jeff-hykin opened this issue Apr 16, 2024 · 0 comments

Comments

@jeff-hykin
Copy link
Owner

jeff-hykin commented Apr 16, 2024

Right now, these are supported:

  • $PARAMETER
  • ${PARAMETER}

These are not yet supported:
  • Indirection
    • ${!PARAMETER}
  • Case modification
    • ${PARAMETER^}
    • ${PARAMETER^^}
    • ${PARAMETER,}
    • ${PARAMETER,,}
    • ${PARAMETER~}
    • ${PARAMETER~~}
  • Variable name expansion
    • ${!PREFIX*}
    • ${!PREFIX@}
  • Substring removal (also for filename manipulation!)
    • ${PARAMETER#PATTERN}
    • ${PARAMETER##PATTERN}
    • ${PARAMETER%PATTERN}
    • ${PARAMETER%%PATTERN}
  • Search and replace
    • ${PARAMETER/PATTERN/STRING}
    • ${PARAMETER//PATTERN/STRING}
    • ${PARAMETER/PATTERN}
    • ${PARAMETER//PATTERN}
  • String length
    • ${#PARAMETER}
  • Substring expansion
    • ${PARAMETER:OFFSET}
    • ${PARAMETER:OFFSET:LENGTH}
  • Use a default value
    • ${PARAMETER:-WORD}
    • ${PARAMETER-WORD}
  • Assign a default value
    • ${PARAMETER:=WORD}
    • ${PARAMETER=WORD}
  • Use an alternate value
    • ${PARAMETER:+WORD}
    • ${PARAMETER+WORD}
  • Display error if null or unset
    • ${PARAMETER:?WORD}
    • ${PARAMETER?WORD}

Here's some documentation for future me, courtsey of akpircher

Quick Reference Guides

GNU Bash Documentation

  • Shell Parameter Expansion has a complete list of all parameter expansions syntax with examples, which also apply to arrays. Scroll past the paragraphs.

    • Detail hidden in paragraphs: the first 4 examples (the parameter value test expansions, like :-) do NOT need the colon. The colon means "unset or empty" and without the colon the tests just means "unset"
    • Documented ...somewhere?: when using a (numeric) variable for the key of an indexed array, or a variable for the offset/length, you can reference the variable without the expansion identifier ($)
      • This applies to any situation where shell arithmetic can be performed
  • Shell Arithmetic has a complete list of shell arithmetic operators

    • Detail hidden in first paragraph: the syntax is valid in the following:
      • let expression[s]
      • declare -i expression[s]
      • local -i expression[s]
      • ((expression[s])) and $((expression[s]))
    • Detail hidden later: bash supports the base syntax of
      base#value, which looks like it's covered.

Builtin references pages

  • Bash Builtins can direct you to where to find builtin commands/functions.
    • Pages you may care about are:
    • Ignore:
      • Modifying shell, it's a TOC for the set and shopt commands (not actually listed elsewhere)
      • "Special Builtins", describes behavior of previously listed builtins in POSIX mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant