Search code examples
bashvariablesdata-conversioncapitalize

How to apply text modifications to bash special variables


I have bash script that takes input arguments using bash special variables like $1, $2 etc.

I want to apply some text modification using sed on $2 and want to transfer the end result to $2. How can I achieve this??

Alternatively, I just want to format the text inside $2 into a capitalised manner i.e. Bar, Neo, Me etc. no matter which format they provide me like bar, nEO, ME and want to persist this value into $2 variable.

main.sh
echo "${2^}"

./main.sh me nEO --> NEO -expected is-> Neo

For which I used ${2^} but was only successful with bar scenario


Solution

  • With bash:

    declare -c string="nEO"
    echo "$string"
    

    This converts the first character to uppercase and all others to lowercase.

    Output:

    Neo