Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize and replace characters in shell script in one echo

I am trying to find a way to capitalize and replace dashes of a string in one echo. I do not have the ability to use multiple lines for reassigning the string value.

For example: string='test-e2e-uber' needs to echo $string as TEST_E2E_UBER

I currently can do one or the other by utilizing

${string^^} for capitalization

${string//-/_} for replacement

However, when I try to combine them it does not appear to work (bad substitution error). Is there a correct syntax to achieve this?

echo ${string^^//-/_}

like image 636
Balry Avatar asked Oct 15 '25 08:10

Balry


2 Answers

This does not answer directly your question, but still following script achieves what you wanted :

declare -u string='test-e2e-uber'
echo ${string//-/_}
like image 109
Philippe Avatar answered Oct 17 '25 02:10

Philippe


Why do you dislike it so much to have two successive assignment statements? If you really hate it, you will have to revert to some external program to do the task for you, such as

string=$(tr  a-z- A-Z_ <<<$string)

but I would consider it a waste of resources to create a child process for such a simple operation.

like image 41
user1934428 Avatar answered Oct 17 '25 03:10

user1934428