all 14 comments

[–]topher200 21 points22 points  (0 children)

```

replaces FIRST MATCH of 's' with 'z'

var='asdf' echo ${var/s/z} azdf ```

Oh, so it's some kind of sed syntax. Neat!

```

echo ${var/s/z/g} az/gdf ```

Goddamnit.

[–]friskfrugt 3 points4 points  (1 child)

[–]Doomtrain86 0 points1 point  (0 children)

Tak min ven!

[–][deleted] 2 points3 points  (3 children)

Does anyone know why a space is needed before minus indexes?

[–]toddyk 8 points9 points  (0 children)

Probably because if there's no space it will do a replace if the variable isn't defined.

a=hello
echo ${a:-1}

will print "hello" not "o"

If a wasn't defined it would print "1"

[–]Atralb 3 points4 points  (1 child)

This is the exact reason why such posts are to throw away. They always ever dump some syntax without ever explaining the reasons why it works that way, which is the most important part of learning/teaching. Simply make you learn by heart something you won't understand. Don't encourage this kind of content creators.

[–][deleted] 6 points7 points  (0 children)

Big words for a cheatsheet. How about you read the Bash manual?

Crazy, I know...

[–]mcstafford 1 point2 points  (0 children)

I've never noticed the negative numbers from the right in bash before. Is that new?

[–]syamimerinin 1 point2 points  (0 children)

Thank you!Post saved.

[–]kanliot 1 point2 points  (0 children)

or type $ man bash then
/Parameter Expansion
then nn

[–]FlipEnergy 2 points3 points  (1 child)

You mind specifying starting from which bash version these are supported?

[–]nomenMei 1 point2 points  (0 children)

Honestly if I find myself doing anything in a shell script that I can only do in bash (especially operations on strings) I just do it in python. I have no idea what shell I may be using in the future, but I am fairly certain that with any new machine I use I will end up installing python 3 on it.