Ask Your Question
0

How can one retrieve the last x characters of a string in Bash?

asked 2023-06-20 20:23:02 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-20 20:45:02 +0000

ladyg gravatar image

To retrieve the last x characters of a string in Bash, you can use the following syntax:

${string: -x}

Here, the variable string contains the string from which you want to retrieve the last x characters. The -x specifies the number of characters you want to retrieve, counting from the end of the string. The space in between : and -x is important.

For example, if you want to retrieve the last four characters of a string hello world, you can use the following command:

$ echo ${string: -4}

This will output orld.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-20 20:23:02 +0000

Seen: 12 times

Last updated: Jun 20 '23