'Bash delimit string without IFS

I have an email like [email protected]
and the output should be first_last

I tried delimiting on @ first and then replaced . with _
Is there an easier single line way to do it without using IFS?



Solution 1:[1]

You can use parameter expansion:

#! /bin/bash
[email protected]
name=${email%@*}   # Remove everything after the last @.
name=${name//./_}  # Replace all dots by underscores.
echo "$name"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 choroba