I’m studying bash, and I came across this Stackoverflow thread which contains this bit of code:

var="abcde"
echo ${var%d*}

The output is abc, but I can’t figure out why. I understand that %d is used to indicate an integer number and * represents anything, but I can’t figure out why those together would truncate var to only 3 characters.

You are viewing a single thread.
View all comments
20 points

That’s manipulation of the string held in var. Dollar sign and curly braces wrap the variable, the single percent sign say remove the shortest matching substring from the end of the variable contents, and the ‘d*’ is the substring to match, a lowercase d followed by any characters in this instance. var will still hold the entire original string, only the echo output is modified here. You can find more documentation here, https://tldp.org/LDP/abs/html/string-manipulation.html.

permalink
report
reply
4 points

That’s the exact Bible I would have linked to.

permalink
report
parent
reply

Bash

!bash@lemmy.ml

Create post

Talk about the Bash Shell and Bash scripting

Community stats

  • 11

    Monthly active users

  • 31

    Posts

  • 26

    Comments

Community moderators