Search code examples
stringbashprefix

Bash replace prefix with spaces


How to remove prefix from string in Bash?

For example:

STRING1="My name is James"
echo "${STRING1}" 
# My name is James

NAME="${STRING1#???}"
echo "${NAME}" 
# James

Solution

  • Alternative: FOO=${FOO//$WORDTOREMOVE/}

    $ STRING1="My name is James"
    $ NAME="${STRING1//My name is /}"
    $ echo "${NAME}"
    James
    

    Update as per @gniourf_gniourf's suggestion : FOO=${FOO/#$WORDTOREMOVE/}

    $ NAME="${STRING1/#My name is /}"
    $ echo "${NAME}"
    James